chelnak
2/26/2015 - 3:45 PM

Windows Hotfix Removal

Windows Hotfix Removal

#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