Invoke-Command with script block doesn't work on remote machine without error
I am trying to call a batch file located on my local machine running a PowerShell command from a remote machine.
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
This gives no error, but nothing happened on the remote computer.
If I run the batch file from the local machine it works fine.
source to share
You cannot run a local file on a remote host. If the account abc\XXX
has administrator rights on your local computer (and access to administrative resources is enabled), you can try the following:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
param($myhost)
Start-Process "\\$myhost\c$\installagent.bat"
} -ArgumentList $env:COMPUTERNAME -Credential abc\XXX
Otherwise, you will first have to copy the script to the remote host:
Copy-Item 'C:\installagent.bat' '\\XXXXXX\C$'
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
Also, I would recommend using the call operator ( &
) instead Start-Process
to run the batch file:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
& "c:\installagent.bat"
} -Credential abc\XXX
This way, Invoke-Command
should return the output of the batch file, giving you a better idea of ββwhat's going on.
Or you can just use psexec
:
C:\> psexec \\XXXXXX -u abc\XXX -c installagent.bat
source to share