使用wusa.exe卸载更新

问题描述:

我想写一个卸载更新的powershell脚本。使用wusa.exe卸载更新

我的问题是脚本在启动脚本时不运行wusa.exe

function Uninstall-Hotfix() { 
    [string]$parameters = "KB4041691" 
    [string]$KBs = @() 

    if ($parameters.Contains(":")) { 
     [object]$arguments = $parameters.Split(":") 
     foreach ($argument in $arguments) { 
      $argument.Replace("KB", "") 
      $KBs.add($argument) 

      for ($i = 0; $i -lt $KBs.length; $i++) { 
       Cmd /c wusa.exe /uninstall /KB:$KBs[$i] /quiet /norestart 
       Do { 
        Start-Sleep -Seconds 3 
       } 
       while (Wait-Process | Where-Object {$_.name -eq "wusa"}) 

       If (Get-Hotfix -Id $KBs[$i] -ErrorAction SilentlyContinue) { 
        Write-Host "KB $KBs[$i] Fehlerhaft" 
       } 
       Else { 
        Write-Host "KB $KBs[$i] Erfolgreich deinstalliert" 
       } 
      } 
     } 
    } 
    Else { 
     $parameter = $parameters.Replace("KB", "") 
     $parameter 
     cmd /c wusa.exe /uninstall /KB:$parameter /quiet /norestart 
     Do { 
      Start-Sleep -Seconds 3 
     } 
     while (Wait-Process | Where-Object {$_.name -eq "wusa.exe"}) 
    } 
} 
Uninstall-Hotfix 
+0

CMD不是命令,读取错误消息。使用启动进程或直接调用wusa.exe – guiwhatsthat

+0

试试'&wusa.exe/uninstall/KB:$ parameter/quiet/norestart' –

+0

@guiwhatsthat:Powershell显示没有错误,我看到其他脚本也使用“cmd”命令.. 。 – MRed

你的代码是非常沉重的,在我看来包含了太多的循环,你可以试试这个:

function Uninstall-Hotfix() { 
    Param(
     [Parameter(Mandatory=$true)][array]$Patches 
    ) 

    foreach($patch in $Patches){ 
     $numKb = $null 
     if($patch -match "\d{7}"){ 
      [int]$numkb = $Matches[0] 
      Write-Host $numKb 

      Start-Process wusa.exe -ArgumentList "/KB:$numKb /uninstall /quiet /norestart" -Wait 

      if(Get-Hotfix -Id $numKb -ErrorAction SilentlyContinue){ 
       Write-Host "KB $KBs[$i] Fehlerhaft" 
      } 
      else{ 
       Write-Host "KB $KBs[$i] Erfolgreich deinstalliert" 
      } 
     } 
     else{ 
      Write-Host "KB is not valid" 
     } 
    } 
} 
Uninstall-Hotfix -Patches KB4041691,KB1234567 
+0

谢谢。但这仍然无效。我发现,当我不使用参数“/安静”他打开wusa并问我我想卸载它,当我使用“/ quiet”参数时,他什么都不做,我看不到一个wusa进程......任何想法为什么这种方法不起作用?到目前为止,谢谢你 – MRed

+0

看起来它不是一个PowerShell的问题。如果你在Windows 10上,你可以看看这篇文章:https://social.technet.microsoft.com/F orums/windows/zh-CN/f6594e00-2400-4276-85a1-fb06485b53e6/issues-with-wusaexe-and-windows-10-enterprise?forum = win10itprogeneral – Manu

+0

我们使用远程监控和管理来推出脚本(全部机器使用Windows 7),但我们不能使用“/ quiet”开关时......我还发现,当您使用“/ uninstall/kb:..”开关时,不能使用“ /安静”。但是当您使用“/ uninstall ”开关时,您可以使用“/ quiet”。但是这个是什么意思?当你打开没有参数的wusa.exe时,它显示你可用的参数... – MRed