Search code examples
powershellremote-serverpsexec

Errors running remote powershell script with PsEcec.exe


I have a powershell script on a remote windows box that finds the folder pointed to by a junction. The contents of the script looks like this:

return fsutil reparsepoint query C:\foo\bar\junction_name | where-object { $_ -imatch 'Print Name:' } | foreach-object { $_ -replace 'Print Name\:\s*','' }

When I run this on the remote box, it executes as expected :)

However, when I try to run this remotely from my local machine:

C:\Users\foo>C:\pstools\PsExec.exe \\remote_server_name "powershell D:\bar\my_script.ps1"

I get errors:

PsExec could not start powershell D:\bar\my_script.ps1 on remote_server_name: The filename, directory name, or volume label syntax is incorrect.

Any ideas what this error is telling me (given that I can run the script directly on the remote box with no issues)?

Thx!


Solution

  • I managed to get the following to work:

    PsExec.exe \\remote_server_name powershell.exe D:\bar\my_script.ps1
    

    However, the powershell session did not close as expected and remained in a hanging state after my script returned so calling it via cmd as detailed here seems to fix that:

    PsExec.exe \\remote_server_name cmd /c "echo . | powershell.exe D:\bar\my_script.ps1"
    

    Thanks for all of the suggestions...