Created
July 7, 2014 15:07
-
-
Save henkmeulekamp/e1f288716cc055e0e46e to your computer and use it in GitHub Desktop.
Powershell script to create recurring maintenance windows in PagerDuty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[int]$numberOfDaysForward = 365, | |
[string]$requesterId = "XXXXXXXXx", #pagerduty-userid | |
[string]$apiKey = "XXXXXXXXXXXXXXX", #pagerduty api-key | |
[string]$pagerdutyUrl = "https://XXXXXXX.pagerduty.com", #https://[account-name].pagerduty.com | |
[string]$description = "Daily Blackout Hours 2:30 to 4am", #description of maintenance window | |
[string]$serviceIDs="'XXXXXX', 'XXXXXX', 'XXXXXX'", #serviceIds of services to set in maintenance "'ABC123', 'ABC100', 'ABC101'" | |
[string]$start = "2014-07-07T02:30:00", # "yyyy-MM-ddTHH:mm:ss" | |
[string]$end = "2014-07-07T04:00:00" # "yyyy-MM-ddTHH:mm:ss" | |
) | |
function Execute-HTTPPostCommand() { | |
param( | |
[string] $target = $null | |
) | |
$webRequest = [System.Net.WebRequest]::Create($target) | |
$webRequest.ContentType = "application/json" | |
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post) | |
$webrequest.ContentLength = $PostStr.Length | |
$webRequest.ServicePoint.Expect100Continue = $false | |
$webRequest.Headers.Add("Authorization", "Token token=$apiKey"); | |
$webRequest.Method = "POST" | |
$requestStream = $webRequest.GetRequestStream() | |
$requestStream.Write($PostStr, 0,$PostStr.length) | |
$requestStream.Close() | |
[System.Net.WebResponse] $resp = $webRequest.GetResponse(); | |
$rs = $resp.GetResponseStream(); | |
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs; | |
[string] $results = $sr.ReadToEnd(); | |
return $results; | |
} | |
$currentdt = [datetime]::ParseExact($start, "yyyy-MM-ddTHH:mm:ss", $null) | |
$currentEnddt = [datetime]::ParseExact($end, "yyyy-MM-ddTHH:mm:ss", $null) | |
#loop thorugh amount of days | |
for ($j=1; $j -lt $numberOfDaysForward; $j++) | |
{ | |
$currentdt = $currentdt.AddDays(1); | |
$currentEnddt = $currentEnddt.AddDays(1); | |
$s = $currentdt.ToString("yyyy-MM-ddTHH:mm:sszzzzZ") | |
$e = $currentEnddt.ToString("yyyy-MM-ddTHH:mm:sszzzzZ") | |
Write-Host (" From " + $s + " to " + $e) | |
$post = "{ | |
'maintenance_window': { | |
'start_time': '$s', | |
'end_time': '$e', | |
'description': '$description', | |
'service_ids': [ | |
$serviceIDs | |
] | |
}, | |
'requester_id': '$requesterId' | |
}" | |
Write-Host $post | |
$URL = $pagerdutyUrl + "/api/v1/maintenance_windows" | |
Execute-HTTPPostCommand $URL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am new to GitHub so I have no idea what I am doing, but I forked the script to add a $numberOfDaysToIncrement parameter. This will allow you to create the maintenance window, for example, every Sunday.