-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManditoryCOReboot.ps1
86 lines (65 loc) · 6.64 KB
/
ManditoryCOReboot.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
#Starts logging
$d = (get-date -format MMddyyy_HHmmtt) #Gets the date and specifies the format
Start-Transcript -path c:\scripts\log\COManditoryReboot-$d.txt -append #Starts a new transcript
$Servers = (Get-Content '\\userdata\isshare$\Citrix Scripts\Reference Files\Test.txt')
Write-Host $Servers
#Remove-Item c:\servertempco.txt
#Write-Host Querying user sessions on $Servername
Add-PSSnapin Citrix.XenApp.Commands
#15 Minute Warning
ForEach ($Server in $Servers)
{
$QUERY = Get-XASession -ServerName $Server | where { $_.AccountName -like 'corpdomain*'}
ForEach($Session in $QUERY)
#write-host $Session
{
IF ($Session.sessionid -ne $null)
{Send-XASessionMessage -ServerName $Server -MessageTitle "ATTENTION!" `
-MessageBody "A Critical Problem has been detected with this Server, it Will Reboot in 15 Minutes, Please save your work Immediately." `
-SessionId $Session.SessionId -MessageBoxIcon "error" -ErrorAction "SilentlyContinue"}
}
}
#10 Minute Warning
Start-Sleep -Seconds 5
ForEach ($Server in $Servers)
{
$QUERY = Get-XASession -ServerName $Server | where { $_.AccountName -like 'corpdomain*'}
ForEach($Session in $QUERY)
#write-host $Session
{
IF ($Session.sessionid -ne $null)
{Send-XASessionMessage -ServerName $Server -MessageTitle "ATTENTION!" `
-MessageBody "A Critical Problem has been detected with this Server, it Will Reboot in 10 Minutes, Please save your work Immediately." `
-SessionId $Session.SessionId -MessageBoxIcon "error" -ErrorAction "SilentlyContinue"}
}
}
#Write-Host Second Warning Sent
Start-Sleep -Seconds 5
ForEach ($Server in $Servers)
{
$QUERY = Get-XASession -ServerName $Server | where { $_.AccountName -like 'corpdomain*'}
ForEach($Session in $QUERY)
#write-host $Session
{
IF ($Session.sessionid -ne $null)
{Send-XASessionMessage -ServerName $Server -MessageTitle "ATTENTION! - FINAL WARNING!!!" `
-MessageBody "A Critical Problem has been detected with this Server, it Will Reboot in 5 Minutes, Please save your work Immediately. This is the Final Warning" `
-SessionId $Session.SessionId -MessageBoxIcon "error" -ErrorAction "SilentlyContinue"}
}
}
#$ErrorActionPreference="SilentlyContinue" #Set default ErrorAction behavior
$pass = Get-Content c:\credential.txt | ConvertTo-SecureString #Gets credentials as secure string
$credential = New-Object System.Management.Automation.PSCredential("corpdomain\winservice",$pass) #Creates the object to pass credentials
$credential
Write-Host $d Loading XenServer PS SnapIn... $Server
Add-PSSnapin XenServerPSSnapIn -ErrorAction "SilentlyContinue"
Write-Host $d Connecting to Xen Farm...
Connect-XenServer -Creds $credential -Server coxen2 -nowarncertificates
Write-Host Testing Connection...
Get-XenServer:VM | where { $_.name_label -like $Server } | select name_label
foreach($Server in $Servers)
{
Write-Host $d Rebooting Server - $Server
Invoke-XenServer:VM_appliance.HardReboot –VM $Server
}
Stop-Transcript