Advertisement
Guest User

Supers_v11

a guest
Oct 25th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. ## #################################
  2. ##
  3. ##
  4. ## Check servers for which windows updates are installed which have not been superseded.
  5. ##
  6. ## Change Log:
  7. ##
  8. ## 25.10.2016 - Failed connection servers appear in $Final as n/a
  9. ## 24.10.2016 - Now works with '2003 Service Pack 2 32/64'
  10. ##
  11. ## #################################
  12.  
  13. #region Define variables and csvs
  14. $drive = 'C:\Users\skews\Google Drive\PowerShell\Scripts\WindowsUpdates\csvs'
  15.  
  16. ## get list of servers
  17. $servers = Get-Content "$drive\servers.txt"
  18.  
  19. ## import list of all non-superseded updates
  20. $supers = Import-Csv "$drive\supers.csv"
  21. $OSes = Import-Csv "$drive\OSes.csv"
  22. #endregion
  23. $OSLookup = @{}
  24. foreach ($OS in $OSes) {
  25. $OSLookup.Add("$($OS.Version) $($OS.OSArchitecture)",$OS.Name)
  26. }
  27.  
  28.  
  29. $scriptblock = {
  30. $OSLookup = $args[1]
  31. $Server = $args[0]
  32. if (-not (Test-Connection -ComputerName $server -count 1 -Quiet)){
  33. return $null
  34. }
  35. $ThisOS = Get-WmiObject -Class Win32_OperatingSystem –ComputerName $Server
  36. if ($ThisOS.version -eq '5.2.3790'){
  37. $CPU = (Get-WmiObject -Class win32_processor -ComputerName $Server).Addresswidth | Select-Object -First 1
  38. $ThisCPU = [string]$CPU +"-bit"
  39. $OSLookup["$($ThisOS.Version) $($ThisCPU)"]
  40. }
  41. else {
  42. $OSLookup["$($ThisOS.Version) $($ThisOS.OSArchitecture)"]
  43. }
  44. Get-WmiObject win32_quickfixengineering –ComputerName $Server | Select-Object 'HotfixID' |
  45. ForEach-Object {
  46. $_.HotfixID -replace 'KB',''
  47. }
  48. }
  49.  
  50.  
  51. $jobs = @()
  52. foreach ($server in $servers) {
  53. $running = @( Get-Job | Where-Object { $_.State -eq 'running' })
  54. if (-not ($running.Count -le 15)) {
  55. $running | Wait-Job | Out-Null
  56. }
  57. $jobs += Start-Job -Name $server -ScriptBlock $scriptblock -Argument $Server,$OSLookup
  58. Get-Job
  59. }
  60.  
  61. Wait-Job -Job $jobs | Out-Null
  62.  
  63.  
  64. $Final = foreach ($job in $jobs) {
  65.  
  66. $Result = Receive-Job -Job $job
  67. $name = $job.Name
  68. $Online = 'Yes'
  69.  
  70. if(($Result -eq $null) -eq $true){
  71.  
  72. $Installed = New-Object psobject
  73. $Installed | Add-Member -MemberType NoteProperty "Servername" -value $name -Force
  74. $Installed | Add-Member -MemberType NoteProperty "Online" -value 'No' -Force
  75. $Installed | Add-Member -MemberType NoteProperty "Total" -value 'na' -Force
  76. $Installed | Add-Member -MemberType NoteProperty "Missing" -Value 'na' -Force
  77. $Installed | Add-Member -MemberType NoteProperty "Installed" -Value 'na' -Force
  78. $Installed | Add-Member -MemberType NoteProperty "%_Installed"-value 'na' -Force
  79.  
  80. }
  81. else{
  82.  
  83.  
  84. ##Get Hotfixes for current job
  85. $hotfix = $Result | where 'length' -LT 10
  86. ## Gets OS name from current job
  87. $os = $result | where 'length' -GT 10 | select -First 1
  88.  
  89. $selection = 'ServerName','Date Posted','ComponentKB','Affected Product'
  90.  
  91. ## setting header for csv file
  92. $supers | Add-Member -MemberType NoteProperty "ServerName" -Value $name -Force
  93.  
  94. #region Get Total, Instaled and Missing KB by OS version (Affected Product)
  95.  
  96. $total = $supers | where { $_.'Affected Product' -eq $os }
  97. $missing = $supers | where { $_.'Affected Product' -eq $os } | where { $hotfix -notcontains $_.'ComponentKB' } | select $selection
  98. $missing | Export-Csv "$drive\Missing\Hotfix_missing_$name.csv" -Force
  99. $Installed = $Supers | where { $_.'Affected Product' -eq $os } | where { $hotfix -contains $_.'ComponentKB' } | select $selection
  100. $Installed | Export-Csv "$drive\Installed\Hotfix_installed_$name.csv" -Force
  101.  
  102. #endregion
  103.  
  104. if ($installed.count -ne 0) {
  105. $percent = ($installed.count/$total.count).ToString("P", $nfi)
  106. }
  107.  
  108. #region Add Properties for $Final
  109.  
  110. $Installed | Add-Member -MemberType NoteProperty "Servername" -value $name -Force
  111. $Installed | Add-Member -MemberType NoteProperty "Online" -value $Online -Force
  112. $Installed | Add-Member -MemberType NoteProperty "Total" -value $Total.Count -Force
  113. $Installed | Add-Member -MemberType NoteProperty "Missing" -Value $Missing.Count -Force
  114. $Installed | Add-Member -MemberType NoteProperty "Installed" -Value $Installed.Count -Force
  115. $Installed | Add-Member -MemberType NoteProperty "%_Installed"-value $percent -Force
  116. #endregion
  117.  
  118.  
  119. ## If Test-connection failed create a line of no connection in $Final
  120.  
  121. }
  122. $Properties = @(
  123. 'ServerName'
  124. 'Online'
  125. 'Date Posted'
  126. 'ComponentKB'
  127. 'Affected Product'
  128. 'Total'
  129. 'Installed'
  130. 'Missing'
  131. '%_Installed'
  132. )
  133. $Installed | select -First 1 | select $Properties
  134.  
  135. }
  136.  
  137. $Final | Out-GridView ## Export-Csv "$drive\All_Server_Info.csv"
  138.  
  139. ## Cleanup Jobs
  140. Get-Job | Remove-Job
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement