We help IT Professionals succeed at work.

perl CGI script hangs calling powershell.exe

Please help!  I usually have no issue gathering output from command line using perl with the SYSYEM() or backticks `` and placing the results into variable. That is, until I tried to call the output of powershell.exe. The most basic command:

powershell.exe -command "& echo hello"

The perl script will run fine from the command line, however, running the same script from CGI via web browser fails. If I substitute the powershell command for any standard windows command I will see the output in the browser so it's not a script issue

I have also tried calling from a batch file:
@go = system "C:\\Inetpub\\wwwroot\\manage\\test.bat";

... I don't think this is a perl issue but rather IIS/windows restriction with powershell.exe. Other people have also been experiencing this issue:


Any thoughts on this?

Many Thanks
Watch Question

Based on what you pointed to, my suspicion is that your CGI program needs Administrator access in its security properties to be able to execute PowerShell.

You can right-click on the script and change the user and password for that CGI program in the IIS Manager utility.  Technically, you can change cgi-bin in general, but I don't recommend putting Administrator on more than is necessary.

My educated guess is that the IIS default user does not actually have permissions to execute PowerShell.  That could be due to something about PowerShell itself, or it could be due to PowerShell's location, assuming I'm right at all.
Meir RivkinFull stack Software Engineer

try giving full path to powershell.
check if cgi has sufficient permissions
I worked it out finally... POWERSHELL.EXE will hang because on a remote script from IIS because it does not close off the POWERSHELL.EXE process automatically... all you have to do is add < NUL to the end of the command. For some reason this only happens from IIS and not if the script is run from command line. This fixed the problem of powershell hanging. I also notice that you can't use IUSR_ANONYMOUS as POWERSHELL.EXE makes an active directory connection... you have to setup IIS authentication to your domain. Once you authenticate with DOMAIN\USERNAME you should be good.