Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanhoskin/4544017 to your computer and use it in GitHub Desktop.
Save ryanhoskin/4544017 to your computer and use it in GitHub Desktop.
Import users from Active Directory to PagerDuty
# Import users from Active Directory to PagerDuty
# Requires Windows Server 2008 R2
# Users should be members of a security group named "pagerduty"
Import-Module activedirectory
# Import users via the PD API
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)
$http_request.statusText
}
# Pull all users from the pagerduty group within Active Directory
Get-ADGroup "pagerduty" | % {
$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 (e.g. <subdomain>.pagerduty.com)"
$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
}
@ryanhoskin
Copy link
Author

I fixed the space issue a while ago. Thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment