khess Posted February 2, 2013 Share Posted February 2, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/ Share on other sites More sharing options...
requinix Posted February 2, 2013 Share Posted February 2, 2013 (edited) 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 February 2, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/#findComment-1409797 Share on other sites More sharing options...
khess Posted February 3, 2013 Author Share Posted February 3, 2013 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" } Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/#findComment-1409802 Share on other sites More sharing options...
kicken Posted February 3, 2013 Share Posted February 3, 2013 After reading the powershell help for a bit, this appears to work for me: system('powershell -Command "& {Get-WmiObject win32_service -ComputerName SERVER1} | Where { $_.StartMode -eq \'Auto\' }"'); Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/#findComment-1409803 Share on other sites More sharing options...
requinix Posted February 3, 2013 Share Posted February 3, 2013 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'\\\"" Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/#findComment-1409808 Share on other sites More sharing options...
khess Posted February 3, 2013 Author Share Posted February 3, 2013 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'\\\"" Quote Link to comment https://forums.phpfreaks.com/topic/273957-a-question-on-the-use-of-system-or-passthru/#findComment-1409818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.