-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharePoint-Monitor.ps1
More file actions
155 lines (140 loc) · 5.14 KB
/
Copy pathSharePoint-Monitor.ps1
File metadata and controls
155 lines (140 loc) · 5.14 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function Start-Monitor {
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$ComputerName = $env:COMPUTERNAME,
# reset the lists of hosts prior to looping
$OutageHosts = @(),
# specify the time you want email notifications resent for hosts that are down
$EmailTimeOut = 30,
# specify the time you want to cycle through your host lists.
$SleepTimeOut = 5,
# specify the maximum hosts that can be down before the script is aborted
$MaxOutageCount = 100,
# specify who gets notified
$notificationto = "user@domain.org",
# specify where the notifications come from
$notificationfrom = "admin@domain.org",
# specify the SMTP server
$smtpserver = "relay.domain.org"
)#End Param
# start looping here
Do{
$available = @()
$notavailable = @()
Write-Host (Get-Date)
# Read the File with the Hosts every cycle, this way to can add/remove hosts
# from the list without touching the script/scheduled task,
# also hash/comment (#) out any hosts that are going for maintenance or are down.
$ComputerName | Where-Object {!($_ -match "#")} |
#"test1","test2" | Where-Object {!($_ -match "#")} |
ForEach-Object {
if(Test-Connection -ComputerName $_ -Count 1 -ea silentlycontinue)
{
# if the Host is available then write it to the screen
write-host "Available host ---> "$_ -BackgroundColor Green -ForegroundColor White
[Array]$available += $_
# if the Host was out and is now backonline, remove it from the OutageHosts list
if ($OutageHosts -ne $Null)
{
if ($OutageHosts.ContainsKey($_))
{
$OutageHosts.Remove($_)
}
}
}
else
{
# If the host is unavailable, give a warning to screen
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
if(!(Test-Connection -ComputerName $_ -Count 2 -ea silentlycontinue))
{
# If the host is still unavailable for 4 full pings, write error and send email
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
[Array]$notavailable += $_
if ($OutageHosts -ne $Null)
{
if (!$OutageHosts.ContainsKey($_))
{
# First time down add to the list and send email
Write-Host "$_ Is not in the OutageHosts list, first time down"
$OutageHosts.Add($_,(get-date))
$Now = Get-date
#$Body = "$_ has not responded for 5 pings at $Now"
#Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom `
# -Subject "Host $_ is down" -SmtpServer $smtpserver
}
else
{
# If the host is in the list do nothing for 1 hour and then remove from the list.
Write-Host "$_ Is in the OutageHosts list"
if (((Get-Date) - $OutageHosts.Item($_)).TotalMinutes -gt $EmailTimeOut)
{$OutageHosts.Remove($_)}
}
}
else
{
# First time down create the list and send email
Write-Host "Adding $_ to OutageHosts."
$OutageHosts = @{$_=(get-date)}
#$Body = "$_ has not responded for 5 pings at $Now"
#Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom `
# -Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
}
}
# Report to screen the details
Write-Host "Available count:"$available.count
Write-Host "Not available count:"$notavailable.count
Write-Host "Not available hosts:"
$OutageHosts
Write-Host ""
Write-Host "Sleeping $SleepTimeOut seconds"
sleep $SleepTimeOut
if ($OutageHosts.Count -gt $MaxOutageCount)
{
# If there are more than a certain number of host down in an hour abort the script.
$Exit = $True
$body = $OutageHosts | Out-String
Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom `
-Subject "More than $MaxOutageCount Hosts down, monitoring aborted" -SmtpServer $smtpServer
}
}
while ($Exit -ne $True)
}
function Global:Convert-HString {
#Requires -Version 2.0
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$HString
)#End Param
Begin
{
Write-Verbose "Converting Here-String to Array"
}
Process
{
$HString -split "`n" | ForEach-Object {
$ComputerName = $_.trim()
if ($ComputerName -notmatch "#")
{
$ComputerName
}
}
}
End
{
# Nothing to do here.
}
}
$HS=@"SERVER1, SERVER2, SERVER3, SERVER4"@
Start-Monitor (Convert-HString $HS)