运行 通过 cmd 的 powershell 命令

run powershell command over cmd

我正在尝试通过 cmd 运行 这个 powershell 命令。当我直接从 powershell 运行 它时它起作用了。但是当我尝试 运行 if 从 cmd我收到错误

Powershell 命令:

(Get-WmiObject -Class Win32_Product -Filter "Name='Symantec Endpoint Protection'" -ComputerName localhost. ).Uninstall()

我如何运行它(cmd):

powershell.exe -Command (Get-WmiObject -Class Win32_Product -Filter Name='Symantec Endpoint Protection' -ComputerName localhost. ).Uninstall()

输出:

Get-WmiObject : Invalid query "select * from Win32_Product where Name=Symantec 
Endpoint Protection"
At line:1 char:2
+ (Get-WmiObject -Class Win32_Product -Filter Name='Symantec Endpoint P ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], Management 
   Exception
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C 
   ommands.GetWmiObjectCommand
 
You cannot call a method on a null-valued expression.
At line:1 char:1
+ (Get-WmiObject -Class Win32_Product -Filter Name='Symantec Endpoint P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

试试这个:

powershell.exe -Command "& {(Get-WmiObject -Class Win32_Product -Filter """Name='Symantec Endpoint Protection'""" -ComputerName XOS-MS182. ).Uninstall()}"

试试这些。括号对 cmd 有特殊意义。过滤器需要两组引号。由于管道在双引号内,cmd 将忽略它。

powershell "(Get-WmiObject -Class Win32_Product -ComputerName localhost | where name -eq 'symantec endpoint protection').Uninstall()"
powershell "Get-WmiObject win32_product -cn localhost | ? name -eq 'symantec endpoint protection' | remove-wmiobject"

您不需要使用 for this task, from an elevated Windows Command Prompt, (), you could use 而是:

WMIC.exe Product Where "Name='Symantec Endpoint Protection'" Call Uninstall

其他答案已经回答了您关于 运行 powershell over CMD 的问题。我建议您停止使用 Win32_Product wmi class。您可以阅读任何 never ending articles 解释原因。至于使用参数构建命令,我建议使用 splatting。作为专门针对删除 SEP 的奖励,这里是用于使用 MSIexec 和 guid 删除 Symantec Endpoint 的生产脚本的片段。

$DateStamp = get-date -Format yyyyMMddTHHmmss
$logFile = '{0}-{1}-{2}.log' -f 'SymantecUninstall',$PC,$DateStamp
$locallog = join-path 'c:\windows\temp' -ChildPath $logFile

$uninstalljobs = Foreach($PC in $SomeList){

    start-job -name $pc -ScriptBlock {
    Param($PC,$locallog)
        $script = {
        Param($locallog)
        $MSIArguments = @(
            "/x"
            ('"{0}"' -f '{A0CFB412-0C01-4D2E-BAC9-3610AD36B4C8}')
            "/qn"
            "/norestart"
            "/L*v"
            $locallog
        )
        
        Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
        }
        Invoke-Command -ComputerName $pc -ArgumentList $locallog -ScriptBlock $script
    } -ArgumentList $PC,$locallog

}

只需更新 guid 以匹配您的产品。如果您想从注册表中提取卸载字符串并使用它,它也比 Win32_Product.

更可取

您可以通过以下几种方法找到卸载字符串。

$script = {
    $ErrorActionPreference = 'stop'
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
    "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach{
        try
        {
            $key = reg query $_ /f "Symantec Endpoint" /s | select -skip 1 -first 1
            $key = $key -replace 'HKEY_LOCAL_MACHINE','HKLM:'
            (Get-ItemProperty $key -Name UninstallString).UninstallString
        }
        catch{}
    }
}
powershell.exe -command $script

$script = {
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach{
        Get-childitem $_ |
            Where {($_ | get-itemproperty -Name displayname -ea 0).displayname -like 'Symantec Endpoint*'} |
            Get-ItemPropertyValue -name UninstallString
    }
}
powershell.exe -command $script