in this video we use powershell to find the second tuesday of the month
patching windows servers is important but automating updates can be difficult
due to patching releases on the second tuesday of the month
patch tuesday falls on a different date every month
in this video we walk through code that identifies the second tuesday of the
month and show a couple other ways it can be
used before that please like subscribe share
email a friend and call your mom if you're able
don't forget to click the bell icon to get notifications of new content
i saw a comment on twitter about the dilemma of scheduling and automating
around patch tuesday and remember that i'd found a way to identify the second
tuesday of the month in powershell on a project i worked on a while ago i
reformatted that code as a function that will return the second tuesday of the
current month after that i modified it so you could
pass other parameters to find other day instances in a month say the
fifth wednesday or the second monday of the month let's
just start by taking a look at the code here i am in powershell this is intended
to be a function used as part of other automation for
example putting a vm in maintenance mode during patching
or forcing a reboot on a specific day say the first saturday after patch
tuesday i gave it the name get patch tuesday but
you can change that to anything if you'd like
i've got some descriptions and an overview and some of the outputs
let's go down to the actual execution i have two parameters here weekday this
is the day of the week that we're looking for
so saturday sunday monday tuesday wednesday thursday or friday
and then fine nth day that's the instance of that weekday this will find
the occurrence specified in find nth day of the weekday specified
the default value for example is the second tuesday
no parameters need to be passed to find patch tuesday or the second tuesday
of the month the logic of this function is not limited to the second tuesday
you could specify the third sunday or the fifth wednesday
for that you can pass those parameters in
after parameters there's code that gets the current date it pulls out the month
in the year and finds the start of that month
after that it goes through each day starting with the first to find the
first occurrence of the day specified in the
weekday parameter that gets assigned to the first weekday
variable
next it does a calculation to find the number of days
to offset based on the instance that we've returned
once it finds the number of days to offset you'll add that to the first
weekday that will return patch tuesday or the
second tuesday of the month unless you pass parameters through then
it's going to be whatever day you specify
when i wrote this originally it was only for patch tuesday
so it is a little misleading that i'm calling a patch tuesday but it could
actually be like the third wednesday uh you can change that if you'd like
next i'm going to select this function and i'll hit f8 to run the
code that's just going to add it to memory
although it's in a script file it's actually a function
nothing calls it within the script so it won't return anything
so i'm just running it more interactively this way so let me clear
the screen if i run the function without any
parameters
there it is it says patch tuesday for september of 2020
is september 8th september 8th which happens to be today
is the second tuesday of the month
i've got some other examples up here i'm going to run through
let's copy this next example
we'll just paste it in here to run so what we're doing is getting today's date
and then we're just looking at the day of the week
so that would be the eighth and then we're testing it to see if
the day we return from get patch tuesday is the same day
and that returned true because today is the eighth
patch tuesday the next line of code we're going to get patch tuesday and
we'll add five days
so that just took patch tuesday which is today and added five days
this would be helpful say if you want to set a schedule to reboot
a server five days after patch tuesday that happens to be on a sunday so with
this type of logic you could have a server's reboot on a specific
weekend evening for the next example
we're going to use the same get patch tuesday but this time we're not
looking actually for a tuesday we're looking for
the weekday of wednesday and we're going to find the third occurrence of that
so the third wednesday in september 2020 is on the 16th that is correct
i'm using an advanced function with this so we don't even need to put in
the parameters we can use positional
parameters
there it is the fourth friday in september is september 25th
one thing to watch out for is we can run that and add the fifth friday
in september which is going to be actually october
2nd so that's actually the first friday in october
that's something to be aware of and if it's going to be a problem you may need
to add some additional validation and to make sure that this doesn't happen
this is a good starting point for any automation that takes place
around patch tuesday it allows you to identify what day of the month
patch tuesday falls on and then you can build out schedules from there
i hope you found this code that finds a specific day of the month helpful
if so please like subscribe and share thanks for watching