-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatchTuesdayReboot.ps1
executable file
·92 lines (68 loc) · 2.44 KB
/
PatchTuesdayReboot.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Import-Module activedirectory
$hostname = (Get-ChildItem Env:\COMPUTERNAME).value
$name = (Get-ADComputer -Identity "$hostname" -Properties *).dnshostname
$FSMOCheck=Get-ADDomain | select infrastructuremaster, ridmaster, pdcemulator
$PDC=$FSMOCheck.pdcemulator
$RID=$FSMOCheck.ridmaster
$Infra=$FSMOCheck.infrastructuremaster
If (!(($PDC -eq $name) -or ($RID -eq $name) -or ($Infra -eq $name)))
{
$PresentDate=Get-date
$PresentDayOfMonth=($PresentDate).Day
$FirstDateOfMonth=($PresentDate).AddDays(-$PresentDayOfMonth + 1)
$FirstDayOfMonth=(($PresentDate).AddDays(-$PresentDayOfMonth + 1)).DayOfWeek
If ($FirstDayOfMonth -eq "Sunday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(+2)}
Elseif ($FirstDayOfMonth -eq "Monday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(+1)}
Elseif ($FirstDayOfMonth -eq "Tuesday")
{$FirstTuesday=$FirstDateOfMonth}
Elseif ($FirstDayOfMonth -eq "Wednesday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(-1)}
Elseif ($FirstDayOfMonth -eq "Thursday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(-2)}
Elseif ($FirstDayOfMonth -eq "Friday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(-3)}
Elseif ($FirstDayOfMonth -eq "Saturday")
{$FirstTuesday=($FirstDateOfMonth).AddDays(-4)}
$SecondTuesday = $FirstTuesday.AddDays(7)
$ThirdTuesday = $FirstTuesday.AddDays(14)
$FourthTuesday = $FirstTuesday.AddDays(21)
$FifthTuesday = $FirstTuesday.AddDays(28)
If ($PresentDate -lt $SecondTuesday)
{
exit
}
Elseif (($PresentDate -gt $SecondTuesday) -and ($PresentDate -lt $ThirdTuesday))
{
Restart-Computer -Force
}
Elseif (($PresentDate -gt $ThirdTuesday) -and ($PresentDate -lt $FourthTuesday))
{
$result=Test-path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
If ($result -eq $true)
{
Restart-Computer -Force
}
else
{
exit
}
}
Elseif (($PresentDate -gt $FourthTuesday) -and ($PresentDate -lt $FifthTuesday))
{
$result=Test-path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
If ($result -eq $true)
{
Restart-Computer -Force
}
else
{
Exit
}
}
}
else
{
exit
}