Create a gist now

Instantly share code, notes, and snippets.

@darrenjrobinson /Import.ps1 Secret
Last active Nov 27, 2017

What would you like to do?
xMatters PowerShell FIM/MIM Management Agent - Import.ps1
PARAM
(
$Username,
$Password,
$Credentials,
$OperationType,
[bool] $usepagedimport,
$pagesize
)
# xMatters Basic AuthN
$site_auth = "$($Username):$($Password)"
$site_Bytes = [System.Text.Encoding]::utf8.GetBytes($site_auth)
$Site_EncodedAuth=[Convert]::ToBase64String($site_Bytes)
# xMatters is using TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$DebugFilePath = "C:\PROGRA~1\MICROS~4\2010\SYNCHR~1\EXTENS~2\xMatters\Debug\xMattersImport.txt"
if(!(Test-Path $DebugFilePath))
{$DebugFile = New-Item -Path $DebugFilePath -ItemType File}
else
{$DebugFile = Get-Item -Path $DebugFilePath}
"Starting Import : " + (Get-Date) | Out-File $DebugFile -Append
# API Timeout (seconds)
[int]$Global:apiTimeout = 15
# URI Variables
$xMattersBaseURI = 'https://customer.hosted.xmatters.com'
$xMattersGetUsersURI = '/api/xm/1/people'
$xMattersGetSitesURI = '/reapi/2015-04-01/sites'
# Create and Modify
$xMattersCreateUsersURI = '/api/xm/1/people'
function GetDevices ($n) {
$Global:return = $null
try{
$Global:return = Invoke-RestMethod -Uri ($xMattersBaseURI + $xMattersCreateUsersURI +"/$($n)/devices") -Headers @{Authorization = "Basic $($Site_EncodedAuth)"} -method GET -ContentType 'application/json;charset=UTF-8' -TimeoutSec $Global:apiTimeout -ErrorAction SilentlyContinue
if ($Global:return){"$($Global:return.count) devices Exist for $($n)" | Out-File $DebugFile -Append}
} catch {
$Global:return = $null
switch ($_.Exception.Response.StatusCode.value__){
"400" {"Devices Check: 400 Bad Request" | Out-File $DebugFile -Append}
"404" {"Devices Check: 404 Not Found" | Out-File $DebugFile -Append}
Default {"Devices Check Failed: $($_.Exception.Response.StatusDescription)" | Out-File $DebugFile -Append}
}
}
}
# GET USERS
$xUsers = @()
[int]$offset = 1000
$xResult = Invoke-RestMethod -Uri ($xMattersBaseURI + $xMattersGetUsersURI +'?limit=1000') -Headers @{Authorization = "Basic $Site_EncodedAuth"} -Method Get -ContentType 'application/json' -TimeoutSec 60 -ErrorAction:Stop
$xUsers += $xResult.data
do {
# next page
$more = Invoke-RestMethod -Uri ($xMattersBaseURI + $xMattersGetUsersURI +"?offset=$($offset)&limit=1000") -Headers @{Authorization = "Basic $Site_EncodedAuth"} -Method Get -ContentType 'application/json' -TimeoutSec 60 -ErrorAction:Stop
$xUsers += $more.data
[int]$offset += 1000
}while ($more.links.next)
# GET SITES
$xSites = @()
# try{
$xResult = Invoke-RestMethod -Uri ($xMattersBaseURI + $xMattersGetSitesURI) -Headers @{Authorization = "Basic $Site_EncodedAuth"} -Method Get -ContentType 'application/json' -TimeoutSec 60 -ErrorAction:Stop
$xSites += $xResult.sites
$xSitesNext = $xResult.nextRecordsURL
do {
# next page
$more = Invoke-RestMethod -Uri ($xMattersBaseURI + $xSitesNext) -Headers @{Authorization = "Basic $Site_EncodedAuth"} -Method Get -ContentType 'application/json' -TimeoutSec 60 -ErrorAction:Stop
$xSites += $more.sites
$xSitesNext = $more.nextRecordsURL
}while ($more.nextRecordsURL)
# Parse USERS
ForEach($user in $xUsers)
{
$obj = @{}
$obj.Add("id",$user.id)
$obj.Add("objectClass", "user")
$obj.Add("targetName", $user.targetName)
$obj.Add("recipientType", $user.recipientType)
$obj.Add("externallyOwned", $user.externallyOwned)
$obj.Add("firstName", $user.firstName)
$obj.Add("lastName", $user.lastName)
$obj.Add("language", $user.language)
$obj.Add("timezone", $user.timezone)
$obj.Add("webLogin", $user.webLogin)
$obj.Add("siteID", $user.site.id)
$obj.Add("siteName", $user.site.name)
$obj.Add("manuallyManaged", $user.properties.'Manually Managed')
$obj.Add("status", $user.status)
# Get Users Devices
GetDevices $user.targetName
if ($return){$blnDevice = $true; $xDevices = $return}else{$blnDevice = $false}
if ($blnDevice){
foreach ($device in $xDevices.data){
switch ($device.name){
"Work Email" {
$obj.Add("email", $device.emailAddress)
$obj.Add("emailID", $device.id)
}
"SMS Phone" {
$obj.Add("workMobile", $device.phoneNumber)
$obj.Add("workMobileID", $device.id)
}
}
}
$obj
} else {
$obj
}
}
# Parse SITES
ForEach($site in $xSites) {
$obj2 = @{}
$obj2.Add("id",$site.identifier)
$obj2.Add("objectClass", "site")
$obj2.Add("name", $site.name)
$obj2.Add("status", $site.status)
$obj2.Add("language", $site.language)
$obj2.Add("timezone", $site.timezone)
$obj2.Add("cluster", $site.cluster)
$obj2.Add("address1", $site.address1)
$obj2.Add("address2", $site.address2)
$obj2.Add("city", $site.city)
$obj2.Add("state", $site.state)
$obj2.Add("country", $site.country)
$obj2.Add("postalCode", $site.postalCode)
$obj2.Add("latitude", $site.latitude)
$obj2.Add("longitude", $site.longitude)
$obj2
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment