在cmd上运行powershell命令

我试图在cmd上运行此powershell命令。当我直接从powershell运行它时,它起作用了。但是当我尝试从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
iCMS 回答:在cmd上运行powershell命令

尝试一下:

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"
,

您不需要使用来完成此任务,从提升的Windows命令提示符中,您可以使用

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

其他答案已经回答了您通过CMD运行powershell的问题。我建议您停止使用Win32_Product wmi类。您可以阅读任何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
本文链接:https://www.f2er.com/1781395.html

大家都在问