Why does PsExec hang after successfully running a powershell script?

Solution 1:

Turns out this is a common problem. Found the solution a here. Essentially, if you pipe some data on stdin with cmd it will return propertly after execution (because it is being run via cmd, not powershell).


psexec \\target -u domain\username -p password cmd /c "echo . | powershell c:\path\script.ps1"

Solution 2:

I know the answer comes late it would have already been figured out, If not it might be useful for future visitors.

STDIN has to be re-directed in powershell execution inorder to be able to come from hang (Here it waits in STDIN). to be able to do this use -inputformat none

powershell -inputformat none -File powershell_script.ps1 will work.

Check - https://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected

Solution 3:

psexec \\target -u domain\username -p password -d powershell c:\path\script.ps1

Also fixes the problem.

The -d flag for psexec is like "run and exit" in a non-interactive way:

-d Don’t wait for the application to terminate.

Only use for non-interactive applications.