Pergunta

I have been wracking my brain and my pc on this one, hoping someone can help me out here.

I have a PHP site that needs to execute a powershell script which in turn executes and executable.

the PHP that calls the .ps1 is

define('updaterPath','C:\\artemis\\DbUpdateUtility1.0.12.2');
define('outputLocation',updaterPath.'\\Output');
$CMD = updaterPath . '\\ArtemisXmlUtil.exe';
$action = 'import';
$args= ' -f' . outputLocation;
$psScriptPath = '.\\get-process.ps1';
$query = shell_exec("powershell -command $psScriptPath -cmd '$CMD' -action '$action' -paras '$args' < NUL");

my .ps1 script is:

param(
[string]$CMD,
[string]$action,
[string]$paras
)

& $CMD $action $paras

Right now, when I echo the full command, it looks perfect and can be copied and pasted into powershell and runs successfully, but when I try to actually execute it from php, it runs with only the $CMD variable and ignores the $action and $paras.

I tried concactenating the variables all into 1 string, but that fails as not even being able to see the executable.

the full command shouls look like:

C:\artemis\DbUpdateUtility1.0.12.2\ArtemisXmlUtil.exe import -fC:\artemis\DbUpdateUtility1.0.12.2\Output
Foi útil?

Solução

Ok, figured this one out on my own.

The problem is how the command was being executed. Using the "&" forced powershell to ignore the parameters being pass into the script. The correct way, since the command is an executable anyhow, was to build the entire command as a string and pass it all at once to the ps1 file and then change the ps1 file to use the invoke-expression commandlet.

param(
[string]$CMD
)

Invoke-expression $CMD
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top