I am new to Powershell.I am trying to generate a war file in the location of jar.exe , any inputs highly appreciated.
I have tried to generate the war file using Start Process, But it is not generating the war file
$wargen=Start-Process -FilePath "cmd.exe" -ArgumentList "/K cd /d D:\Temp\ws\9.0\base\java\8.0\bin\jar.exe -cvf foo.war" -Verb "runas" -Verbose
You don't need to try so hard. It's not necessary to invoke Start-Process or cmd.exe to run a command. PowerShell is a shell; it can run commands. Just type the command at the PowerShell prompt and press Enter.
PS C:\> D:\Temp\ws\9.0\base\java\8.0\bin\jar.exe -cvf foo.war
I am wondering about why your example uses the runas shell verb to run the command as an elevated process; my guess is that this is unnecessary.
Related
Don't ask me why, but I'm trying to run a batch file from CMD as admin by using Powershell. I have the following:
Start-Process -FilePath "cmd.exe" `
-ArgumentList "/K cd /d C:\Users\$($User)\Desktop\Activation\'win and off 2013 act.bat'" `
-Verb "runas"
The CMD opens as admin, but I get an error saying that "The system cannot find the path specified." I know it's something to do with how I've written the path to the batch file, but can't figure it out.
As I said in the comment, the quotations are at the wrong place, and I just checked, and cmd would not accept single quotes anyway. So use
"/K cd /d ""C:\Users\$User\Desktop\Activation\win and off 2013 act.bat"""
I'm trying to install an application that is an MSI file during the build process of an image with Dockerfile.
I can get the MSI to install properly when I enter a PowerShell session on the container by running:
C:\Windows\Syswow64\msiexec.exe /i "C:\Folder\File Name with spaces.msi" /qn /l*v "C:\log.log"
I have tried everything I can think with various RUN commands using powershell and cmd in the dockerfile.
Anyone have any ideas if this is just a matter of properly escaping quotes or the spaces?
Thanks,
Using powershell you can install msi file using below piece of code.
$msiPath = "your msi file path"
$arguments = "/i `"$msiPath`" /quiet"
Start-Process msiexec.exe -ArgumentList $arguments -Wait
Use same quotes
I'm working on a utility to automate some processes and one task is to install a .msi file on a remote machine. The file is found in C:\Users\username on the remote machine and for simplicity's sake, the filename is file.msi. The command I'm using is:
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{cmd /c start /wait msiexec /i $installPath /quiet}
When I execute this on my local dev machine, it doesn't show any errors, but doesn't install the file.
However, when I copy the exact command inside the brackets and run it in a PowerShell script on the remote machine, it installs successfully. I know my $remoteMachine is correct because I use it extensively throughout the rest of the script.
I know the $installPath variable also isn't the issue because for testing purposes I hardcoded the full path and it still doesn't install.
I also have proper permissions on the remote machine because earlier in the script I copy and paste the .msi from one machine to another without a problem.
I've tried a combination of commands and have been stuck here for a while, so any help would be greatly appreciated!
Ideally, this should work.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{msiexec /i $installPath /quiet}
The reason it is failing is coz you are not passing the $installPath as argumentlist. Modify it like this.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{
param(
[Parameter(Mandatory=$true,
Position=0)]
$installPath
)
cmd /c start /wait msiexec /i $installPath /quiet
} -ArgumentList $installPath
But if it isn't working, here is a workaround that I used a while ago.
Create a .bat file with the command msiexec /i $installPath /quiet and push it to the location just like you pushed the msi file.
Now from the invoke scriptblock, simply call the bat file instead.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{C:\Users\Username\Install.bat}
where Install.bat is the name of your bat file.
Note: You might want to use the /norestart switch as well if you are not looking to cause a reboot. Depends on what you are trying to install.
Beginning in PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.
syntax of Using :- $Using:<VariableName>
In your case :
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{cmd /c start /wait msiexec /i $Using:installPath /quiet}
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.2#using-local-variables
Is there any possibility to run a cmd command from a different folder then the script home location (e.g. C:\ScriptHome)?
I mean e.g.
Cmd /C "C:\Test\test.exe"
but this exe should be called from e.g. "C:\RunTestExeHere"
Basically, it can be done in pure cmd, like cd "C:\RunTestExeHere" and after
C:\RunTestExeHere>C:\Test\test.exe
but can it be done in powershell?
Thank you!
Your best bet is to sandwich your external command between Push-Location and Pop-Location commands.
A simple example:
Push-Location -EA Stop C:\ # You'd use C:\RunTestExeHere instead
cmd /c dir # You'd use & "C:\Test\test.exe" instead
Pop-Location
Another option (the invocation syntax is less convenient):
Start-Process -Wait -NoNewWindow cmd -ArgumentList /c, dir -WorkingDirectory C:\
Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd