Skip to content

Instantly share code, notes, and snippets.

View rasmusgude's full-sized avatar

Rasmus Gude Gadegaard rasmusgude

View GitHub Profile
View azure-metadata-service-1.ps1
# “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 "$($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",
View azure-metadata-service-2.ps1
# Function for easily querying the Azure Instance Metadata Service
Function AzMeta {
Param( [Parameter(ValueFromPipeline)] $p )
Invoke-RestMethod `
-Headers @{"Metadata"="true"} `
-URI "$($p)?api-version=2017-08-01&format=text"
View Attach-PortForwardLoadBalancer.ps1
# 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}