如何在powershell命令行,get-wmiobject和查询或过滤器

时间:2016-03-01 23:36:23

标签: powershell

我的命令和我得到的错误如下。我已经尝试过我能想到的所有引号,括号和反引号的组合。有谁知道如何让这个工作?

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:查询无效"从win32_service中选择*   命名= MSSQLSERVER"在行:1 char:6   +& {(Get-WmiObject -Class win32_service -Filter name = MSSQLSERVER).StartName}   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~       + CategoryInfo:InvalidArgument:(:) [Get-WmiObject],ManagementException       + FullyQualifiedErrorId:GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

1 个答案:

答案 0 :(得分:1)

在Windows批处理中,您需要使用^来转义(和)字符。 在Powershell中,您需要使用反引号来逃避单引号,以确保它被传递给wmi。 (我省略了呼叫运营商等,我认为你不需要那些)。

powershell.exe -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -InputFormat None ^
    -Command ^(Get-WmiObject -Class win32_service -Filter "name=`'MSSQLSERVER`'"^).StartName
相关问题