SharePoint.ps1

<#
    .SYNOPSIS
        Resolves the Office 365 Tenant Id of a given Office 365 Tenant Name
    .DESCRIPTION
        Resolves the Office 365 Tenant Id of a given Office 365 Tenant Name.
        No need to specify the *.onmicrosoft.com extension but will work nonetheless
        if specified.
        Will throw if the Tenant Name does not exists!
    .LINK
        Nexus Innovations : http://www.nexusinno.com
#>

function global:Resolve-TenantId {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true)]
        [string]$Office365TenantName
    )

    Write-Verbose "[Resolve-TenantId] Entering function scope"
    $sanitizedTenantName = $Office365TenantName.Split('.') | Select-Object -First 1
    $uri = "https://login.windows.net/$sanitizedTenantName.onmicrosoft.com/.well-known/openid-configuration"

    Write-Verbose "Making (GET) request to $uri"
    $json = Invoke-WebRequest -Uri $uri | ConvertFrom-Json

    Write-Verbose "Matching tenant id from returned json object"
    $guidPattern = "({){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}";
    $json.authorization_endpoint -match $guidPattern | Out-Null

    return $Matches[0]
}