Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all Hex and Rackspace mailboxes for your account, and any indirect accounts.
## This code released under the MIT open source license.
## See here for details: https://opensource.org/licenses/MIT
## This code is provided with NO SUPPORT and NO WARRANTY and NO GUARANTEE. Use at your own risk.
[string] $ApiKey = ""
[string] $ApiSecret = ""
## only populate $indirectAccountNumber if you want a list from one specific account, otherwise leave blank.
[string] $indirectAccountNumber = ""
[string] $path = "AllMailboxes.csv"
[string]$UserAgent = $account
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
function Start-Throttle() {
Start-Sleep -Seconds 1
}
function Get-XapiSignature($userAgent, $apiKey, $apiSecret) {
$dateTime = [DateTime]::UtcNow.ToString("yyyyMMddHHmmss");
$dataToSign = [String]::Format("{0}{1}{2}{3}", $apikey, $userAgent, $dateTime, $apiSecret);
$sha1 = [System.Security.Cryptography.SHA1]::Create();
$signedBytes = $sha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($dataToSign));
$signature = [System.Convert]::ToBase64String($signedBytes);
$sha1.Dispose()
[String]::Format("{0}:{1}:{2}", $apikey, $dateTime, $signature);
}
function Get-AllDomains($account) {
$allDomains = @()
$offset = 0
do {
$url = "https://api.emailsrvr.com/v1/customers/all/Domains?size=250&offset=$offset"
if ($account) {
$url = "https://api.emailsrvr.com/v1/customers/$account/Domains?size=250&offset=$offset"
}
Write-Host $url
$domains = Invoke-RestMethod $url -Headers $headers -Method Get -UseBasicParsing
$allDomains = $allDomains + $domains.domains
$offset = $offset + 250
Start-Throttle
} while($allDomains.Count -lt $domains.total)
$allDomains
}
function Get-AllRseMailboxes($domainName) {
$allMailboxes = @()
$offset = 0
do {
$url = "https://api.emailsrvr.com/v1/customers/all/Domains/$domainName/rs/mailboxes?size=250&offset=$offset"
Write-Host $url
$mailboxes = Invoke-RestMethod $url -Headers $headers -Method Get -UseBasicParsing
$allMailboxes = $allMailboxes + $mailboxes.rsMailboxes
$offset = $offset + 250
Start-Throttle
} while ($allMailboxes.Count -lt $mailboxes.total)
$allMailboxes
}
function Get-AllHexMailboxes($domainName) {
$allMailboxes = @()
$offset = 0
do {
$url = "https://api.emailsrvr.com/v1/customers/all/Domains/$domainName/ex/mailboxes?size=250&offset=$offset"
Write-Host $url
$mailboxes = Invoke-RestMethod $url -Headers $headers -Method Get -UseBasicParsing
$allMailboxes = $allMailboxes + $mailboxes.mailboxes
$offset = $offset + 250
Start-Throttle
} while ($allMailboxes.Count -lt $mailboxes.total)
$allMailboxes
}
$signature = Get-XapiSignature $userAgent $apiKey $apiSecret
$headers = @{
"Content-Type" = "application/x-www-form-urlencoded";
"Accept" = "application/json";
"User-Agent" = $userAgent;
"X-Api-Signature" = $signature
}
$allMailboxes = @()
$domains = Get-AllDomains $indirectAccountNumber
foreach ($domain in $domains) {
Write-Host $domain.name" "$domain.serviceType" "$domain.accountNumber
if ($domain.serviceType -ne "rsemail" ) {
$mailboxes = $null
$mailboxes = Get-AllHexMailboxes $domain.name
$allMailboxes = $allMailboxes + ( $mailboxes | % { New-Object –TypeName PSObject –Prop (@{"account"=$domain.accountNumber; "domain"=$domain.name; "type"="hex"; "mailbox"= $_.name; }) } )
}
if ($domain.serviceType -ne "hex") {
$mailboxes = $null
$mailboxes = Get-AllRseMailboxes $domain.name
$allMailboxes = $allMailboxes + ( $mailboxes | % { New-Object –TypeName PSObject –Prop (@{"account"=$domain.accountNumber; "domain"=$domain.name; "type"="rsemail"; "mailbox"= $_.name; }) } )
}
}
$allMailboxes | Export-Csv -Path $path -Encoding UTF8 -NoTypeInformation -Force
Write-Host "I found $($allMailboxes.Count) mailboxes! All mailboxes exported to $path. "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.