r/PowerShell • • Jun 07 '18

WSUS management with PowerShell

http://www.happysysadm.com/2018/06/wsus-management-with-powershell.html
161 Upvotes

24 comments sorted by

View all comments

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.Year

In December, this line is going to break in one of two ways...

  1. You are going to end up getting January 1st of the current year, rather than next year

  2. $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

u/happysysadm Jun 11 '18

Updated the blog post with a mention to your contributions. Thanks mate.

2

u/clevertwain Jun 11 '18

Aww shucks... Thank you!