Get-MicrosoftTenantId.ps1
|
<#PSScriptInfo .VERSION 1.1 .GUID 4b250f93-9e99-4018-90c7-7b9a3b8f6e9a .AUTHOR Kalichuza .COMPANYNAME .COPYRIGHT .TAGS 365 .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Get Microsoft tenant id from public api url #> [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string] $TenantDomain ) try { $uri = "https://login.microsoftonline.com/$TenantDomain/v2.0/.well-known/openid-configuration" $response = Invoke-RestMethod -Uri $uri -ErrorAction Stop $tenantId = $response.issuer.Split('/')[3] if (-not $tenantId) { throw "Tenant ID could not be parsed from issuer URL." } return $tenantId } catch { Write-Error "Failed to retrieve tenant ID for domain '$TenantDomain'. The domain might not be valid or connected to M365." } |