Created
May 24, 2017 17:48
-
-
Save ctigeek/e9af5b352ea11d1b3af001d000e2d406 to your computer and use it in GitHub Desktop.
Get all domains and alias/alternate domains from CP REST API.
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
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 Start-Throttle() { | |
## You have to throttle REST API requests. | |
Start-Sleep -Seconds 2 | |
} | |
function Get-AllDomains($header, $baseUrl) { | |
$allDomains = New-Object System.Collections.ArrayList | |
$offset = 0 | |
$size = 100 | |
do { | |
if ($offset -gt 0) { Start-Throttle; } | |
$url = "/customers/all/domains?size=$size&offset=$offset" | |
$address = $baseUrl+$url | |
$domains = Invoke-RestMethod -Uri $address -Method Get -Headers $header | |
foreach ($domain in $domains.domains) { $allDomains.Add($domain.name) | Out-Null; } | |
$offset = $offset + $size | |
} while ($domains.domains.Count -eq $size) | |
$allDomains | |
} | |
function Get-AllAlternateAndAliases($listOfDomains, $header, $baseUrl) { | |
$allAlternatesAndAlises = @{} | |
foreach ($domain in $listOfDomains) { | |
Write-Host "Getting alias/alt for $domain." | |
Start-Throttle | |
$url = "/customers/all/domains/$domain/alternatedomains" | |
$address = $baseUrl+$url | |
$altDomains = Invoke-RestMethod -Uri $address -Method Get -Headers $header | |
if ($altDomains.alternateDomains.Count -gt 0) { | |
Write-Host "Found alias/alternate domains for $domain." | |
foreach ($altDomain in $altDomains.alternateDomains) { | |
$allAlternatesAndAlises.Add($altDomain) | |
} | |
} | |
} | |
} | |
$apiKey = "***Put api key here..." | |
$apiSecret = "***Put api secret here..." | |
$userAgent = "query" | |
$baseUrl = "https://api.emailsrvr.com/v1" | |
$signature = Get-XapiSignature $userAgent $apiKey $apiSecret | |
$header = @{ "User-Agent"=$userAgent; "Accept"="application/json"; "X-Api-Signature"=$signature } | |
$listOfDomains = Get-AllDomains $header $baseUrl | |
$listOfAliasAlts = Get-AllAlternateAndAliases $listOfDomains $header $baseUrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment