Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## #################################
- ##
- ##
- ## Check servers for which windows updates are installed which have not been superseded.
- ##
- ## Change Log:
- ##
- ## 25.10.2016 - Failed connection servers appear in $Final as n/a
- ## 24.10.2016 - Now works with '2003 Service Pack 2 32/64'
- ##
- ## #################################
- #region Define variables and csvs
- $drive = 'C:\Users\skews\Google Drive\PowerShell\Scripts\WindowsUpdates\csvs'
- ## get list of servers
- $servers = Get-Content "$drive\servers.txt"
- ## import list of all non-superseded updates
- $supers = Import-Csv "$drive\supers.csv"
- $OSes = Import-Csv "$drive\OSes.csv"
- #endregion
- $OSLookup = @{}
- foreach ($OS in $OSes) {
- $OSLookup.Add("$($OS.Version) $($OS.OSArchitecture)",$OS.Name)
- }
- $scriptblock = {
- $OSLookup = $args[1]
- $Server = $args[0]
- if (-not (Test-Connection -ComputerName $server -count 1 -Quiet)){
- return $null
- }
- $ThisOS = Get-WmiObject -Class Win32_OperatingSystem –ComputerName $Server
- if ($ThisOS.version -eq '5.2.3790'){
- $CPU = (Get-WmiObject -Class win32_processor -ComputerName $Server).Addresswidth | Select-Object -First 1
- $ThisCPU = [string]$CPU +"-bit"
- $OSLookup["$($ThisOS.Version) $($ThisCPU)"]
- }
- else {
- $OSLookup["$($ThisOS.Version) $($ThisOS.OSArchitecture)"]
- }
- Get-WmiObject win32_quickfixengineering –ComputerName $Server | Select-Object 'HotfixID' |
- ForEach-Object {
- $_.HotfixID -replace 'KB',''
- }
- }
- $jobs = @()
- foreach ($server in $servers) {
- $running = @( Get-Job | Where-Object { $_.State -eq 'running' })
- if (-not ($running.Count -le 15)) {
- $running | Wait-Job | Out-Null
- }
- $jobs += Start-Job -Name $server -ScriptBlock $scriptblock -Argument $Server,$OSLookup
- Get-Job
- }
- Wait-Job -Job $jobs | Out-Null
- $Final = foreach ($job in $jobs) {
- $Result = Receive-Job -Job $job
- $name = $job.Name
- $Online = 'Yes'
- if(($Result -eq $null) -eq $true){
- $Installed = New-Object psobject
- $Installed | Add-Member -MemberType NoteProperty "Servername" -value $name -Force
- $Installed | Add-Member -MemberType NoteProperty "Online" -value 'No' -Force
- $Installed | Add-Member -MemberType NoteProperty "Total" -value 'na' -Force
- $Installed | Add-Member -MemberType NoteProperty "Missing" -Value 'na' -Force
- $Installed | Add-Member -MemberType NoteProperty "Installed" -Value 'na' -Force
- $Installed | Add-Member -MemberType NoteProperty "%_Installed"-value 'na' -Force
- }
- else{
- ##Get Hotfixes for current job
- $hotfix = $Result | where 'length' -LT 10
- ## Gets OS name from current job
- $os = $result | where 'length' -GT 10 | select -First 1
- $selection = 'ServerName','Date Posted','ComponentKB','Affected Product'
- ## setting header for csv file
- $supers | Add-Member -MemberType NoteProperty "ServerName" -Value $name -Force
- #region Get Total, Instaled and Missing KB by OS version (Affected Product)
- $total = $supers | where { $_.'Affected Product' -eq $os }
- $missing = $supers | where { $_.'Affected Product' -eq $os } | where { $hotfix -notcontains $_.'ComponentKB' } | select $selection
- $missing | Export-Csv "$drive\Missing\Hotfix_missing_$name.csv" -Force
- $Installed = $Supers | where { $_.'Affected Product' -eq $os } | where { $hotfix -contains $_.'ComponentKB' } | select $selection
- $Installed | Export-Csv "$drive\Installed\Hotfix_installed_$name.csv" -Force
- #endregion
- if ($installed.count -ne 0) {
- $percent = ($installed.count/$total.count).ToString("P", $nfi)
- }
- #region Add Properties for $Final
- $Installed | Add-Member -MemberType NoteProperty "Servername" -value $name -Force
- $Installed | Add-Member -MemberType NoteProperty "Online" -value $Online -Force
- $Installed | Add-Member -MemberType NoteProperty "Total" -value $Total.Count -Force
- $Installed | Add-Member -MemberType NoteProperty "Missing" -Value $Missing.Count -Force
- $Installed | Add-Member -MemberType NoteProperty "Installed" -Value $Installed.Count -Force
- $Installed | Add-Member -MemberType NoteProperty "%_Installed"-value $percent -Force
- #endregion
- ## If Test-connection failed create a line of no connection in $Final
- }
- $Properties = @(
- 'ServerName'
- 'Online'
- 'Date Posted'
- 'ComponentKB'
- 'Affected Product'
- 'Total'
- 'Installed'
- 'Missing'
- '%_Installed'
- )
- $Installed | select -First 1 | select $Properties
- }
- $Final | Out-GridView ## Export-Csv "$drive\All_Server_Info.csv"
- ## Cleanup Jobs
- Get-Job | Remove-Job
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement