0
votes

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

1 Answers

1
votes

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