-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart-OpsViewMaint.ps1
58 lines (41 loc) · 1.35 KB
/
Start-OpsViewMaint.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
# Start-OpsViewMaint.ps1
# v0.2
# T.J. Gohl
Param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
# [ValidateScript({Resolve-DNSName $_})]
[String[]]
$Hostname,
[parameter(Mandatory=$false)]
[Int]
$MaintMinutes = 60,
[parameter(Mandatory=$false)]
[String]
$Comment = "Scripted REST Maintenance"
)
Begin {
$opsurl = 'https://opsviewurl.company.com/rest'
$username = 'opsviewaccount'
$login = @{
'username' = $username
'password' = 'your_password'
}
$MaintMinutesString = $MaintMinutes -as [string]
$MaintMinutesString = $MaintMinutesString.Insert(0,"+") + "m"
}
process {
foreach ($opsviewhost in $hostname) {
$token = Invoke-RestMethod -Method Post -Uri "$opsurl/login" -body $login
$downtimebody = @{
starttime = "now"
endtime = $MaintMinutesString
comment = $Comment
}
$downtimebody = $downtimebody | convertto-json -compress
$result = Invoke-RestMethod -Method Post -Uri "$opsurl/downtime?host=$opsviewhost" -Headers @{"X-Opsview-Username"="$username";"X-Opsview-Token"="$($token.token)"} -body $downtimebody -contentType "application/json"
if ($result.summary.num_hosts -gt 0) {
write-host "$($result.list.hosts.hostname) successfully placed into maintenance mode for $MaintMinutes minute(s)!" -foregroundcolor "green"
}
}
}