Share via


Running Powershell Script from PHP

Question

Sunday, January 10, 2010 12:09 AM

Hi, 

I am trying to run a Powershell script from PHP using Apache on Windows Server 2008. I am using the following code to launch the powershell script: 

shell_exec('powershell.exe -command C:\PSscripts\helloworld.ps1')

Unfortunately I cannot get the script to run at all. I keep getting the error message in the apache log file:

File C:\PSscripts\helloworld.ps1 cannot be loaded because the execution of scripts is disabled on this system. 

I have used the "Set-ExecutionPolicy" command to set the policy to Unrestricted and the Apache service is running as Administrator but I can't for the life of me get it to run the Powershell script. 

Running the script directly from Powershell works fine.

Any help would be fantastic! 

Thanks.

All replies (4)

Sunday, January 10, 2010 1:21 AM âś…Answered | 1 vote

Make sure to go into both the 32-bit and 64-bit instances and set the execution policy then retry.  Perl is likely using the 32-bit instance of PowerShell.


Sunday, January 10, 2010 2:29 PM

Make sure to go into both the 32-bit and 64-bit instances and set the execution policy then retry.  Perl is likely using the 32-bit instance of PowerShell.

Worked like a charm! Turns out that PHP was running the 64bit version but it did not have the correct execution policy. 

Big Thanks! 


Tuesday, December 11, 2012 11:32 AM

works fine for me also, seems like executing Powershell on the command line or from PHP executes two different Powershell, each with it's own set of permissions :-(


Tuesday, July 30, 2013 9:22 AM

Hello,

I have the exact same issue on w2k8 R2 with IIS 7.5.

I tried these codes from php to execute my command:

$cmd = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Noninteractive -command ". e:\myscript.ps1 1;"';
    //$cmd = 'dir';
    if ( $a=exec($cmd) ) {
        echo "ok";
    }

I tried with shell_execute, but it doesn't work. Each time the script goes uuntil having an execute timeout 500 internal error.

How do you go into 64 bit powershell instance to set the policy ?