Run PowerShell scripts on remote PC
Asked Answered
H

4

10

I have installed PS 1.0 on a remote PC(RPC001). I used Windows Sysinternals tool PSExec.exe to execute the following process on the remote:

 PSExec \\RPC001 -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

I can see the PowerShell.exe process running on the remote PC afterwards, but it is actually doing nothing, just hanging there. I tried to put a simple code of "Write-Output/Host" a string in the script. I run the same script on the remote by RTS, it works there.

Not sure if I miss anything else to run the script by using PSExec, or it is PSExec.exe limitation. I would like to start a PS script on remote to do something there locally (compress some files locally and remove old files) from my box.

I asked a similar question in Stackoverflow: Run remote process by powershell. Don suggested me to use PSExec. It sounds like an alternative way to solve the issue. However, I cannot get it working with PowerShell. Any way to get PS working on remote PC?

By the way, I cannot use PS 2.0 since my network does not allow me to install Windows XP SP3, which is required for PS 2.0.

Hereford answered 23/11, 2009 at 21:39 Comment(0)
H
9

After further investigating on PSExec tool, I think I got the answer. I need to add -i option to tell PSExec to launch process on remote in interactive mode:

PSExec \\RPC001 -i -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

Without -i, powershell.exe is running on the remote in waiting mode. Interesting point is that if I run a simple bat (without PS in bat), it works fine. Maybe this is something special for PS case? Welcome comments and explanations.

Hereford answered 24/11, 2009 at 0:8 Comment(1)
How can I achieve for this using PSExec Start-Process "wusa.exe" -ArgumentList "$W2K12 /quiet" -WaitButtonball
C
11

The accepted answer didn't work for me but the following did:

>PsExec.exe \\<SERVER FQDN> -u <DOMAIN\USER> -p <PASSWORD> /accepteula cmd 
    /c "powershell -noninteractive -command gci c:\"

Example from here

Cavender answered 20/4, 2012 at 6:41 Comment(4)
Same problem ... accepted ran and returned error code 0, but did nothing, whereas your solution workedIngate
I have "powershell -noninteractive -file C:\temp\scripts.ps1" which works for me (accepted answer did not).Northern
@Northern Where is your scripts.ps1 located? I am trying to get a script running remotely but to no avail.Photojournalism
@Northern in the folder 'c:\temp\' on each remote machine.Northern
H
9

After further investigating on PSExec tool, I think I got the answer. I need to add -i option to tell PSExec to launch process on remote in interactive mode:

PSExec \\RPC001 -i -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

Without -i, powershell.exe is running on the remote in waiting mode. Interesting point is that if I run a simple bat (without PS in bat), it works fine. Maybe this is something special for PS case? Welcome comments and explanations.

Hereford answered 24/11, 2009 at 0:8 Comment(1)
How can I achieve for this using PSExec Start-Process "wusa.exe" -ArgumentList "$W2K12 /quiet" -WaitButtonball
B
2

Can you try the following?

psexec \\server cmd /c "echo . | powershell script.ps1"
Beltz answered 26/4, 2011 at 10:50 Comment(1)
i think you are missing the total path C:\script\StartPS.ps1Nuptial
N
2

Accepted answer doesn't work for me, but this does. Ensure script in the location (c:\temp_ below on each remote server. servers.txt contains a list of IP addresses (one per line).

psexec @servers.txt -u <username> cmd /c "powershell -noninteractive -file C:\temp\script.ps1"
Northern answered 21/6, 2017 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.