PoshCode Logo PowerShell Code Repository

WSUS production approval by rov3_ 4 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6584"></script>download | new post

Compare updates between WSUS target groups (e.g Test and Production) and approve updates to the latter and email report on changes made.

  1. ## SETTINGS START ##
  2.  
  3. $date = Get-Date
  4. $targetGroup = "Enter the name of your production WSUS target group here"
  5. $updateFilter = "$_.title -notlike '*Server*' -and $_.title -notlike '*sharepoint*'"
  6. $reportSMTP = "your SMTP server"
  7. $reportTitle = "<h1>$env:computername</h1><h3>$updateCount updates approved for $targetGroup on $date.</h3>"
  8. $reportFrom = "address status email comes from"
  9. $reportTo = "address status email goes to"
  10. $reportSubject = "Updates approved for $targetGroup"
  11.  
  12. ## SETTINGS END ##
  13.  
  14. # Formatting for reporting email.
  15.  
  16. $reportHeader = @"
  17.    <link rel="stylesheet" src="https://necolas.github.io/normalize.css/latest/normalize.css">
  18.    <style>
  19.        body {
  20.            color: #222;
  21.            font-family: sans-serif;
  22.            font-size: 14px;
  23.            margin: 2% 0;
  24.        }
  25.        h1 {
  26.            font-size: 2em;
  27.            font-weight: normal;
  28.            padding: 0 2%;
  29.        }
  30.        h3 {
  31.            font-size: 1.25em;
  32.            font-weight: normal;
  33.            padding: 0 2%;
  34.        }
  35.        table {
  36.            border-collapse: collapse;
  37.            width: 100%;
  38.        }
  39.        tr:nth-child(even) {
  40.                         background: #ADD8E6;
  41.                 }
  42.        tr:nth-child(odd) {
  43.             background: #E0FFFF;       
  44.        }
  45.        tr:first-child {
  46.                         background: #EEE;
  47.                 }
  48.        th {
  49.            border-bottom: 1px solid #999;
  50.            font-weight: bold;
  51.            text-align: left;
  52.        }
  53.        td,
  54.        th {
  55.            padding: .25em;
  56.        }
  57.        td:first-child,
  58.        th:first-child    {
  59.            padding-left: 2%;
  60.        }
  61.        td:last-child,
  62.        th:last-child {
  63.            padding-left: 2%;
  64.        }
  65.        h2 {
  66.            font-size: 1.5em;
  67.            font-weight: normal;
  68.            margin: 1 0 .5%;
  69.            padding: 0 2%;
  70.        }
  71.    </style>
  72. "@
  73.  
  74. # Function to find updates applied per group.
  75.  
  76. function Get-WSUSgroupupdates {
  77. Param ($target)
  78.  
  79.     [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
  80.     $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer()
  81.     $updateScope = new-object Microsoft.UpdateServices.Administration.UpdateScope;
  82.     $updateScope.UpdateApprovalActions =[Microsoft.UpdateServices.Administration.UpdateApprovalActions]::Install -bor [Microsoft.UpdateServices.Administration.UpdateApprovalActions]::Uninstall -bor [Microsoft.UpdateServices.Administration.UpdateApprovalActions]:: All -bor [Microsoft.UpdateServices.Administration.UpdateApprovalActions]::NotApproved
  83.      
  84.     $updates = $wsus.GetUpdates($updateScope)
  85.      
  86.     $groups = $wsus.GetComputerTargetGroups() | where {$_.name -eq $target}
  87.     $updateRep = @()
  88.    
  89.     foreach($update in $updates) {
  90.      
  91.         foreach($group in $groups) {
  92.        
  93.         $status = "Install"
  94.      
  95.             if ($update.GetUpdateApprovals($group).Count -ne 0)
  96.             {$status = $update.GetUpdateApprovals($group)[0].Action}
  97.            
  98.             $updateRep += $update
  99.                 #@{Label='Title';Expression={$update.Title}},`
  100.                 #@{Label='Group';Expression={$group.Name}},`
  101.                 #@{Label='Status';Expression={$status}},`
  102.                 #@{Label='guid';Expression={$update.id.updateid}}
  103.             }
  104.         }
  105.  
  106.         $updateRep
  107.     }
  108.  
  109. # Use function to find updates for target groups.
  110.  
  111. $testGroup = Get-WSUSgroupupdates -target "Workstations Test"
  112. $corpGroup = Get-WSUSgroupupdates -target "Production Corporate"
  113.  
  114. # Compare test against target group to find difference.
  115.  
  116. Compare-Object $testGroup.id.updateid $corpGroup.id.updateid | `
  117.     where {$_.SideIndicator -eq '<='} | `
  118.     ForEach-Object  { $findDiff += $_.InputObject }
  119.  
  120. # Approve updates found in test that have not been applied for target group.
  121.  
  122. $allUpdate = $findDiff | where {$updateFilter} | `
  123.     Approve-WsusUpdate -Action Install -TargetGroupName $targetGroup
  124.  
  125. # Get count of updates approved for reporting email sub header.
  126.  
  127. $updateCount = ($allUpdate.update.title).Count | Out-String
  128.  
  129. # Put updates into an HTML table with user friendly column names.
  130.  
  131. $outBody = $allUpdate.Update | select `
  132.     @{Label='Update';Expression={$_.title}},`
  133.     @{Label='Released';Expression={$_.creationdate}},`
  134.     @{Label='Type';Expression={$_.UpdateClassificationTitle}} | `
  135.     ConvertTo-Html -fragment -Property Update,Released,Type -PreContent $reportTitle
  136.  
  137. # Create final HTML with formatting applied and send as email.
  138.  
  139. $outHTM = ConvertTo-Html -Head $reportHeader -Body $outBody
  140.  
  141. Send-MailMessage `
  142.    -SmtpServer $reportSMTP `
  143.    -BodyAsHtml ($outHTM | out-string) `
  144.    -From $reportFrom `
  145.    -To $reportTo `
  146.    -Subject $reportSubject

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me