Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# “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",
# "name": "Win10",
# "offer": "Windows",
# "osType": "Windows",
# "placementGroupId": "",
# "platformFaultDomain": "0",
# "platformUpdateDomain": "0",
# "publisher": "MicrosoftVisualStudio",
# "resourceGroupName": "****",
# "sku": "Windows-10-N-x64",
# "subscriptionId": "****",
# "tags": "",
# "version": "2017.10.12",
# "vmId": "****",
# "vmSize": "Standard_B2s"
# },
# "network": {
# "interface": [
# {
# "ipv4": {
# "ipAddress": [
# {
# "privateIpAddress": "10.1.2.4",
# "publicIpAddress": "51.143.***.***"
# }
# ],
# "subnet": [
# {
# "address": "10.1.2.0",
# "prefix": "24"
# }
# ]
# },
# "ipv6": {
# "ipAddress": []
# },
# "macAddress": "******"
# }
# ]
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment