I keep losing the details for the metadata service, so I’m posting them here so I have a reference! The link for the documentation is here: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service .
The Azure Metadata Service is used for getting information about the currently running Virtual Machine instance and is accessible from within the VM. An important/useful note, you don’t need any permission to Azure to get this information! IE: you can incorporate details here in scripts, logs, etc., without needing additional information like a service principal or managed identity! It’s a simple call to an endpoint and you get a JSON payload back.
Here’s a sample script to demonstrate it!
try {
$details = Invoke-RestMethod `
-Headers @{"Metadata"="true"} `
-Method GET `
-Uri "http://169.254.169.254/metadata/instance?api-version=2021-02-01"
Write-Host -ForegroundColor Green "Information about the current virtual machine: "
$details | Select-Object -Property `
@{Name="Name";E={$_.compute.name}},
@{Name="OSType";E={$_.compute.osType}},
@{Name="ResourceGroupName";E={$_.compute.resourceGroupName}},
@{Name="SubscriptionId";E={$_.compute.subscriptionId}},
@{Name="VmSize";E={$_.compute.vmSize}} `
| Write-Output
}
catch {
Write-Host -ForegroundColor Red "This does not appear to be an Azure Virtual Machine, Meta Service not reachable"
}
Let me know in the comments below if you’ve found other clever things to do with the Azure Metadata Service!