Why does my PowerShell script hang when called in PSEXEC via a batch (.cmd) file?

19,220

Solution 1

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

Solution 2

This is a common issue with POSH. The problem is stdin hangs. Try this:

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

Solution 3

Most of the answers in the forums solve this with a workaround like echoing and piping so that powershell gets some input from STDIN.

But there exits a solution within powershell. Just start powershell with the option -inputformat none like:

powershell -inputformat none -command ...

This solves the hanging issue via psexec for Win2003 and Win2008.

Share:
19,220

Related videos on Youtube

Kev
Author by

Kev

###Actively looking for freelance work ###About Me: I'm a professional software developer and have spent my time building provisioning and web based self-service systems for IIS, Apache and Citrix XenServer, amongst other things. My Curriculum Vitae can be viewed on Stack Overflow Careers (might be a bit out of date). Stuff I like to listen to at last.fm You can get in touch here: kevin.e.kenny #@# gmail.com (you know what to do with the # and spaces). No Survey Emails Please. Also not ashamed to admit I like trains, mostly diesels, late Era 8 (BR Sectorisation) and Era 9 onwards :) I'm also interested in signalling if anyone from Network Rail is looking this far down ;)

Updated on September 17, 2022

Comments

  • Kev
    Kev over 1 year

    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?

    • Admin
      Admin about 14 years
      Are you somehow being forced to use psexec or can you use winrm instead? What Os is this running on?
    • Admin
      Admin about 14 years
      @Jimb - The originating server is running Windows 2003 SP2, the remote server is running Windows 2008 SP2. I'm not constrained to PSEXEC. WinRM is a new one on me, will take a look.
  • Kev
    Kev about 14 years
    I wouldn't expect the local Administrator account to hang on a UAC hidden prompt, a regular account that's a member of administrators maybe. That said I'll take a look at winrm in the fresh light of day.
  • Marco Shaw
    Marco Shaw about 14 years
    This is one thing I've not dived into. PowerShell.exe was designed to not be directly invoked remotely, in certain circumstances.
  • Yauheni Sivukha
    Yauheni Sivukha almost 13 years
    I have been looking for the solution for several days and finally found this answer. Thanks a lot!!!
  • MIGUEL ANGEL GONZALEZ PINTO
    MIGUEL ANGEL GONZALEZ PINTO over 12 years
    SonOfNun: You have it right. Using the < NUL works a lot better than other options I've tried. Thanks!
  • Nicolas Mommaerts
    Nicolas Mommaerts over 9 years
    people reading this thread should try this solution, works like a charm