AzWhoAmI.psm1
function Get-AzUpn { [cmdletbinding()] [alias("Get-WhoAmI")] [alias("WhoAmI")] [alias("AzWhoAmI")] param() $response = $(curl http://localhost:50342/oauth2/token --data "resource=https://management.azure.com/" -H Metadata:true -s) if ( [string]::IsNullOrEmpty($response) ) { Write-Error "Failed to get token from Azure Instance Metadata Service" return } else { $token = $response | ConvertFrom-Json | Select-Object -exp access_token foreach ($i in 0..1) { $data = $token.Split('.')[$i].Replace('-', '+').Replace('_', '/') switch ($data.Length % 4) { 0 { break } 2 { $data += '==' } 3 { $data += '=' } } } $decodedToken = [System.Text.Encoding]::UTF8.GetString([convert]::FromBase64String($data)) | ConvertFrom-Json return $decodedToken.upn } } Export-ModuleMember -Function Get-AzUpn -Alias 'Get-WhoAmI', 'WhoAmI', 'AzWhoAmI' |