Last active
August 29, 2015 14:16
-
-
Save chelnak/12f8760d22f8c0a0cf77 to your computer and use it in GitHub Desktop.
Windows Hotfix Removal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Run - Get-Cluster CLUSTERNAME | Get-VM | Select Name | Sort Name | Export-Csv ./CLUSTERNAME.csv -NoTypeInformation | |
| $clusterVMs = Import-Csv '.\ASMCL06.csv' | |
| $KB = "KB2982791" | |
| $KBNumber = $KB.Replace("KB", "") | |
| $RemovalCommand = "wusa.exe /uninstall /kb:$KBNumber /quiet /norestart" | |
| $Domain = "corp.local" | |
| $report = @() | |
| $clusterVMs | % { | |
| $computerName ="$($_.Name).$($Domain)" | |
| $reportObject = New-Object PSObject | |
| Add-Member -InputObject $reportObject -MemberType NoteProperty -Name Name -Value $computerName | |
| Write-Host "Processing $computername " -NoNewLine | |
| try { | |
| $result = Invoke-Command -ComputerName $computerName -ScriptBlock { | |
| if ((Get-Hotfix -Id $Using:KB -ErrorAction SilentlyContinue)){ | |
| Invoke-Expression -Command $Using:RemovalCommand | |
| while ($true) { | |
| Start-Sleep 3 | |
| if ((Get-Process | Where-Object {$_.name -eq "wusa"}).Count -eq 0 -or (Get-Process | Where-Object {$_.name -eq "wusa"}).Count -eq $null){ | |
| break | |
| } | |
| } | |
| if (!(Get-Hotfix -Id $Using:KB -ErrorAction SilentlyContinue)){ | |
| Write-Host "|KB Removed|" -ForegroundColor GREEN | |
| $result = "KB Removed" | |
| } | |
| } | |
| else { | |
| Write-Host "|Could not find KB|" -ForegroundColor GREEN | |
| $result = "Could not find KB" | |
| } | |
| #Return result here | |
| $result | |
| } -ErrorAction Stop | |
| Add-Member -InputObject $reportObject -MemberType NoteProperty -Name KBRemoval -Value $result | |
| } | |
| catch [Exception] { | |
| Write-Host "|Failed!|" -ForegroundColor RED | |
| Add-Member -InputObject $reportObject -MemberType NoteProperty -Name KBRemoval -Value $_.Exception.Message | |
| } | |
| #Add each new object to the report array | |
| $report += $reportObject | |
| } | |
| #Export the array to CSV | |
| $report | Export-Csv -Path "./hotfixReport-$(Get-Date -Format ttMMyyyHHmmSS).csv" -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment