r/PowerShell • u/happysysadm • Jun 07 '18
WSUS management with PowerShell
http://www.happysysadm.com/2018/06/wsus-management-with-powershell.html5
u/apperrault Jun 07 '18
This is great. Thanks for sharing. Will read through again at work and see if we can tweak it to fit out patching schedule
3
u/happysysadm Jun 07 '18
Cool. Let me know how it goes and if I can improve it somehow.
4
u/apperrault Jun 07 '18
I will. We have our servers broken down in a 4 week period so we sync with MS on Wednesday after patch Tuesday and then approve patches each Wednesday for a different group. If there was a way to make this more automated, that would be great. I will read through this tomorrow and see what I can manipulate
3
u/happysysadm Jun 07 '18
Thats really is the kind of feedback I was expecting. My script can for sure be tweaked to span 4 WSUS computer groups on four different weeks.
It could just be difficult to have all your computers patched before the script by AdamJ does the cleaning, if you use it.
3
u/happysysadm Jun 07 '18
Any improvement to the script is welcome.
13
u/clevertwain Jun 07 '18 edited Jun 07 '18
A few times a year, the first of the month will be a Tuesday. This will cause the fourth Monday to be AFTER the fourth Tuesday. Because of this, you may want to do this:
(Get-Date -Date $patchTuesday).AddDays(13)This will ensure that you always sync two weeks after patch Tuesday.
edit:
This line will give you grief!
$d1 = Get-Date -Day 1 -Month $($now.Month + 1) -Year $now.YearIn December, this line is going to break in one of two ways...
You are going to end up getting January 1st of the current year, rather than next year
$now.Month + 1 will equal 13, which will cause Get-Date to throw an error
Rather than manually checking for any of these, I would just use Get-Date and not specify a year (it will use the current year by default). That will wrap into next year if appropriate. It would probably end up something like this:
$d1 = Get-Date -Day 1 -Month ( (Get-Date).AddMonths(1).Month )2
u/happysysadm Jun 08 '18
Thanks for the improvements: I always appreciate community contributions and especially when they are of high quality.
Thanks again.
1
2
u/Lee_Dailey [grin] Jun 07 '18
howdy happysysadm,
this aint an improvement, but i can't stop myself from pointing out that ...
servers following their criticity.
... the word criticity is not an official word. [grin] i think you meant criticality instead.
thank you for posting this ... it's a right nice read! [grin]
take care,
lee2
u/happysysadm Jun 07 '18
Fixed it, thanks!
2
u/Lee_Dailey [grin] Jun 07 '18
howdy happysysadm,
you are quite welcome, good sir! [grin]
take care,
lee
2
Jun 07 '18
Funny how the AdamJ script goes paid and now everyone is doing something, i love it!
3
u/happysysadm Jun 08 '18
Actually my script is by no means meant to replace AdamJ script. He performs daily, monthly and quartely cleanup. I am trying to automate the patch process in the time window that starts with Patch Tuesday and ends on the 7th of the following month (the day AdamJ scripts kicks by the schedule I choose).
What other scripts are you aware of that apply to this subject? I am more than interested.
2
u/quazywabbit Jun 07 '18
Leaving this here. http://damgoodadmin.com/2017/11/13/patch-tuesday-is-a-lie/
3
u/happysysadm Jun 08 '18
That made an interesting read!
2
u/quazywabbit Jun 11 '18
Thanks. I’ve thought about just pushing for dev to just wait 7 days for any updates and 14 for production and do away with patch Tuesday/Tuesday +offset. I’m having to read all the updates anyways because Microsoft keeps breaking network adapters.
3
u/Beablebeable Jun 07 '18 edited Jun 07 '18
Nice article, thank you.
For fun, here's what I came up with awhile ago to find Patch Tuesday:
function Get-MicrosoftPatchDay
{
[CmdletBinding()]
param([Parameter(Mandatory=$False)] [int] $month = -1)
$now = Get-Date
if ($month -eq -1) { $month = $now.Month }
# BUG: this uses the current minute, hour, second to construct the date.
# keep that in mind for comparison (worse), or set it to a specific time (safer)
$startofmonth = Get-Date -Year $now.Year -Month $month -Day 1
$tempdate = $startofmonth
$tuesdays = 0
if ($tempdate.DayOfWeek -eq "Tuesday") {$tuesdays++}
while ($tuesdays -lt 2)
{
$tempdate = $tempdate.AddDays(1)
if ($tempdate.DayofWeek -eq "Tuesday") { $tuesdays++ }
}
$tempdate
} 4
u/Beablebeable Jun 07 '18
Rewrote this because it was bothering me that the tuesday thing was hardcoded:
# This helper function will find the $nth $day of a certain month. # For instance, the second Tuesday for Microsoft Patch Day # If no month or year are passed in, we use the current month and/or year # Return $null if there is no valid answer (for example the 8th Tuesday will not be in the current month) function Get-NthDayOfMonth { [CmdletBinding()] param( [Parameter(Mandatory=$True)] [string] $dayname, # The Day of Week we will be counting (ex: Monday) [Parameter(Mandatory=$True)] [int] $nthday, # The nth $dayname (ex: 2 means second $dayname) [Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month, # The month we will be using for our constructed date [Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year # The year we will be using for our constructed date ) # TODO: validate $dayname as a valid dayname $numfound = 0 $tempdate = $null # Construct the date object for the month we'll be examining try { $tempdate = Get-Date -Year $year -month $month -Day 1 if ($tempdate.DayOfWeek -eq $dayname) { $numfound++ } while (($numfound -lt $nthday) -and ($tempdate.month -eq $month)) { $tempdate = $tempdate.AddDays(1) if ($tempdate.DayOfWeek -eq $dayname) { $numfound++ } } } catch { Write-Output "Invalid date." } # Verify that we found the right number (prevent going into next month) if ($numfound -ne $nthday) { $tempdate = $null } $tempdate } function Get-MicrosoftPatchDay { [CmdletBinding()] param( [Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month, [Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year ) Get-NthDayOfMonth -dayname "Tuesday" -nthday 2 -month $month -year $year } function Get-HappysysadmPatchDay { [CmdletBinding()] param( [Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month, [Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year ) Get-NthDayOfMonth -dayname "Monday" -nthday 4 -month $month -year $year } Get-NthDayOfMonth -dayname "Monday" -nthday 8 Get-NthDayOfMonth -dayname "Wednesday" -nthday 3 -year 1995 Get-NthDayOfMonth -dayname "Tuesday" -nthday 2 -month 4 Get-NthDayOfMonth -dayname "Friday" -nthday 3 -month 9 -year 2012 Get-MicrosoftPatchDay Get-HappysysadmPatchDay3
u/clevertwain Jun 07 '18
I guess everyone has their own Get-NthWeekDay.... Heres mine
function Get-NthWeekday { [CmdletBinding( )] [Alias()] [OutputType([datetime])] Param ( [Parameter( HelpMessage = 'The ordinal (1st, 2nd, etc.) of the day you would like to get.' )] [ValidateSet("1st", "2nd", "3rd", "4th", "5th", "First", "Second", "Third", "Fourth", "Fifth", "Previous", "Next", "Last", "Current", "1", "2", "3", "4", "5")] [Alias('OccurrenceNumber', 'Number')] [string] $Ordinal = 'Current', [Parameter( HelpMessage = 'The name of the weekday to check for.' )] [ValidateSet("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","All")] [Alias('Day')] [string] $WeekDay = (Get-Date).DayOfWeek, [Parameter( HelpMessage = 'The month to use when finding the particular date.' )] [ValidateSet("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")] [string] $Month, [Parameter( HelpMessage = 'Combined with the month specified when looking for the requested weekday occurrence.' )] [ValidateRange(0, 9999)] [int] $Year ) Begin { $AllDaysInMonth = @() $AllOccurrences = @() $Return = $null if ($WeekDay -eq 'All') { $WeekDay = (Get-Date).DayOfWeek } if ($Ordinal -in ('Previous', 'Next', 'Current') -and ($Month -or $Year)) { Write-Warning -Message "Specific months and/or years are ignored when finding the 'Previous', 'Next', or 'Current' WeekDay" $Month = (Get-Date).Month $Year = (Get-Date).Year } Switch ($Ordinal) { {$_ -eq 1 -or $_ -eq '1st' -or $_ -eq 'First'} { Write-Debug "Ordinal is 1 or 1st or First" $Ordinal = 'first' $OccurrenceNumber = 1 } {$_ -eq 2 -or $_ -eq '2nd' -or $_ -eq 'Second'} { Write-Debug "Ordinal is 2 or 2nd or Second" $Ordinal = 'second' $OccurrenceNumber = 2 } {$_ -eq 3 -or $_ -eq '3rd' -or $_ -eq 'Third'} { Write-Debug "Ordinal is 3 or 3rd or Third" $Ordinal = 'third' $OccurrenceNumber = 3 } {$_ -eq 4 -or $_ -eq '4th' -or $_ -eq 'Fourth'} { Write-Debug "Ordinal is 4 or 4th or Four" $Ordinal = 'fourth' $OccurrenceNumber = 4 } {$_ -eq 5 -or $_ -eq '5th' -or $_ -eq 'Fifth'} { Write-Debug "Ordinal is 5 or 5th or Fifth" $Ordinal = 'fifth' $OccurrenceNumber = 5 } 'Last' { Write-Debug "Ordinal is Last" $OccurrenceNumber = -1 } } Switch ($Month) { {$_ -eq 1 -or $_ -eq 'January'} { $MonthName = 'January' $Month = 1 } {$_ -eq 2 -or $_ -eq 'February'} { $MonthName = 'February' $Month = 2 } {$_ -eq 3 -or $_ -eq 'March'} { $MonthName = 'March' $Month = 3 } {$_ -eq 4 -or $_ -eq 'April'} { $MonthName = 'April' $Month = 4 } {$_ -eq 5 -or $_ -eq 'May'} { $MonthName = 'May' $Month = 5 } {$_ -eq 6 -or $_ -eq 'June'} { $MonthName = 'June' $Month = 6 } {$_ -eq 7 -or $_ -eq 'July'} { $MonthName = 'July' $Month = 7 } {$_ -eq 8 -or $_ -eq 'August'} { $MonthName = 'August' $Month = 8 } {$_ -eq 9 -or $_ -eq 'September'} { $MonthName = 'September' $Month = 9 } {$_ -eq 10 -or $_ -eq 'October'} { $MonthName = 'October' $Month = 10 } {$_ -eq 11 -or $_ -eq 'November'} { $MonthName = 'November' $Month = 11 } {$_ -eq 12 -or $_ -eq 'December'} { $MonthName = 'December' $Month = 12 } default { $MonthName = Get-Date -Format MMMM $Month = (Get-Date).Month } } if (!($Year)) { $Year = (Get-Date).Year } Write-Debug "Ordinal was set to $Ordinal" Write-Debug "WeekDay was set to $WeekDay" Write-Debug "MonthName was set to $MonthName" Write-Debug "Month was set to $Month" Write-Debug "Year was set to $Year" switch ($Ordinal) { 'Previous' { Write-Verbose "Finding Previous $WeekDay" $Counter = -8 } 'Current' { Write-Verbose "Finding $WeekDay" $Counter = -1 } 'Next' { Write-Verbose "Finding the next $WeekDay starting from $((Get-Date).Month)/$((Get-Date).Day)/$((Get-Date).Year)" $Counter = 0 } default { $Date = Get-Date -Year $Year -Month $Month -Day 1 While ($Date.Month -eq $Month) { $AllDaysInMonth += [pscustomobject]@{ Date = $Date.Date DayOfWeek = $Date.DayOfWeek } $Date = $Date.AddDays(1) } $AllOccurrences = $AllDaysInMonth | Where-Object {$_.DayOfWeek -eq $WeekDay} Write-Verbose -Message "There are $($AllOccurrences.Count) occurrences of $WeekDay in $MonthName of $Year. Selecting the $Ordinal one." } } if ($AllOccurrences) { Write-Debug "Choosing from $($AllOccurrences.Date)" if ($OccurrenceNumber -eq -1) { $Return = $AllOccurrences | Select-Object -Last 1 } else { $Return = $AllOccurrences | Select-Object -First 1 -Skip ($OccurrenceNumber - 1) } } else { While ($Date.DayOfWeek -ne $WeekDay) { ++$Counter $Date = (Get-Date).AddDays($Counter) $Return = [pscustomobject]@{ Date = $Date DayOfWeek = $Date.DayOfWeek } } } } End { if (! $Return) { Write-Verbose "There is no $Ordinal $WeekDay in $MonthName, $Year" } else { Get-Date -Date $Return.Date -DisplayHint Date } } }2
3
u/Siddd- Jun 07 '18 edited Jun 07 '18
Great initiative. My colleague started a WSUS script last year, that I finetuned further recently.
If I read your script correctly it retrieves all updates a few times that have status failed/needed on them and approves it for all groups in the groups array, that is also what her version did. I found a way to cycle through all updates and query each update against the group being handled to see if any device in the corresponding group reports that it needs it, if a device is found, the update is approved. If no device reports it, the update is skipped for approval. I also added a parameter to decline preview updates cause they break shit anyway. All actions are logged for our reference afterwards.
Some groups go unattended and some servers I approve and install manually, that’s how my customer wants it. However WSUS is rather unclear about which devices haven’t got the updates you just approved, Creating a custom view with “approved for group X” gives a lot of unnecessary results, so using my script I was able to write a little XAML bases app around it to show updates that I might want to approve manually for another group. It only shows the updates for devices that really need them, nothing else.
If you want I can share the code, it’s now on my private Gitlab.
8
u/alexjitsu Jun 07 '18
Oh my lord,
Thank you for sharing this ....