Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-Command with script block is not working on remote machine with no error

I am trying to call a batch file located in local machine executing the below PowerShell command from remote computer.

Invoke-Command -ComputerName XXXXXX -ScriptBlock {
  Start-Process "c:\installagent.bat"
} -Credential abc\XXX

It's not giving any error but nothing happened on the remote computer.

If I run the batch file from local machine, it's working fine.

like image 477
Raju Banerjee Banerjee Avatar asked Oct 28 '25 01:10

Raju Banerjee Banerjee


1 Answers

You can't run a local file on a remote host like that. If the account abc\XXX has admin privileges on your local computer (and access to the administrative shares is enabled) you could try this:

Invoke-Command -ComputerName XXXXXX -ScriptBlock {
  param($myhost)
  Start-Process "\\$myhost\c$\installagent.bat"
} -ArgumentList $env:COMPUTERNAME -Credential abc\XXX

Otherwise you'll have to copy the script to the remote host first:

Copy-Item 'C:\installagent.bat' '\\XXXXXX\C$'

Invoke-Command -ComputerName XXXXXX -ScriptBlock {
  Start-Process "c:\installagent.bat"
} -Credential abc\XXX

Also, I'd recommend using the call operator (&) instead of Start-Process for running the batch file:

Invoke-Command -ComputerName XXXXXX -ScriptBlock {
  & "c:\installagent.bat"
} -Credential abc\XXX

That way Invoke-Command should return the output of the batch file, giving you a better idea of what's going on.

Or, you could simply use psexec:

C:\> psexec \\XXXXXX -u abc\XXX -c installagent.bat
like image 58
Ansgar Wiechers Avatar answered Oct 30 '25 22:10

Ansgar Wiechers