You are here

Schedule downtime using Powershell through the REST API

5 posts / 0 new
Last post
fajasan's picture
Schedule downtime using Powershell through the REST API


I'm trying to create a Powershell script for scheduling downtime for specific hosts through the REST API. But I cannot get it to work.

My script is below. I think the issue is with the parameters and the way I specify what host to schedule the downtime for.

I hope someone can point me in the right direction?



##### opsview-host-scheduledowntime.ps1
##### Get OPSview Host Config

$urlauth = ""
$urlconfig = ""


$creds = '{"username":"' + $user + '","password":"' + $pass + '"}'

###Generate Auth Token

$bytes1 = [System.Text.Encoding]::ASCII.GetBytes($creds)
$web1 = [System.Net.WebRequest]::Create($urlauth)
$web1.Method = "POST"
$web1.ContentLength = $bytes1.Length
$web1.ContentType = "application/json"
$stream1 = $web1.GetRequestStream()

$reader1 = New-Object System.IO.Streamreader -ArgumentList $web1.GetResponse().GetResponseStream()
$token1 = $reader1.ReadToEnd()

#Parse Token for follwoing sessions
$token1=$token1.Replace("{`"token`":`"", "")
$token1=$token1.Replace("`"}", "")

$hostname = 'SP-16010'
$hostdata = '{"hst.hostname":"' + $hostname + '","starttime":"now","endtime":"+2h","comment":"Maintenance"}'

#Write-Host $hostdata

$bytes2 = [System.Text.Encoding]::ASCII.GetBytes($hostdata)
$web2 = [System.Net.WebRequest]::Create($urlconfig)
$web2.Method = "POST"
$web2.ContentLength = $bytes2.Length
$web2.ContentType = "application/json"
$stream2 = $web2.GetRequestStream()

$reader2 = New-Object System.IO.Streamreader -ArgumentList $web2.GetResponse().GetResponseStream()
$output2 = $reader2.ReadToEnd()

Write-Host $output2's picture
Same problem here

Hi fajasan,

I'm wokring on the same, did you get this working?


feytullahede's picture
Here is the solution

Hi Daniel,

I saw your message when i'm looking for active topics. I hope you're looking for solution yet? I tried many different ways for the solution and solved it!

If i don't misunderstand, problem is in hostdata. "hst.hostname" parameter should not be included in hostdata. Because it's a filtering parameter not an attribute for the downtime 

Actually i'm not so good to understand REST API calls and i don't know powershell at all as a linux administrator :)

I excluded "hst.hostname" in "hostdata" and added "params" variable "urlconfig" as follows.

So i succeded to schedule a downtime for a host :) I really enjoyed to solve an issue. 


hostname = "nagios"

#$params = "?host=$hostname"     OR     $params = "?hst.hostname=$hostname"

$params = "?hst.hostname=$hostname"
#$hostdata = '{"hst.hostname":"' + $hostname + '","starttime":"now","endtime":"+2h","comment":"Maintenance"}'
$hostdata = '{"starttime":"now","endtime":"+2h","comment":"Maintenance"}'  

$bytes2 = [System.Text.Encoding]::ASCII.GetBytes($hostdata)
$web2 = [System.Net.WebRequest]::Create("$urlconfig$params")



Feytullah Ede's picture

Hi Feytullah,

this works like a charm. Many many thanks!!


feytullahede's picture

Hi Daniel,

I'm so glad to hear from you :)