Skip to content

Instantly share code, notes, and snippets.

View rasmusgude's full-sized avatar

Rasmus Gude Gadegaard rasmusgude

View GitHub Profile
# authentication using service principal (appid+secret)
$tenantid = '<tenant-id>';
$appid = '<app-id>';
$secret = ConvertTo-SecureString '<app-secret>' -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($appid, $secret);
Login-AzAccount -ServicePrincipal -TenantId $tenantid -Credential $cred
# if login does not succeed with service principal then prompt for login
if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {Login-AzureRmAccount}
# Function for easily querying the Azure Instance Metadata Service
Function AzMeta {
Param( [Parameter(ValueFromPipeline)] $p )
Invoke-RestMethod `
-Headers @{"Metadata"="true"} `
-URI "http://169.254.169.254/metadata/instance/$($p)?api-version=2017-08-01&format=text"
}
# “One-liner” for easily querying the Azure Instance Metadata Service.
"network/interface/0/ipv4/ipAddress/0/publicIpAddress" | Tee-Object -Variable p | Out-Null ; Invoke-RestMethod -Headers @{"Metadata"="true"} -URI "http://169.254.169.254/metadata/instance/$($p)?api-version=2017-08-01&format=text"
# Instead of "network/interface/0/ipv4/ipAddress/0/publicIpAddress" you can use any
# path throgh the instance metadata json structure.
# You can find a sample json structure here:
# {
# "compute": {
# "location": "westus2",