Created
July 19, 2013 18:57
-
-
Save baskarp/6041508 to your computer and use it in GitHub Desktop.
Import Users from Active Directory into PagerDuty (via the API) Instructions here: http://support.pagerduty.com/entries/23586368-Import-Users-from-Active-Directory-into-PagerDuty-via-the-API-
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
Import-Module ActiveDirectory | |
#Import users via the PD API, echo results | |
function POST_Request ($url,$parameters, $api_key) { | |
$http_request = New-Object -ComObject Msxml2.XMLHTTP | |
$http_request.open('POST', $url, $false) | |
$http_request.setRequestHeader("Content-type", "application/json") | |
$token = "Token token=" + $api_key | |
$http_request.setRequestHeader("Authorization", $token) | |
$http_request.setRequestHeader("Content-length", $parameters.length) | |
$http_request.setRequestHeader("Connection", "close") | |
$http_request.send($parameters) | |
Write-Host "Server Response:" $http_request.statusText | |
} | |
$ad_group = Read-Host "Enter AD Group Name:" | |
#Pull all users from the specified group within Active Directory | |
Get-ADGroup $ad_group | % { | |
$users = "Name,Email`r`n"; | |
$_ | Get-ADGroupMember | % { | |
$user = Get-ADUser $_ -Properties * | |
$users += $user.Name + "," + $user.EmailAddress + "`r`n" | |
} | |
} | |
#Get the authentication information and add each users via POST_Request | |
$subdomain = Read-Host "Enter subdomain" | |
$api_key = Read-Host "Enter API key" | |
$requester_id = Read-Host "Enter Requester ID" | |
$url = "https://" + $subdomain + ".pagerduty.com/api/v1/users" | |
$parameters = New-Object Collections.Specialized.NameValueCollection; | |
$users = ConvertFrom-Csv $users | |
$users | % { | |
Write-Host "Importing user:" $_.Name | |
$parameters = "{`"requester_id`":`"" + $requester_id + "`",`"name`":`"" + $_.Name + "`",`"email`":`"" + $_.Email + "`"}" | |
POST_Request $url $parameters $api_key | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment