-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifySlotStatus.ps1
78 lines (68 loc) · 2.82 KB
/
NotifySlotStatus.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
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[Int16]
$Mode,
[Parameter(Mandatory=$false)]
[String]
$SlotWhitelist
)
function Get-WhitelistedSlots(){
$WhitelistedSlots = [System.Collections.ArrayList]::new()
$_SlotWhitelist = $SlotWhitelist.Split(",")
if($_SlotWhitelist){
foreach ($slot in $SlotInfo){
if($_SlotWhitelist | Where-Object {$_ -eq $slot.id.value.ToString()}){
[Void]$WhitelistedSlots.Add($slot)
}
}
}else{
$WhitelistedSlots = $SlotInfo
}
return ,($WhitelistedSlots)
}
#Define the Send-MailMessage variables to receive notifications
$SendFromEmail = "[email protected]"
$SendFromEmailPassword = "mypassword"
$SendFromCredential = [PSCredential]::new($SendFromEmail, (ConvertTo-SecureString $SendFromEmailPassword -AsPlainText -Force))
$SendToEmail = "[email protected]"
$SmtpServer = "mail.domain.com"
$SmtpPort = 587
#Ensure this module is installed
Import-Module newtonsoft.json
#Ensure this module is in the same directory
Import-Module ".\[email protected]"
#Connect to local instance
$session = Connect-FoldingInstance -addr "127.0.0.1" -port 36330
if($session.Connected -eq $false){
#Break; connection failed
Exit 233
}
#Send slot-info command
Send-Command -session $session -cmd "slot-info"
#Sleep to give time for processing; only banner displayed otherwise
Start-Sleep -Seconds 1
#Receive slot-info command response
[String]$SlotInfoStr = Receive-Response($session)
#Convert slot-info response into object
$SlotInfo = Convert-ResponseToObject($SlotInfoStr)
#Include only the slots provided by the user; if no whitelist, use entire list
$WhitelistedSlots = Get-WhitelistedSlots
#Notify based on the mode user selected
if($Mode -eq 0){
#If no running slots found, send notification
$FilteredSlots = $WhitelistedSlots | Where-Object {$_.status.ToString().ToLower() -eq "running" -or $_.status.ToString().ToLower() -eq "download"}
if($null -eq $FilteredSlots){
$Subject = "No running slots found on " + [Environment]::MachineName
Send-MailMessage -From $SendFromEmail -To $SendToEmail -Subject $Subject -Credential $SendFromCredential -UseSsl -SmtpServer $SmtpServer -Port $SmtpPort
}
}elseif($Mode -eq 1){
#If any idle slots found, send notification
$FilteredSlots = $WhitelistedSlots | Where-Object {$_.status.ToString().ToLower() -eq "paused"}
if($FilteredSlots){
$Subject = "Idle slots detected on " + [Environment]::MachineName
Send-MailMessage -From $SendFromEmail -To $SendToEmail -Subject $Subject -Credential $SendFromCredential -UseSsl -SmtpServer $SmtpServer -Port $SmtpPort
}
}
#Dispose of used resources, disconnect from the instance
Disconnect-FoldingInstance($session)