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?

Link to comment
Share on other sites

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" }

Edited by requinix
Link to comment
Share on other sites

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" }

Link to comment
Share on other sites

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'\\\""

Link to comment
Share on other sites

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'\\\""

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.