Making PUT calls to the RackSpace API

In my inventory database I use the Employee ID from HR as my unique identifier across systems. I’ve already populated Active Directory’s Employee ID fields and have a SQL inventory of that, so to populate RackSpace I just linked up the tables in a SQL query and then send a JSON POST to Rackspace’s REST API. Here is the PowerShell script:

# Created with: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.24
# Created on:   11/25/2013 10:16 AM
# Created by:   Rich Hopkins

Import-Module SQLPS -DisableNameChecking

#region Scope Variables

#Set the customerID and domain
$strDomain = ""

#Get API keys from RackSpace control panel
$userKey = "yourkey"
$secretKey = "yourkey"

#The Powershell UserAgent for HTTP headers
$userAgent = "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) WindowsPowerShell/3.0"
#Fake the IE UserAgent by invoking the PSUserAgent .NET Class
#$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Internetexplorer


#region Begin SQL

#Uncomment $debug to use dev sql database
#$Debug = $true
$SQLServer = "SERVER"
If ($Debug) {$SQLDatabase = "Inventory_Dev"}
Else {$SQLDatabase = "Inventory"}


#region Create REST Header

#Note! Must run the Get-Header function each time a new Invoke-RestMethod is run

#This is a breakdown from:
#using the SignMessage() method there I was able to come up with this
#to create the needed SHA1 hash for the HTTP header

#NOTE! The RackSpace wiki is wrong on the header's name
#"X-Api-User-Signature" need to be "X-Api-Signature"

Function Get-SignatureHash([String] $signatureData) {
    $byteArray = [System.Text.Encoding]::ASCII.GetBytes($signatureData)
    $sha =  New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
    $result = $sha.ComputeHash($byteArray)
    $signature = [System.Convert]::ToBase64String($result)
    return $signature
Function Get-Header {
    $dateTime = (Get-Date).ToString("yyyyMMddHHmmss")
    $signatureData = "$userKey$userAgent$dateTime$secretKey"
    $hashSecret = Get-SignatureHash($signatureData)
    $xApiSignature = "$userKey`:$datetime`:$hashSecret"
    $header = @{"X-Api-Signature"="$xApiSignature"}
    Return $header


$SQLQuery = "SELECT, ADUserAccounts.Employee_ID
FROM            ADUserAccounts INNER JOIN
                         vRSExMailbox ON ADUserAccounts.Mail = vRSExMailbox.replyToAddress
WHERE        (ADUserAccounts.Account_Status = 'Enabled') AND (ADUserAccounts.Employee_Type = 'Consultant' OR
                         ADUserAccounts.Employee_Type = 'Contractor' OR
                         ADUserAccounts.Employee_Type = 'Employee')"
#Write-Host $sqlQuery
$MasterList = Invoke-Sqlcmd -Query $SQLQuery -ServerInstance $SQLServer -Database $SQLDatabase
ForEach ($row in $MasterList) {
    $userId = $
    #Convert the Employee_ID's float data type
    #to a 4 digit string
    $id = $row.Employee_ID.ToString("####")
    If ($id.Length -eq 1) {$id = "000" + $id}
    If ($id.Length -eq 2) {$id = "00" + $id}
    If ($id.Length -eq 3) {$id = "0" + $id}
    #Write-Host $id
    $json = @{
        customID = $id;
    $uri = "$strDomain/ex/mailboxes/$userId"
    $header = Get-Header
    Invoke-RestMethod -Uri $uri -Headers $header -Method "PUT" -ContentType "application/x-www-form-urlencoded" -Body $json | Out-Null
    Start-Sleep -Seconds 2
This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s