-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ps1
106 lines (88 loc) · 3.77 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\mait_log.txt -append
$Date = Get-Date
"Maintenance Starting at " + $Date
$User = 'XXXX'
$Password = (Get-Content "\\gearhead\scripts$\Password.txt" | ConvertTo-SecureString)
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
import-module ActiveDirectory
$ADServers = Get-ADComputer -Filter 'OperatingSystem -like "*windows server*"'
[System.Collections.ArrayList]$WindowsVirtualServers = @()
[System.Collections.ArrayList]$LinuxVirtualServers = @()
[System.Collections.ArrayList]$HyperVServers = @()
ForEach($server in $ADServers) {
if ($server.DNSHostName -like '*gearhead*' -Or $server.DNSHostName -like '*joker*') {
$HyperVServers.add($server)
} else {
$WindowsVirtualServers.add($server)
}
}
#$Date = Get-Date
#"Starting Linux jobs at $Date"
#import-csv \\gearhead\scripts$\linux_servers.csv | foreach {
# Start-Job -ArgumentList $_.server, $_.username, $_.password -FilePath "\\gearhead\scripts$\linux_mait.ps1"
#}
#Get-Job | Receive-Job
#Get-Job | Wait-Job
$Date = Get-Date
"Starting Windows jobs at $Date"
Foreach($server in $WindowsVirtualServers) {
Start-Job -ArgumentList $server -FilePath "\\gearhead\scripts$\windows_mait.ps1"
}
$Date = Get-Date
"Waiting for jobs to complete at $Date"
Get-Job | Receive-Job
Get-Job | Wait-Job
$Date = Get-Date
"Jobs completed at $Date, moving on to physical servers."
Enter-PSSession -ComputerName gearhead -Credential $Cred
ipmo \\gearhead\scripts$\PSWindowsUpdate
"Running Windows Updates on gearhead..."
Get-WUInstall -acceptall -IgnoreReboot -IgnoreUserInput
Exit-PSSession
ipmo \\gearhead\scripts$\PSWindowsUpdate
"Running Windows Updates on joker..."
Get-WUInstall -acceptall -IgnoreReboot -IgnoreUserInput
Foreach($server in $HyperVServer) {
Start-Job -ArgumentList $server -FilePath "\\gearhead\scripts$\vm_snapshot.ps1"
}
$Date = Get-Date
"Rebooting gearhead at " + $Date + " and waiting..."
Restart-Computer -ComputerName gearhead -Wait -Force
$Date = Get-Date
"Rebooting joker and finishing maintenance at " + $Date
Stop-Transcript
Restart-Computer -ComputerName joker -Force
#windows_mait.ps1
param($Server)
$User = 'XXXX'
$File = "\\gearhead\scripts$\Password.txt"
$Password = (Get-Content "\\gearhead\scripts$\Password.txt" | ConvertTo-SecureString)
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
$DNSname = $server.DNSHostName
$PSsession = New-PSSession -ComputerName $DNSname -Credential $Cred
if(-not($PSsession))
{
Write-Warning "$DNSname inaccessible, skipping!"
}
else
{
"Entering PSSession to $DNSname"
ipmo \\gearhead\scripts$\PSWindowsUpdate
"Running Windows Updates on $DNSname"
Get-WUInstall -acceptall -IgnoreReboot -IgnoreUserInput
"Closing remote PSSession on $DNSname"
Remove-PSSession $PSsession
$Date = Get-Date
"Rebooting $DNSname at $Date and waiting..."
Restart-Computer -ComputerName $DNSname -Wait -Force
$Date = Get-Date
"$DNSname came back online at $Date"
}
#vm_snapshot.ps1
param($server)
Get-VM -computername $server.DNSHostName | Where-Object {$_.State -eq "Running"} | Stop-VM
Get-VM -computername $server.DNSHostName | Where-Object {$_.State -eq "Off"} | CheckPoint-VM
Get-VM -computername $server.DNSHostName | Where-Object {$_.State -eq "Off"} | Remove-VMSnapshot (Get-VMSnapshot | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-15)})