1

I have the following problem:

I hava a xampp sever running and I want it to execute a powershell. A php triggers a .bat file which contains the following code:

@echo
cd C:\OpenBR\bin
start /WAIT br -algorithm FaceRecognition -compare C:\xampp\htdocs\upload C:\xampp\htdocs\DP C:\xampp\htdocs\results\result.csv


start  /WAIT C:\xampp\htdocs\CSVconvert\sortieren.ps1 

start  /WAIT C:\xampp\htdocs\CSVconvert\Removedouble.ps1

start  /WAIT C:\xampp\htdocs\CSVconvert\remove_path.ps1

start  /WAIT C:\xampp\htdocs\CSVconvert\remove_foo.ps1

start  C:\xampp\htdocs\CSVconvert\remove_quoatation.ps1

The first part works fine, up until the point when i want to exec the powershell "sortieren.ps1". When I run the batch manually, it executes and does the job, when triggered via php, it doesn't. I set "Set-ExecutionPolicy Unrestricted" in both x86 and x64 shells. I am just confused because the normal command line works and powershell doesn't, even after setting it on unrestricted.

I viewed executing a Powershell script from php and PowerShell on Windows 7: Set-ExecutionPolicy for regular users but couldn't solve the problem. What did i miss?

Community
  • 1
  • 1
Monacco Franze
  • 1,541
  • 1
  • 11
  • 12
  • Start will mostly like open the ps1 file as if you double clicked it instead of running the script, you could try changing the Start lines to `Start /Wait C:\windows\system32\WindowsPowerShell\v1.0\Powershell.exe -file C:\xampp\htdocs\CSVconvert\sortieren.ps1` or alternatively `Start /Wait Powershell -file C:\xampp\htdocs\CSVconvert\sortieren.ps1 ` – Bluecakes Feb 08 '15 at 23:27

2 Answers2

2

The session you are running those commands in doesn't have the same environment variables as when you are using PowerShell to run them manually. You'll have to specify the absolute path to the powershell executeable and the scripts that you want to run so that they will be found.

start /WAIT C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\xampp\htdocs\CSVconvert\sortieren.ps1
campbell.rw
  • 1,366
  • 12
  • 22
  • When you run them manually, do you use the Powershell IDE or the CMD ? I think you are using the IDE, where you are giving extra settings. Try testing cmdline with CMD for finding out the special settings you need. – Walter A Feb 09 '15 at 08:23
0

Since the problem was the environment i thought you might benefit from a package that handles that aspect automatically. Here is a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');

$strCmd1   = 'first command from first script';
$return1  = $shellObj->exeCmd($strCmd1);

$strCmd2   = 'second command from first script';
$return2  = $shellObj->exeCmd($strCmd2);

Instead of triggering a single script you can just trigger each command individually and handle the return. You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script.

MerlinTheMagic
  • 575
  • 5
  • 16