Windows – Why does the PowerShell script hang when called in PSEXEC via a batch (.cmd) file

powershellpstoolsremote-accesswindows

I'm trying to remotely execute a PowerShell script using PSEXEC. The PowerShell script is called via a .cmd batch file. The reason we do this is to change the execution policy, run the powershell script then reset the execution policy again:

On the remote server do-tasks.cmd looks like:

powershell -command "&{ set-executionpolicy unrestricted}"  
powershell DoTasks.ps1  
powershell -command "&{ set-executionpolicy restricted}"  

The PowerShell script DoTasks.ps1 just does this for now:

Write-Output "Hello World!"

Both of these scripts live in c:\windows\system32 (for now) just so they're on the PATH.

On the originating server I do this:

psexec \\web1928 -u administrator -p "adminpassword" do-tasks.cmd

When this runs I get the following response at the command line:

c:\Windows\system32>powershell -command "&{ set-executionpolicy unrestricted}"

and the script runs no further.

I can't ctrl-c to break the script and I just see ^C characters, I can type input from the keyboard and the characters are echoed to console.

On the remote server I see that PowerShell.exe and CMD.exe are running in Task Manager's Process tab. If I end these processes then control returns to the command line on the originating server.

I have tried this with just a simple .cmd batch file with a @echo hello world and it works just fine.

Running do-tasks.cmd on the remote server via an RDP session works ok as well.

The originating server is running Windows 2003 SP2, the remote server is running Windows 2008 SP2.

Why is my remote batch file getting stuck when executing via PSEXEC?

Best Answer

Since the server is running 2k8 I would suspect UAC is causing issues. I would also suggest using winrm instead of psexec. Powershell remoting was one of the best new features of powershell 2.0