Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Created January 31, 2018 14:56
Show Gist options
  • Save jhochwald/9b70561e044a7a15040d8ba51b0faacf to your computer and use it in GitHub Desktop.
Save jhochwald/9b70561e044a7a15040d8ba51b0faacf to your computer and use it in GitHub Desktop.
Check if a given Name is availible as Office365/Azure Tenant Name and optional return the Tenant ID if the Tenant exists.
function Get-MicrosoftCloudTenantInfo
{
<#
.SYNOPSIS
Check if a given Name is availible as Office365/Azure Tenant Name
.DESCRIPTION
Check if a given Name is availible as Office365/Azure Tenant Name and optional return the Tenant ID if the Tenant exists.
.PARAMETER name
Check if a given Name is availible as Office365/Azure Tenant Name
.PARAMETER id
Get the Tenant ID
.EXAMPLE
PS C:\> Get-MicrosoftCloudTenantInfo -name 'Contoso' -id
The Tenant ID of contoso.onmicrosoft.com (contoso) is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
.EXAMPLE
PS C:\> Get-MicrosoftCloudTenantInfo -name 'Contoso'
The Tenant contoso.onmicrosoft.com (contoso) is available
.EXAMPLE
PS C:\> Get-MicrosoftCloudTenantInfo -name 'Contoso'
WARNING: The Tenant contoso.onmicrosoft.com (contoso) is taken!
.NOTES
Author: Joerg Hochwald (http://jhochwald.com)
Copyright: 2018 Joerg Hochwald. All rights reserved.
License: BSD 3-Clause "New" or "Revised" License
Version: 1.0.0
Changelog: Initial Public Release
#>
param
(
[Parameter(Mandatory,
ValueFromPipeline,
Position = 1,
HelpMessage = 'Check if a given Name is availible as Office365/Azure Tenant Name')]
[ValidateNotNullOrEmpty()]
[string]
$name,
[Alias('TenantID')]
[switch]
$id
)
Begin
{
# Define some defaults
$ST = 'Stop'
$SC = 'SilentlyContinue'
# Do not use SSLv3 for any kind of Web Requests
If ([Net.ServicePointManager]::SecurityProtocol -notmatch 'TLS12')
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS11
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::TLS12
}
# Cleanup
$available = $null
$TenantID = $null
$TenantLongName = $null
# Make the tenant name lowercase all the way, just in case!
$name = $name.ToLower()
# Where to check
$uri = 'https://portal.office.com/Signup/CheckDomainAvailability.ajax'
# OK, the Body looks creapy
$body = 'p0=' + $name + '&assembly=BOX.Admin.UI%2C+Version%3D16.0.0.0%2C+Culture%3Dneutral%2C+PublicKeyToken%3Dnull&class=Microsoft.Online.BOX.Signup.UI.SignupServerCalls'
}
Process
{
# get the Info via Rest
$paramInvokeRestMethod = $null
$paramInvokeRestMethod = @{
Method = 'Post'
Uri = $uri
Body = $body
ErrorAction = $SC
WarningAction = $SC
}
$response = (Invoke-RestMethod @paramInvokeRestMethod)
# Error handler
$valid = $response.Contains('SessionValid')
if ($valid -eq $false)
{
# Whoops
Write-Error -Message $response -ErrorAction $ST
Exit
}
# Looks good
$available = $response.Contains('<![CDATA[1]]>')
}
End
{
# Internal log Name
$TenantLongName = $name + '.onmicrosoft.com'
if ($available)
{
Write-Output -InputObject ('The Tenant {0} ({1}) is available' -f $TenantLongName, $name)
}
else
{
if ($id)
{
# Cleanup
$TenantID = $null
try
{
# Build the UIR
$TenantIDURI = 'https://login.windows.net/' + $name + '.onmicrosoft.com/.well-known/openid-configuration'
# Get the Info via regular call and split it
$paramInvokeWebRequest = $null
$paramInvokeWebRequest = @{
Uri = $TenantIDURI
ErrorAction = $ST
}
$TenantID = ((Invoke-WebRequest @paramInvokeWebRequest | ConvertFrom-Json -ErrorAction $ST).token_endpoint.Split('/')[3])
Write-Output -InputObject ('The Tenant ID of {0} ({1}) is {2}' -f $TenantLongName, $name, $TenantID)
}
catch
{
# Whoops
Write-Warning -Message 'The Tenant is taken, but we where unable to get the Tenant ID!!!'
}
}
else
{
Write-Warning -Message ('The Tenant {0} ({1}) is taken!' -f $TenantLongName, $name)
}
}
# Cleanup
$available = $null
$TenantID = $null
$TenantLongName = $null
}
}
@martefranco
Copy link

Hello!
https://portal.office.com/Signup/CheckDomainAvailability.ajax does not work and returns error, is there another endpoint that we can use?

@jhochwald
Copy link
Author

Hello!
https://portal.office.com/Signup/CheckDomainAvailability.ajax does not work and returns error, is there another endpoint that we can use?
Not that I know. At the Moment, I use this web Site: https://o365.rocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment