Script patching via WSUS

Script en PoweShell pour vos Campagne de patch des serveurs

Prérequis : avoir un csv avec les noms des serveurs cibles

Test flush cache WU

######### Import CSV AD group Member #########

[System.Reflection.Assembly]::LoadWithPartialName(« System.windows.forms ») | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

$OpenFileDialog.initialDirectory = $InitialDirectory

$OpenFileDialog.filter = « CSV (*.csv)| *.csv »

$OpenFileDialog.ShowDialog() | Out-Null

$Csv = @()

$Inputfile = $OpenFileDialog.filename

$Servers = Import-Csv $InputFile -Delimiter « ; »

Availability Test with invoke-command

ForEach ($Computer in $Servers){

Try{

Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ChildItem « C:\Windows\SoftwareDistribution\Download »} -ErrorAction Stop

}

Catch{

Add-Content \Sin1sus01\c$\temp\Unavailable-Computers.txt $Computer

}

}

Write-host « Servers non available : \Sin1sus01\c$\temp\Unavailable-Computers.txt »

Start operation

Foreach ($Server in $Servers){

Write-host « Start with $Server »

Invoke-Command -ComputerName $Server -ScriptBlock{

Log path

$Path = « \sin1sus01\temp\patching\log.txt »

Flush Windows Update Agent cache (keep day’s data)

$Cache = « C:\Windows\SoftwareDistribution\Download\ »

$Daysback = « -1 »

$CurrentDate = Get-Date

$DatetoDelete = $CurrentDate.AddDays($Daysback)

$CacheFile = Get-ChildItem $Cache -Recurse | Where-Object { $_.LastWriteTime -lt $CurrentDate }

$Cachefile | Remove-Item -force -Confirm No

Start patching with kb search

Write-host « Start with $Server » >> $Path

$UpdateSession = New-Object -Com Microsoft.Update.Session

$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()

Write-Host(« Searching for applicable updates… ») -Fore Green >> $Path

$SearchResult = $UpdateSearcher.Search(« IsInstalled=0 and Type=’Software' »)

Write-Host(« List of applicable items on the machine: ») -Fore Green >> $Path

For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){

$Update = $SearchResult.Updates.Item($X)

Write-Host( ($X + 1).ToString() + « >  » + $Update.Title)

}

If ($SearchResult.Updates.Count -eq 0) {

Write-Host(« There are no applicable updates. ») >> $Path

}

Downloading updates

Write-Host(« Creating collection of updates to download: ») -Fore Green >> $Path

$UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl

For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){

$Update = $SearchResult.Updates.Item($X)

#Write-Host( ($X + 1).ToString() + « > Adding:  » + $Update.Title)

$Null = $UpdatesToDownload.Add($Update)

}

Write-Host(« Downloading Updates… ») -Fore Green >> $Path

$Downloader = $UpdateSession.CreateUpdateDownloader()

$Downloader.Updates = $UpdatesToDownload

$Null = $Downloader.Download()

Write-Host(« List of Downloaded Updates… ») -Fore Green >> $Path

$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl

For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){

$Update = $SearchResult.Updates.Item($X)

If ($Update.IsDownloaded) {

Write-Host( ($X + 1).ToString() + « >  » + $Update.Title) >> $Path

$Null = $UpdatesToInstall.Add($Update)

}

}

Installing updates

Write-Host(« Installing Updates… ») -Fore Green >> $Path

$Installer = $UpdateSession.CreateUpdateInstaller()

$Installer.Updates = $UpdatesToInstall

$InstallationResult = $Installer.Install()

Write-Host(« List of Updates Installed with Results: ») -Fore Green >> $Path

For ($X = 0; $X -lt $UpdatesToInstall.Count; $X++){

Write-Host($UpdatesToInstall.Item($X).Title + « :  » + $InstallationResult.GetUpdateResult($X).ResultCode) >> $Path

}

}#scriptBlock

}#ForEach

Laisser un commentaire