-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushping.psm1
73 lines (57 loc) · 2.13 KB
/
pushping.psm1
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
# PoSH module to ping a host and send status notifications to pushover as pushping service
Function pushping
{
param (
[parameter(mandatory=$true)]$ip
#[parameter(mandatory=$true)]$reboot = $(Read-Host -Prompt "Reboot $host ? (y/N)")
)
Send-Pushping "Starting ping on $ip..."
Do
{
$status = Test-Connection $ip -Count 1 -Quiet # sets $status as $true or $false
ping-host $ip $status
} while ($status -eq $true)
Do
{
$status = Test-Connection $ip -Count 1-Quiet # sets $status as $true or $false
ping-host $ip $status
} while ($status -eq $false)
}
Function ping-host($ip, $StartingStatus) # pings $ip and passes $status as $startingstatus.
{
Do
{
$CurrentStatus = Test-Connection $ip -Count 1 -Quiet # sets $currentstatus as $true or $false
Start-Sleep -Milliseconds 500 # wait .5 seconds
} while ($StartingStatus -eq $CurrentStatus)
$status = $CurrentStatus # sets global $status to $currentstatus
if ($CurrentStatus -eq $true) # checks $status variable and changes push notification based on status
{
Send-Pushping "$ip is responding to pings"
} else {
Send-Pushping "$ip is not responding to pings"
}
}
Function Send-Pushping
{
param (
[parameter(Mandatory=$true)][string]$message, # Message to be sent to client
[parameter(Mandatory=$false)][string]$title # optional title used by pushover notification
) # end parameters
$user = '[user token]'
$token = '[application token]'
$uri = 'https://api.pushover.net/1/messages.json' # Pushover API address
$parameters = @{ # Parameters to be sent to Pushover service for notification
token = $token
user = $user
title = $title
message = $message
}
$parameters | Invoke-RestMethod -Uri $uri -Method Post | Out-Null # Post rest method used to push $parameters to Pushover; Out-Null to remove all output
}
if ($status -eq $true) # checks $status variable and changes push notification based on status
{
echo "$ip is responding to pings"
} else {
echo "$ip is not responding to pings"
}