Jump to content

A question on the use of system() or passthru()


khess

Recommended Posts

I am trying to solve the following problem in a php script.

 

This works:

 

system("powershell -command get-wmiobject win32_service -computername SERVER1");

 

It returns the expected results.

 

However, this returns nothing:

 

system("powershell -command get-wmiobject win32_service -computername SERVER1 -filter "startmode='auto'"");

 

I've tried escaping the single and double quotes in almost every possible combination. For example:

 

...SERVER1 -filter \"startmode=\'auto\'\"

 

and

 

...SERVER1 -filter \"startmode='auto'\"

 

And nothing seems to work past the SERVER1.

 

Anyone have any ideas?

And you've verified that the command itself works?

 

You're using "s around the entire command which means you [a] don't use them inside the string, if you even need them in the first place, or escape them.

system("powershell -command get-wmiobject win32_service -computername SERVER1 -filter startmode=auto");

 

[edit] Tried it, your command doesn't work. I can't figure out how to get it to accept quotes in the SQL it generates.

 

This does work:

Get-WmiObject win32_service -ComputerName SERVER1 | Where { $_.StartMode -eq "Auto" }

Yes, this is PowerShell 3.0. The PowerShell command works, as does yours at the PS command line but yours doesn't work in my php script.

 

Is there a way that I can get the command to print out to screen to see how it's being parsed?

 

This hasn't worked:

 

$query = system (" command stuff");

echo $query;

 

 

And you've verified that the command itself works?

 

You're using "s around the entire command which means you [a] don't use them inside the string, if you even need them in the first place, or escape them.

system("powershell -command get-wmiobject win32_service -computername SERVER1 -filter startmode=auto");

 

[edit] Tried it, your command doesn't work. I can't figure out how to get it to accept quotes in the SQL it generates.

 

This does work:

Get-WmiObject win32_service -ComputerName SERVER1 | Where { $_.StartMode -eq "Auto" }

Finally got it working from cmd.exe.

powershell -command get-wmiobject win32_service -computername SERVER1 -filter \"startmode='auto'\"

So then you have to escape the backslashes and the quotes if you do it from PHP.

"powershell -command get-wmiobject win32_service -computername SERVER1 -filter \\\"startmode='auto'\\\""

Perfect!!! the three \'s made it work! Thank you very much.

 

Finally got it working from cmd.exe.

powershell -command get-wmiobject win32_service -computername SERVER1 -filter \"startmode='auto'\"

So then you have to escape the backslashes and the quotes if you do it from PHP.

"powershell -command get-wmiobject win32_service -computername SERVER1 -filter \\\"startmode='auto'\\\""

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.