Invoke-Command -cn (Get-Content C:\Users\Administration\Documents\MyText.txt) -scriptblock {Start-Process -FilePath "MyProg.exe" -WorkingDirectory "C:\Program Files\ThisFolder\ThatFolder" -ArgumentList "-1", "-2", "-3"}
Obviously I'm doing something wrong, but not sure what. Anyone seeing something I'm missing?
The process is ending after your session on the remote system is closed. When i run the following test, notepad will run for 5 seconds on the remote computer, then close:
Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {start-process notepad.exe;Start-Sleep -Seconds 5}
If myprog.exe closes on it's own use the -Wait switch to wait for the process to close on it's own. You could also use New-PSSession to keep a session open until you are done then use Remove-PSSession to end the session and have the remote process end.
Related
I'm trying to remotely run this Windows Update Assistance Installer .exe and I notice that the .exe closes immediately unless I use the -wait command. However, if I use the -wait command I can't continue my foreach loop for the other computers since it takes hours for an install to finish. If I take the -wait command out, I think it launches then closes immediately.
$computers | % {
{more code...}
Invoke-Command -Session $Session -ScriptBlock {
$msbuild = "C:\windows\temp\Windows10Upgrade9252(21H2).exe"
$Args = '/quietinstall /skipeula /auto upgrade /copylogs'
Start-Process -FilePath $msbuild -ArgumentList $args -Wait
}
}
Run in parallel (within -throttlelimit) with $computers or $sessions as the computername/sessionname. Can also use -asjob. $args might be reserved already. Start-process won't return text output or the exit code without further action, which I've added. You might use scheduled tasks instead.
$sessions = new-pssession $computers
Invoke-Command $sessions {
$msbuild = "C:\windows\temp\Windows10Upgrade9252(21H2).exe"
$myargs = '/quietinstall /skipeula /auto upgrade /copylogs'
$p = start-process -wait $msbuild $myargs -passthru
[pscustomobject]#{exitcode=$p.exitcode]
} # -asjob
exitcode PSComputerName RunspaceId
-------- -------------- ----------
0 localhost 197d26f5-754b-49d3-baf4-2ca8fccacd4c
Other ways to wait for a program to finish: How to tell PowerShell to wait for each command to end before starting the next?
I'm trying to run a command on a VM using Invoke-Command. The command should stop a program that processes jobs after it finishes its current job. It works if I run it in the terminal using RDC.
& 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' /stop
But if I run it from a different machine using Invoke-Command nothing seems to happen.
$session = New-PSSession -ComputerName 'hostname' -Credential (Get-Credential)
Invoke-Command -Session $session -ScriptBlock {
& 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' /stop
}
However Process Monitor shows the command come in for both cases, but the program is still running.
I have also tried using Start-Process with the same result, i.e. it works in the terminal on the VM but not using Invoke-Command.
Start-Process -FilePath 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' -ArgumentList '/stop'
I've been stuck for many days and I've exhausted my googlable knowledge for this problem.
Are you sure that file exists on the remote computer?
For simplicity, I rewrote your command to a known executable that is always there in Windows and returns unique info for any given computer.
C:\> & 'C:\Windows\system32\HOSTNAME.EXE'
server1
C:\> icm {& 'C:\Windows\system32\HOSTNAME.EXE'}
server1
C:\> icm {& 'C:\Windows\system32\HOSTNAME.EXE'} -ComputerName server2
server2
Here's your script with some error handling.
$session = New-PSSession -ComputerName 'hostname' -Credential (Get-Credential)
Invoke-Command -Session $session -ScriptBlock {
$exe = 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe'
$ok = Test-Path $exe
if ($ok) {& $exe /stop} else {
Write-Warning "EXE not present on $($env:COMPUTERNAME)!"
}
}
Learn how to add error handling and you'll be well on your way to solving your problems faster and getting more stuff done.
$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Invoke-Command -ScriptBlock {start-process C:\Users\marshmellow\Documents\Some\xyz.bat }
Exit-PSSession
Above is the script which calls a bat file saved on remote server C:\Users\marshmellow\Documents\Some\xyz.bat
The bat file has two commands one which sets the working directory using "pushd" and another which stops a application process. The second command takes a couple of minutes to complete. I have found that the Start-Process doesn't wait for the second command to complete successfully, it just fires the command and closes the process.
Is there any way to make the Start-Process wait for the command to get completed successfully as I have already tried using -Wait which doesn't work.
If there's a way to even open a cmd session on the remote server and pass few commands in it saved in variables and that output is relayed to my PowerShell script even that is fine. can anyone please help?
Using cmd.exe might work?
Invoke-Command -ScriptBlock {
cmd /k "C:\Users\marshmellow\Documents\Some\xyz.bat"
} -ErrorAction Stop
If not, you could probably Start-Process -Wait on cmd.exe, then supply the batch commands as an -ArgumentList
What do you get if you try this?
Invoke-Command -FilePath $PathToBatchFile
You do not need the Invoke-Command cmdlet. Just use Start-Process with the -Wait parameter and pass the correct parameters to cmd:
$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Start-Process cmd -ArgumentList "/C C:\Users\marshmellow\Documents\Some\xyz.bat" -Wait
Exit-PSSession
Im trying to run a local script to run a server script:
Invoke-Command -ComputerName ServerName -Credential MyCredentials {Start-Process "C:\Program Files\test.new.cmd"}
So here comes the problem, it runs the Script, but not for long enough. It generates me only one file and the server script needs around 6 seconds to rule everything.
What I have to change to let it run for longer time?
Start-Process can wait for the command you are running by using the -Wait parameter. So you should use:
Invoke-Command -ComputerName ServerName -Credential MyCredentials {Start-Process "C:\Program Files\test.new.cmd" -Wait }
Hey I am very new to PowerShell and found one of Ed Wilson's helpful scripts on his blog: http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/12/force-a-domain-wide-update-of-group-policy-with-powershell.aspx.
I needed to customize it a little for my needs and just need some help getting the right code down.
I will just use his code because all I did was replace it with my credentials and AD info:
$cn = Get-ADComputer -filt *
$cred = Get-Credential iammred\administrator
$session = New-PSSession -cn $cn.name -cred $cred
icm -Session $session -ScriptBlock {gpupdate /force}
What I added was the next two lines to attempt to pause the script to allow the gpupdate to process then restart the computer(s):
Start-Sleep -s 120
Restart-Computer -ComputerName $cn.name
When I run the script all together it seems to just hang after I enter my credentials. My guess would be it doesn't like the way I present the Start-Sleep cmdlet because I can run the Restart-Computer cmdlet with success without Start-Sleep. The problem is it doesn't wait for gpupdate to finish so the policy doesn't get pushed. I do not get any errors when I run the script, it just hangs. I have left it running for about 10 minutes with no success.
I appreciate any help or suggestions to make this script work properly. Thanks in advance for any input.
There's nothing wrong with your sleep invocation but it isn't the best way to go. You can wait on the job to finish with a timeout (in case the command hangs) e.g.:
$job = icm -Session $session -ScriptBlock {gpupdate /force} -AsJob
Wait-Job $job -Timeout 120
Remove-PSSession $session
Restart-Computer $cn.name
#Keith Hill is right, sleep looks good. Another thing you can do is push the sleep/restart commands onto your target machines:
icm -Session $session -ScriptBlock {
gpupdate /force; Start-Sleep -s 120; Restart-Computer -Force}