Advertisement
Guest User

PowerShell - Remove KB3035583

a guest
Apr 4th, 2016
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #################
  2. ### Functions ###
  3. #################
  4.  
  5. Function Write-Log {
  6.     Param ([string]$logString)
  7.     Add-content $logFile -value $logString
  8.     Write-Host $logString
  9. }
  10.  
  11. function Uninstall-Hotfix {
  12.     [cmdletbinding()]
  13.     param(
  14.     $computername = $env:computername,
  15.     [string] $HotfixID
  16.     )            
  17.  
  18.     $logContents += "`r`nSearching for hotfix..."
  19.     $hotfixes = Get-WmiObject -ComputerName $computername -Class Win32_QuickFixEngineering -Filter "HotFixId = '$HotfixID'"
  20.  
  21.     if($hotfixes -match $HotfixID) {
  22.         $HotfixID = $HotfixID.Replace("KB","")
  23.         $logContents += "`r`nFound the hotfix KB{0}." -f $HotfixID
  24.         $logContents += "`r`nUninstalling the hotfix."
  25.         $UninstallString = "wusa.exe /uninstall /KB:{0} /quiet /norestart" -f $HotfixID
  26.         ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null
  27.         #Invoke-Expression -Command $UninstallString
  28.  
  29.         while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyContinue).Count -ne 0) {
  30.             Start-Sleep 10
  31.             $logContents += "`r`nWaiting for update removal to finish ..."
  32.         }
  33.         $logContents += "`r`nCompleted the uninstallation of KB{0}." -f $HotfixID
  34.     } else {
  35.         $logContents += "`r`nGiven hotfix({0}) not found." -f $HotfixID
  36.     }    
  37.     return
  38. }
  39.  
  40. Function Get-WindowsUpdate {
  41.  
  42.     [Cmdletbinding()]
  43.     Param()
  44.  
  45.     Process {
  46.         try {
  47.             $logContents += "Getting Windows Update"
  48.             $Session = New-Object -ComObject Microsoft.Update.Session          
  49.             $Searcher = $Session.CreateUpdateSearcher()          
  50.             $Criteria = "IsInstalled=0 and DeploymentAction='Installation' or IsPresent=1 and DeploymentAction='Uninstallation' or IsInstalled=1 and DeploymentAction='Installation' and RebootRequired=1 or IsInstalled=0 and DeploymentAction='Uninstallation' and RebootRequired=1"          
  51.             $SearchResult = $Searcher.Search($Criteria)          
  52.             $SearchResult.Updates
  53.         } catch {
  54.             $logContents += "Failed to query Windows Update because $($_.Exception.Message)"
  55.         }
  56.     }
  57. }
  58.  
  59. Function Set-WindowsHiddenUpdate {
  60.  
  61.     [Cmdletbinding()]
  62.  
  63.     Param(
  64.         [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
  65.         [System.__ComObject[]]$Update,
  66.  
  67.         [Parameter(Mandatory=$true)]
  68.         [boolean]$Hide
  69.     )
  70.  
  71.     Process {
  72.         $Update | ForEach-Object -Process {
  73.             if (($_.pstypenames)[0] -eq 'System.__ComObject#{c1c2f21a-d2f4-4902-b5c6-8a081c19a890}') {
  74.                 try {
  75.                     $_.isHidden = $Hide
  76.                     $logContents += "Hiding update $($_.Title)"
  77.                 } catch {
  78.                     $logContents += "Failed to perform action because $($_.Exception.Message)"
  79.                 }
  80.             } else {
  81.                 $logContents += "Ignoring object submitted"
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. #####################
  88. ### Configuration ###
  89. #####################
  90.  
  91. $logContents = ""
  92. $logPath = "path\to\logdirectory"
  93. if (-not (Test-Path -Path $logPath)) {
  94.     New-Item -Type "Directory" -Path $logPath | Out-Null
  95. }
  96. $KB = "KB3035583"
  97. $logFile = "$logPath\Rmv-$KB-$env:COMPUTERNAME-$(get-date -format 'MM-dd-yyyy').log"
  98.  
  99. ############
  100. ### Main ###
  101. ############
  102.  
  103. Uninstall-Hotfix -computername $env:COMPUTERNAME -HotfixID $KB
  104. Get-WindowsUpdate | Where { $_.Title -match $KB } | Set-WindowsHiddenUpdate -Hide $true -Verbose
  105. Write-Log -logString $logContents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement