Skip to content

A Software Engineer's Logbook

Cloud Development, Productivity, and Learning

Menu
  • Home
Menu

Using the Azure Metadata Service for Virtual Machines

Posted on November 17, 2022November 17, 2022 by Pete

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"
}

image

Let me know in the comments below if you’ve found other clever things to do with the Azure Metadata Service!

Subscribe to Posts

Peter Hauge

Contact me!
Email: pete @ hauge.cloud
Software Engineer at Microsoft

Recent Posts

  • Using the Azure Metadata Service for Virtual Machines
  • Authoring Azure ARM Templates
  • Recover Azure Queue Messages From the Poison Queue (unpoison messages!)
  • Creating and Deleting VSTS Team Projects with Powershell
  • How to easily switch versions of Azure PowerShell

Archives

Categories

  • ARM
  • Azure
  • Code
  • DevTest Labs
  • Powershell
  • Soft Skills
  • Troubleshooting
  • Uncategorized
  • Visual Studio
  • VSCode
  • VSTS
©2025 A Software Engineer's Logbook | Design: Newspaperly WordPress Theme