Last active
November 9, 2016 16:24
-
-
Save lfepp/ec388dbeb2c2ad301313143f44844fa5 to your computer and use it in GitHub Desktop.
Script to import users from Active Directory into 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
# Copyright (c) 2016, PagerDuty, Inc. <[email protected]> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of PagerDuty Inc nor the | |
# names of its contributors may be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
# 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, $requester_email) { | |
$http_request = New-Object -ComObject Msxml2.XMLHTTP | |
$http_request.open('POST', $url, $false) | |
$http_request.setRequestHeader("Content-type", "application/json") | |
$http_request.setRequestHeader("Accept", "application/vnd.pagerduty+json;version=2") | |
$http_request.setRequestHeader("From", $requester_email) | |
$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 | |
$api_key = Read-Host "Enter API key" | |
$requester_email = Read-Host "Enter your PagerDuty email address here" | |
$url = "https://api.pagerduty.com/users" | |
$parameters = New-Object Collections.Specialized.NameValueCollection; | |
$users = ConvertFrom-Csv $users | |
$users | % { | |
Write-Host "Importing user:" $_.Name | |
$parameters = "{`"user`":{`"name`":`"" + $_.Name + "`",`"email`":`"" + $_.Email + "`"}}" | |
POST_Request $url $parameters $api_key $requester_email | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment