Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #################
- ### Functions ###
- #################
- Function Write-Log {
- Param ([string]$logString)
- Add-content $logFile -value $logString
- Write-Host $logString
- }
- function Uninstall-Hotfix {
- [cmdletbinding()]
- param(
- $computername = $env:computername,
- [string] $HotfixID
- )
- $logContents += "`r`nSearching for hotfix..."
- $hotfixes = Get-WmiObject -ComputerName $computername -Class Win32_QuickFixEngineering -Filter "HotFixId = '$HotfixID'"
- if($hotfixes -match $HotfixID) {
- $HotfixID = $HotfixID.Replace("KB","")
- $logContents += "`r`nFound the hotfix KB{0}." -f $HotfixID
- $logContents += "`r`nUninstalling the hotfix."
- $UninstallString = "wusa.exe /uninstall /KB:{0} /quiet /norestart" -f $HotfixID
- ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null
- #Invoke-Expression -Command $UninstallString
- while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyContinue).Count -ne 0) {
- Start-Sleep 10
- $logContents += "`r`nWaiting for update removal to finish ..."
- }
- $logContents += "`r`nCompleted the uninstallation of KB{0}." -f $HotfixID
- } else {
- $logContents += "`r`nGiven hotfix({0}) not found." -f $HotfixID
- }
- return
- }
- Function Get-WindowsUpdate {
- [Cmdletbinding()]
- Param()
- Process {
- try {
- $logContents += "Getting Windows Update"
- $Session = New-Object -ComObject Microsoft.Update.Session
- $Searcher = $Session.CreateUpdateSearcher()
- $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"
- $SearchResult = $Searcher.Search($Criteria)
- $SearchResult.Updates
- } catch {
- $logContents += "Failed to query Windows Update because $($_.Exception.Message)"
- }
- }
- }
- Function Set-WindowsHiddenUpdate {
- [Cmdletbinding()]
- Param(
- [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
- [System.__ComObject[]]$Update,
- [Parameter(Mandatory=$true)]
- [boolean]$Hide
- )
- Process {
- $Update | ForEach-Object -Process {
- if (($_.pstypenames)[0] -eq 'System.__ComObject#{c1c2f21a-d2f4-4902-b5c6-8a081c19a890}') {
- try {
- $_.isHidden = $Hide
- $logContents += "Hiding update $($_.Title)"
- } catch {
- $logContents += "Failed to perform action because $($_.Exception.Message)"
- }
- } else {
- $logContents += "Ignoring object submitted"
- }
- }
- }
- }
- #####################
- ### Configuration ###
- #####################
- $logContents = ""
- $logPath = "path\to\logdirectory"
- if (-not (Test-Path -Path $logPath)) {
- New-Item -Type "Directory" -Path $logPath | Out-Null
- }
- $KB = "KB3035583"
- $logFile = "$logPath\Rmv-$KB-$env:COMPUTERNAME-$(get-date -format 'MM-dd-yyyy').log"
- ############
- ### Main ###
- ############
- Uninstall-Hotfix -computername $env:COMPUTERNAME -HotfixID $KB
- Get-WindowsUpdate | Where { $_.Title -match $KB } | Set-WindowsHiddenUpdate -Hide $true -Verbose
- Write-Log -logString $logContents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement