【问题标题】:How to use quotes with powershell command line, get-wmiobject, and query or filter如何在 powershell 命令行、get-wmiobject 以及查询或过滤器中使用引号
【发布时间】:2016-06-14 15:07:05
【问题描述】:

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

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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    在 windows 批处理中,您需要使用 ^ 转义 ( 和 ) 字符。 在 Powershell 中,您需要使用反引号对单引号进行转义,以确保将其传递给 wmi。 (我省略了呼叫操作员等,我认为您不需要这些)。

    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'" 如果这个解决方案有效,我会遇到的另一个问题是我通过 Chef 中的 powershell_out 函数调用实际调用 powershell,并将 -Command 参数括在这样的双引号中-Command " <stuff I put in>" 所以我无法控制这些引用,它们只是导致引用问题。
    • 这最终为我工作:(Get-WmiObject -Class win32_service -Filter "name=`'MSSQLSERVER`'").StartName。谢谢!没有你,我无法做到。
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多