0

My command and the error I get are below. I've tried every combination of quotes, braces, and backticks I can think of. Does anyone know how to get this to work?

PS C:\Windows\system32> powershell.exe -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -InputFormat None -Command " & {(Get-WmiObject -Class win32_service -Filter "name='MSSQLSERVER'").StartName}"

Get-WmiObject : Invalid query "select * from win32_service where name=MSSQLSERVER" At line:1 char:6 + & {(Get-WmiObject -Class win32_service -Filter name=MSSQLSERVER).StartName} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

1 Answer 1

1

In the windows batch, you need to escape the ( and ) characters using the ^. In Powershell, you need to escape the single quote using the backquote to ensure it gets passed on to wmi. (I left out the call operator etc, I think you don't need those).

powershell.exe -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -InputFormat None ^
    -Command ^(Get-WmiObject -Class win32_service -Filter "name=`'MSSQLSERVER`'"^).StartName
Sign up to request clarification or add additional context in comments.

2 Comments

First of all, thanks so much for helping me. I've spent hours on this so far. I ran that and got this result: Get-WmiObject : Invalid query "select ^ from win32_service where name='MSSQLSERVER'" Another problem I would have with this solution if it worked is that I'm calling actually calling powershell through the powershell_out function in Chef, and it encloses the -Command parameter in double quotes like this -Command " <stuff I put in>" So I don't have control over those quotes, and they are just contributing to the quoting problem.
This ended up working for me: (Get-WmiObject -Class win32_service -Filter "name=`'MSSQLSERVER`'").StartName. Thank you! I couldn't have done it without you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.