Azure 執行個體中繼資料服務

適用於:✔️ Linux VM ✔️ Windows VM ✔️ 彈性擴展集

The Azure Instance Metadata Service (IMDS) 提供目前執行中虛擬機器執行個體的相關資訊。 您可以使用其來管理及設定虛擬機器。 此資訊包括 SKU、儲存體、網路組態及近期維護事件。 如需可用資料的完整清單,請參閱端點類別摘要

IMDS 適用於執行中的虛擬機器 (VM) 執行個體和擴展集執行個體。 所有端點都支援使用 Azure Resource Manager 建立和管理的 VM。 只有執行個體類別的證明類別和網路部分支援以傳統部署模型建立的 VM。 證明的端點只會在有限的範圍內執行。

IMDS 是位於已知非路由式 IP 位址 (169.254.169.254) 上的 REST API。 您只能從 VM 存取。 VM 與 IMDS 之間的通訊永遠不會離開主機。 在查詢 IMDS 時,讓 HTTP 用戶端略過 VM 內的 Web Proxy,並且將 169.254.169.254 視為與 168.63.129.16 相同。

使用方式

存取 Azure Instance Metadata Service

若要存取 IMDS,請從 Azure Resource ManagerAzure 入口網站建立 VM,然後使用下列範例。 如需更多範例,請參閱 Azure Instance Metadata 範例

以下是擷取執行個體所有中繼資料的範例程式碼。 若要存取特定資料來源,請參閱端點類別,以取得所有可用的功能概觀。

要求

重要

此範例會略過 Proxy。 查詢 IMDS 時,您必須略過 Proxy。 如需其他資訊,請參閱 Proxy

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance?api-version=2021-02-01" | ConvertTo-Json -Depth 64

-NoProxy 需要 PowerShell V6 或更新版本。 如需舊版 PowerShell 的範例,請參閱我們的範例存放庫

回應

注意

回應是 JSON 字串。 下列範例回應均列印清晰,很容易閱讀。

{
    "compute": {
        "azEnvironment": "AZUREPUBLICCLOUD",
        "additionalCapabilities": {
            "hibernationEnabled": "true"
        },
        "hostGroup": {
          "id": "testHostGroupId"
        }, 
        "extendedLocation": {
            "type": "edgeZone",
            "name": "microsoftlosangeles"
        },
        "evictionPolicy": "",
        "isHostCompatibilityLayerVm": "true",
        "licenseType":  "Windows_Client",
        "location": "westus",
        "name": "examplevmname",
        "offer": "WindowsServer",
        "osProfile": {
            "adminUsername": "admin",
            "computerName": "examplevmname",
            "disablePasswordAuthentication": "true"
        },
        "osType": "Windows",
        "placementGroupId": "f67c14ab-e92c-408c-ae2d-da15866ec79a",
        "plan": {
            "name": "planName",
            "product": "planProduct",
            "publisher": "planPublisher"
        },
        "platformFaultDomain": "36",
        "platformSubFaultDomain": "",        
        "platformUpdateDomain": "42",
        "priority": "Regular",
        "publicKeys": [{
                "keyData": "ssh-rsa 0",
                "path": "/home/user/.ssh/authorized_keys0"
            },
            {
                "keyData": "ssh-rsa 1",
                "path": "/home/user/.ssh/authorized_keys1"
            }
        ],
        "publisher": "RDFE-Test-Microsoft-Windows-Server-Group",
        "resourceGroupName": "macikgo-test-may-23",
        "resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/virtualMachines/examplevmname",
        "securityProfile": {
            "secureBootEnabled": "true",
            "virtualTpmEnabled": "false",
            "encryptionAtHost": "true",
            "securityType": "TrustedLaunch"
        },
        "sku": "2019-Datacenter",
        "storageProfile": {
            "dataDisks": [{
                "bytesPerSecondThrottle": "979202048",
                "caching": "None",
                "createOption": "Empty",
                "diskCapacityBytes": "274877906944",
                "diskSizeGB": "1024",
                "image": {
                  "uri": ""
                },
                "isSharedDisk": "false",
                "isUltraDisk": "true",
                "lun": "0",
                "managedDisk": {
                  "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampledatadiskname",
                  "storageAccountType": "StandardSSD_LRS"
                },
                "name": "exampledatadiskname",
                "opsPerSecondThrottle": "65280",
                "vhd": {
                  "uri": ""
                },
                "writeAcceleratorEnabled": "false"
            }],
            "imageReference": {
                "id": "",
                "offer": "WindowsServer",
                "publisher": "MicrosoftWindowsServer",
                "sku": "2019-Datacenter",
                "version": "latest",
                "communityGalleryImageId": "/CommunityGalleries/testgallery/Images/1804Gen2/Versions/latest",
                "sharedGalleryImageId": "/SharedGalleries/1P/Images/gen2/Versions/latest",
                "exactVersion": "1.1686127202.30113"
            },
            "osDisk": {
                "caching": "ReadWrite",
                "createOption": "FromImage",
                "diskSizeGB": "30",
                "diffDiskSettings": {
                    "option": "Local"
                },
                "encryptionSettings": {
                  "enabled": "false",
                  "diskEncryptionKey": {
                    "sourceVault": {
                      "id": "/subscriptions/test-source-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                    },
                    "secretUrl": "https://test-disk.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
                  },
                  "keyEncryptionKey": {
                    "sourceVault": {
                      "id": "/subscriptions/test-key-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                    },
                    "keyUrl": "https://test-key.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
                  }
                },
                "image": {
                    "uri": ""
                },
                "managedDisk": {
                    "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampleosdiskname",
                    "storageAccountType": "StandardSSD_LRS"
                },
                "name": "exampleosdiskname",
                "osType": "Windows",
                "vhd": {
                    "uri": ""
                },
                "writeAcceleratorEnabled": "false"
            },
            "resourceDisk": {
                "size": "4096"
            }
        },
        "subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
        "tags": "baz:bash;foo:bar",
        "userData": "Zm9vYmFy",
        "version": "15.05.22",
        "virtualMachineScaleSet": {
            "id": "/subscriptions/xxxxxxxx-xxxxx-xxx-xxx-xxxx/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachineScaleSets/virtual-machine-scale-set-name"
        },
        "vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6",
        "vmScaleSetName": "crpteste9vflji9",
        "vmSize": "Standard_A3",
        "zone": ""
    },
    "network": {
        "interface": [{
            "ipv4": {
               "ipAddress": [{
                    "privateIpAddress": "10.144.133.132",
                    "publicIpAddress": ""
                }],
                "subnet": [{
                    "address": "10.144.133.128",
                    "prefix": "26"
                }]
            },
            "ipv6": {
                "ipAddress": [
                 ]
            },
            "macAddress": "0011AAFFBB22"
        }]
    }
}

安全性和驗證

Instance Metadata Service 只能從非可路由 IP 位址上的執行中虛擬機器執行個體內存取。 VM 只能與自己的中繼資料/功能互動。 API 僅限 HTTP 且永遠不會離開主機。

為了確保要求直接用於 IMDS,並防止非預期或不必要的要求重新導向,要求:

  • 必須包含標頭 Metadata: true
  • 不得包含 X-Forwarded-For 標頭

服務將會拒絕不符合以上兩項需求的任何要求。

重要

IMDS 是敏感性資料的通道。 API 未經驗證且對 VM 上的所有程序都開放。 透過此服務公開的資訊,應視為 VM 內所有執行中應用程式的共用資訊。

如果不是 VM 上每個程序都需要存取 IMDS 端點,您可以設定本機防火牆規則來限制存取。 例如,如果只有已知的系統服務需要存取執行個體中繼資料服務,您可以在 IMDS 端點上設定防火牆規則,只允許特定程序存取,或拒絕其餘程序存取。

Proxy

IMDS 適合在 proxy 後方使用,因此不支援此做法。 大部分 HTTP 用戶端都提供一個選項來讓您在要求上停用 Proxy,而且與 IMDS 通訊時必須利用這項功能。 如需詳細資訊,請參閱您的用戶端文件。

重要

即使您不知道環境中的任何 Proxy 組態,您仍必須覆寫任何預設用戶端 Proxy 設定。 您可以自動探索 Proxy 組態。若未能略過這類組態,未來變更電腦組態時將會有發生中斷的風險。

速率限制

一般而言,對 IMDS 的要求限制為每秒 5 個要求 (以每部 VM 為基礎)。 超過此閾值的要求會遭到拒絕,並出現 429 回應。 受控識別類別的要求限制為每秒 20 個要求和 5 個並行要求 (以每部 VM 為基礎)。

HTTP 指令動詞

目前支援以下 HTTP 動詞:

動詞命令 描述
GET 擷取要求的資源

參數

端點可能支援必要和/或選擇性參數。 如需詳細資訊,請參閱結構描述和特定有問題端點的文件。

查詢參數

IMDS 端點支援 HTTP 查詢字串參數。 例如:

http://169.254.169.254/metadata/instance/compute?api-version=2021-01-01&format=json

指定參數:

名稱
api-version 2021-01-01
format json

具有重複查詢參數名稱的要求將會遭到拒絕。

路由參數

針對某些傳回較大 json Blob 的端點,我們支援將路由參數附加至要求的端點,以篩選為回應的子集:

http://169.254.169.254/metadata/<endpoint>/[<filter parameter>/...]?<query parameters>

如果您與剖析表示法進行互動,參數便會對應到用來逐步執行 json 物件的索引/索引鍵。

舉例來說,/metadata/instance 會傳回 json 物件:

{
    "compute": { ... },
    "network": {
        "interface": [
            {
                "ipv4": {
                   "ipAddress": [{
                        "privateIpAddress": "10.144.133.132",
                        "publicIpAddress": ""
                    }],
                    "subnet": [{
                        "address": "10.144.133.128",
                        "prefix": "26"
                    }]
                },
                "ipv6": {
                    "ipAddress": [
                     ]
                },
                "macAddress": "0011AAFFBB22"
            },
            ...
        ]
    }
}

如果我們想要將回應篩選到只有計算屬性,我們會傳送要求:

http://169.254.169.254/metadata/instance/compute?api-version=<version>

同樣地,如果我們想要篩選巢狀屬性或特定陣列元素,我們會保留附加索引鍵:

http://169.254.169.254/metadata/instance/network/interface/0?api-version=<version>

會篩選至 Network.interface 屬性中的第一個元素,並傳回:

{
    "ipv4": {
       "ipAddress": [{
            "privateIpAddress": "10.144.133.132",
            "publicIpAddress": ""
        }],
        "subnet": [{
            "address": "10.144.133.128",
            "prefix": "26"
        }]
    },
    "ipv6": {
        "ipAddress": [
         ]
    },
    "macAddress": "0011AAFFBB22"
}

注意

篩選至分葉節點時,format=json 無法運作。 對於這些查詢,format=text 需要明確指定,因為預設格式為 json。

結構描述

資料格式

根據預設,IMDS 會傳回 JSON 格式的資料 (Content-Type: application/json)。 不過,支援回應篩選的端點 (請參閱路由參數) 也支援 text 格式。

若要存取非預設的回應格式,請指定要求的格式作為要求中的查詢字串參數。 例如:

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=text"

在 json 回應中,所有基本項目都會是 string 類型,而且一律包含遺漏或不可套用的值,但是會設定為空字串。

版本控制

IMDS 已版本化,因此在 HTTP 要求中指定 API 版本是強制的。 這項需求的唯一例外是版本端點,可用來動態擷取可用的 API 版本。

新增較新版本時,如果您的指令碼對於特定資料格式有相依性,則因為相容性而仍可存取較舊版本。

未指定版本時,您將會收到錯誤,其中包含最新支援版本的清單:

{
    "error": "Bad request. api-version was not specified in the request. For more information refer to aka.ms/azureimds",
    "newest-versions": [
        "2020-10-01",
        "2020-09-01",
        "2020-07-15"
    ]
}

支援的 API 版本

注意

版本 2023-07-01 仍在推出中,某些區域可能無法使用。

  • 2023-07-01
  • 2021-12-13
  • 2021-11-15
  • 2021-11-01
  • 2021-10-01
  • 2021-08-01
  • 2021-05-01
  • 2021-03-01
  • 2021-02-01
  • 2021-01-01
  • 2020-12-01
  • 2020-10-01
  • 2020-09-01
  • 2020-07-15
  • 2020-06-01
  • 2019-11-01
  • 2019-08-15
  • 2019-08-01
  • 2019-06-04
  • 2019-06-01
  • 2019-04-30
  • 2019-03-11
  • 2019-02-01
  • 2018-10-01
  • 2018-04-02
  • 2018-02-01
  • 2017-12-01
  • 2017-10-01
  • 2017 年 8 月 1 日
  • 2017-04-02
  • 2017-03-01

Swagger

IMDS 的完整 Swagger 定義位於:https://github.com/Azure/azure-rest-api-specs/blob/main/specification/imds/data-plane/readme.md

區域可用性

這項服務已在所有 Azure 雲端中正式推出

根端點

根端點為 http://169.254.169.254/metadata

端點類別

IMDS API 包含代表不同資料來源的多個端點類別,每個類別都包含一個或多個端點。 查看每個類別以取得詳細資料。

類別根目錄 描述 導入的版本
/metadata/attested 請參閱證明資料 2018-10-01
/metadata/identity 請參閱透過 IMDS 的受控識別 2018-02-01
/metadata/instance 請參閱執行個體中繼資料 2017-04-02
/metadata/loadbalancer 請參閱透過 IMDS 擷取 Load Balancer 中繼資料 2020-10-01
/metadata/scheduledevents 請參閱透過 IMDS 的已排定事件 2017 年 8 月 1 日
/metadata/versions 請參閱版本 N/A

版本

列出 API 版本

傳回支援的 API 版本集合。

GET /metadata/versions

參數

無 (此端點未建立版本)。

回應

{
  "apiVersions": [
    "2017-03-01",
    "2017-04-02",
    ...
  ]
}

執行個體中繼資料

取得 VM 中繼資料

公開 VM 執行個體的重要中繼資料,包括計算、網路和儲存體。

GET /metadata/instance

參數

名稱 必要/選用 描述
api-version 必要 用來服務要求的版本。
format 選擇性* 回應的格式 (jsontext)。 *注意:使用要求參數時可能需要

此端點支援透過路由參數進行回應篩選。

回應

{
    "compute": {
        "azEnvironment": "AZUREPUBLICCLOUD",
        "additionalCapabilities": {
            "hibernationEnabled": "true"
        },
        "hostGroup": {
          "id": "testHostGroupId"
        }, 
        "extendedLocation": {
            "type": "edgeZone",
            "name": "microsoftlosangeles"
        },
        "evictionPolicy": "",
        "isHostCompatibilityLayerVm": "true",
        "licenseType":  "Windows_Client",
        "location": "westus",
        "name": "examplevmname",
        "offer": "WindowsServer",
        "osProfile": {
            "adminUsername": "admin",
            "computerName": "examplevmname",
            "disablePasswordAuthentication": "true"
        },
        "osType": "Windows",
        "placementGroupId": "f67c14ab-e92c-408c-ae2d-da15866ec79a",
        "plan": {
            "name": "planName",
            "product": "planProduct",
            "publisher": "planPublisher"
        },
        "platformFaultDomain": "36",
        "platformSubFaultDomain": "",        
        "platformUpdateDomain": "42",
        "priority": "Regular",
        "publicKeys": [{
                "keyData": "ssh-rsa 0",
                "path": "/home/user/.ssh/authorized_keys0"
            },
            {
                "keyData": "ssh-rsa 1",
                "path": "/home/user/.ssh/authorized_keys1"
            }
        ],
        "publisher": "RDFE-Test-Microsoft-Windows-Server-Group",
        "resourceGroupName": "macikgo-test-may-23",
        "resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/virtualMachines/examplevmname",
        "securityProfile": {
            "secureBootEnabled": "true",
            "virtualTpmEnabled": "false",
            "encryptionAtHost": "true",
            "securityType": "TrustedLaunch"
        },
        "sku": "2019-Datacenter",
        "storageProfile": {
            "dataDisks": [{
                "bytesPerSecondThrottle": "979202048",
                "caching": "None",
                "createOption": "Empty",
                "diskCapacityBytes": "274877906944",
                "diskSizeGB": "1024",
                "image": {
                  "uri": ""
                },
                "isSharedDisk": "false",
                "isUltraDisk": "true",
                "lun": "0",
                "managedDisk": {
                  "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampledatadiskname",
                  "storageAccountType": "StandardSSD_LRS"
                },
                "name": "exampledatadiskname",
                "opsPerSecondThrottle": "65280",
                "vhd": {
                  "uri": ""
                },
                "writeAcceleratorEnabled": "false"
            }],
            "imageReference": {
                "id": "",
                "offer": "WindowsServer",
                "publisher": "MicrosoftWindowsServer",
                "sku": "2019-Datacenter",
                "version": "latest",
                "communityGalleryImageId": "/CommunityGalleries/testgallery/Images/1804Gen2/Versions/latest",
                "sharedGalleryImageId": "/SharedGalleries/1P/Images/gen2/Versions/latest",
                "exactVersion": "1.1686127202.30113"
            },
            "osDisk": {
                "caching": "ReadWrite",
                "createOption": "FromImage",
                "diskSizeGB": "30",
                "diffDiskSettings": {
                    "option": "Local"
                },
                "encryptionSettings": {
                  "enabled": "false",
                  "diskEncryptionKey": {
                    "sourceVault": {
                      "id": "/subscriptions/test-source-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                    },
                    "secretUrl": "https://test-disk.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
                  },
                  "keyEncryptionKey": {
                    "sourceVault": {
                      "id": "/subscriptions/test-key-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                    },
                    "keyUrl": "https://test-key.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
                  }
                },
                "image": {
                    "uri": ""
                },
                "managedDisk": {
                    "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampleosdiskname",
                    "storageAccountType": "StandardSSD_LRS"
                },
                "name": "exampleosdiskname",
                "osType": "Windows",
                "vhd": {
                    "uri": ""
                },
                "writeAcceleratorEnabled": "false"
            },
            "resourceDisk": {
                "size": "4096"
            }
        },
        "subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
        "tags": "baz:bash;foo:bar",
        "userData": "Zm9vYmFy",
        "version": "15.05.22",
        "virtualMachineScaleSet": {
            "id": "/subscriptions/xxxxxxxx-xxxxx-xxx-xxx-xxxx/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachineScaleSets/virtual-machine-scale-set-name"
        },
        "vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6",
        "vmScaleSetName": "crpteste9vflji9",
        "vmSize": "Standard_A3",
        "zone": ""
    },
    "network": {
        "interface": [{
            "ipv4": {
               "ipAddress": [{
                    "privateIpAddress": "10.144.133.132",
                    "publicIpAddress": ""
                }],
                "subnet": [{
                    "address": "10.144.133.128",
                    "prefix": "26"
                }]
            },
            "ipv6": {
                "ipAddress": [
                 ]
            },
            "macAddress": "0011AAFFBB22"
        }]
    }
}

結構描述明細:

計算

資料 描述 導入的版本
azEnvironment VM 執行所在的 Azure 環境 2018-10-01
additionalCapabilities.hibernationEnabled 識別 VM 上是否已啟用休眠 2021-11-01
customData 這項功能在 IMDS 中已淘汰並停用。 已由 userData 取代 2019-02-01
evictionPolicy 設定現成 VM 的收回方式。 2020-12-01
extendedLocation.type VM 擴充位置的類型。 2021-03-01
extendedLocation.name VM 擴充位置的名稱 2021-03-01
host.id VM 主機的名稱。 請注意,VM 將會有主機或 hostGroup,但不會同時擁有這兩者。 2021-11-15
hostGroup.id VM 的 hostGroup 名稱。 請注意,VM 將會有主機或 hostGroup,但不會同時擁有這兩者。 2021-11-15
isHostCompatibilityLayerVm 識別 VM 是否在主機相容性層級上執行 2020-06-01
licenseType Azure Hybrid Benefit 的授權類型。 這僅適用於已啟用 AHB 的 VM 2020-09-01
location VM 執行所在的 Azure 區域 2017-04-02
name VM 的名稱 2017-04-02
offer 提供 VM 映像的資訊,而且只針對從 Azure 映像資源庫部署的映像呈現 2017-04-02
osProfile.adminUsername 指定系統管理員帳戶的名稱 2020-07-15
osProfile.computerName 指定電腦的名稱 2020-07-15
osProfile.disablePasswordAuthentication 指定密碼驗證是否停用。 這僅適用於 Linux VM 2020-10-01
osType Linux 或 Windows 2017-04-02
placementGroupId 擴展集的放置群組 2017 年 8 月 1 日
plan 如果 VM 是 Azure Marketplace 映像,則 Plan 包含 VM 的名稱、產品及發行者 2018-04-02
platformUpdateDomain VM 執行所在的更新網域 2017-04-02
platformFaultDomain VM 執行所在的容錯網域 2017-04-02
platformSubFaultDomain VM 執行所在的子容錯網域 (如果適用)。 2021-10-01
priority VM 的優先順序。 如需詳細資訊,請參閱現成 VM 2020-12-01
provider VM 的提供者 2018-10-01
publicKeys 指派給 VM 和路徑的公開金鑰集合 2018-04-02
publisher VM 映像的發佈者 2017-04-02
resourceGroupName 虛擬機器的資源群組 2017 年 8 月 1 日
resourceId 資源的完整 ID 2019-03-11
sku VM 映像的特定 SKU 2017-04-02
securityProfile.secureBootEnabled 識別 VM 上是否已啟用 UEFI 安全開機 2020-06-01
securityProfile.virtualTpmEnabled 識別 VM 上是否已啟用虛擬信賴平台模組 (TPM) 2020-06-01
securityProfile.encryptionAtHost 識別 VM 上是否已啟用主機上的加密 2021-11-01
securityProfile.securityType 識別 VM 是否為受信任的 VM機密 VM 2021-12-13
storageProfile 請參閱下列儲存體設定檔 2019-06-01
subscriptionId 虛擬機器的 Azure 訂用帳戶 2017 年 8 月 1 日
tags 虛擬機器的標籤 2017 年 8 月 1 日
tagsList 格式化為 JSON 陣列以方便透過程式設計剖析的標籤 2019-06-04
userData 建立 VM 以在佈建期間或之後使用時所指定的資料集 (Base64 編碼) 2021-01-01
version VM 映像的版本 2017-04-02
virtualMachineScaleSet.id 使用彈性協調流程建立的虛擬機器擴展集識別碼,虛擬機器是其中的一部分。 此欄位不適用於使用統一協調流程建立的虛擬機器擴展集。 2021-03-01
vmId VM 的唯一識別碼。 參考的部落格僅適合用於具有 SMBIOS < 2.6 的 VM。 若是具有 SMBIOS >= 2.6 的 VM,來自 DMI 的 UUID 會以位元組由小到大的格式顯示,因此不需要切換位元組。 2017-04-02
vmScaleSetName 擴展集的虛擬機擴展集名稱 2017-12-01
vmSize VM 大小 2017-04-02
zone 您虛擬機器的可用性區域 2017-12-01

† 此版本尚未完全可用,而且不是所有區域都支援此版本。

儲存體設定檔

VM 的儲存體設定檔分成三個類別:映像參考、OS 磁碟和資料磁碟,加上本機暫存磁碟的其他物件。

映像參考物件包含下列有關 OS 映像的資訊,請注意,映像可能來自平台、市集、社群資源庫或直接共用資源庫,但不可以兩者並存:

資料 描述 導入的版本
id 資源識別碼 2019-06-01
offer 平台或市集映像的供應項目 2019-06-01
publisher 平台或市集映像的發行者 2019-06-01
sku 平台或市集映像的 SKU 2019-06-01
version 映像的版本 2019-06-01
communityGalleryImageId 社群映像的資源識別碼,若無則為空白 2023-07-01
sharedGalleryImageId 直接共用映像的資源識別碼,若無則為空白 2023-07-01
exactVersion 社群或直接共用映像的版本 2023-07-01

OS 磁碟物件包含有關 VM 所使用 OS 磁碟的下列資訊:

資料 描述
caching 快取需求
createOption 如何建立 VM 的相關資訊
diffDiskSettings 暫時性磁碟設定
diskSizeGB 磁碟大小 (GB)
image 來源使用者映像虛擬硬碟
managedDisk 受控磁碟參數
name 磁碟名稱
vhd 虛擬硬碟
writeAcceleratorEnabled 是否在磁碟上啟用 writeAccelerator

資料磁碟陣列包含附加至 VM 的資料磁碟清單。 每個資料磁碟物件都包含下列資訊:

資料 描述 導入的版本
bytesPerSecondThrottle* 磁碟讀取/寫入配額 (以位元組為單位) 2021-05-01
caching 快取需求 2019-06-01
createOption 如何建立 VM 的相關資訊 2019-06-01
diffDiskSettings 暫時性磁碟設定 2019-06-01
diskCapacityBytes* 磁碟的大小 (以位元組為單位) 2021-05-01
diskSizeGB 磁碟大小 (GB) 2019-06-01
encryptionSettings 磁碟的加密設定 2019-06-01
image 來源使用者映像虛擬硬碟 2019-06-01
isSharedDisk* 識別磁碟是否在資源之間共用 2021-05-01
isUltraDisk 識別資料磁碟是否為 Ultra 磁碟 2021-05-01
lun 磁碟的邏輯單元編號 2019-06-01
managedDisk 受控磁碟參數 2019-06-01
name 磁碟名稱 2019-06-01
opsPerSecondThrottle* 磁碟讀取/寫入配額 (以 IOPS 為單位) 2021-05-01
osType 磁碟中包含的 OS 類型 2019-06-01
vhd 虛擬硬碟 2019-06-01
writeAcceleratorEnabled 是否在磁碟上啟用 writeAccelerator 2019-06-01

* 這些欄位只會針對 Ultra 磁碟填入;來自非 Ultra 磁碟時會是空白字串。

加密設定 Blob 包含磁碟加密方式 (如果已加密) 的相關資料:

資料 描述 導入的版本
diskEncryptionKey.sourceVault.id 磁碟加密金鑰的位置 2021-11-01
diskEncryptionKey.secretUrl 祕密的位置 2021-11-01
keyEncryptionKey.sourceVault.id 金鑰加密金鑰的位置 2021-11-01
keyEncryptionKey.keyUrl 金鑰的位置 2021-11-01

資源磁碟物件包含連結至 VM 的本機暫存磁碟大小,如果有的話以 KB 為單位。 如果 VM 沒有本機暫存磁碟,則此值為 0。

資料 描述 導入的版本
resourceDisk.size VM 的本機暫存磁碟大小 (以 kB 為單位) 2021-02-01

Network

資料 描述 導入的版本
ipv4.privateIpAddress VM 的本機 IPv4 位址 2017-04-02
ipv4.publicIpAddress VM 的公用 IPv4 位址 2017-04-02
subnet.address VM 的子網路位址 2017-04-02
subnet.prefix 子網路首碼,範例 24 2017-04-02
ipv6.ipAddress VM 的本機 IPv6 位址 2017-04-02
macAddress VM mac 位址 2017-04-02

注意

網路呼叫所傳回的 nic 不保證依照順序。

取得使用者資料

建立新的 VM 時,您可以指定要在 VM 佈建期間或之後使用的一組資料,並透過 IMDS 加以擷取。 請在這裡查看端對端使用者資料體驗。

若要設定使用者資料,請利用這裡的快速入門範本。 下列範例顯示如何透過 IMDS 擷取此資料。 此功能與 2021-01-01 版本和更新版本一起發行。

注意

安全性注意事項:IMDS 對 VM 上所有應用程式開放,敏感性資料不應放在使用者資料中。

$userData = Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/userData?api-version=2021-01-01&format=text"
[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($userData))

範例 1:追蹤在 Azure 上執行的 VM

身為服務提供者,您可能需要追蹤執行軟體的 VM 數目,或者具有需要追蹤 VM 唯一性的代理程式。 若要能夠取得 VM 的唯一識別碼,請從執行個體中繼資料服務使用 vmId 欄位。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/vmId?api-version=2017-08-01&format=text"

回應

5c08b38e-4d57-4c23-ac45-aca61037f084

範例 2:放置不同的資料複本

對於特定案例,不同資料複本的放置相當重要。 例如 HDFS 複本放置,或透過協調器的容器放置,您需要知道 VM 執行所在的 platformFaultDomainplatformUpdateDomain。 您也可以使用適用於執行個體的可用性區域來做出這些決定。 您可以直接透過 IMDS 來查詢此資料。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/platformFaultDomain?api-version=2017-08-01&format=text"

回應

0

範例 3:取得 VM 標記

VM 標籤包含執行個體/計算/標籤端點下的執行個體 API。 標籤可能已套用至您的 Azure VM,以邏輯方式將其組織成分類法。 您可以使用下列要求來擷取指派給 VM 的標籤。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/tags?api-version=2017-08-01&format=text"

回應

Department:IT;ReferenceNumber:123456;TestStatus:Pending

tags 欄位是字串,其中包含以分號分隔的標記。 如果標記本身使用分號,此輸出可能會是個問題。 如果剖析器是以程式設計方式擷取標記,您應該依賴 tagsList 欄位。 tagsList 欄位是沒有分隔符號的 JSON 陣列,因此更容易剖析。 您可以使用下列要求來擷取指派給 VM 的 tagsList。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/tagsList?api-version=2019-06-04" | ConvertTo-Json -Depth 64

回應

{
    "value":  [
                  {
                      "name":  "Department",
                      "value":  "IT"
                  },
                  {
                      "name":  "ReferenceNumber",
                      "value":  "123456"
                  },
                  {
                      "name":  "TestStatus",
                      "value":  "Pending"
                  }
              ],
    "Count":  3
}

範例 4:取得支援案例期間 VM 的相關詳細資訊

身為服務提供者,您可能會收到支援呼叫,而您想要知道更多 VM 的相關資訊。 要求客戶共用計算中繼資料可以為支援專業人員提供基本資訊,以了解 Azure 上的 VM 種類。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute?api-version=2020-09-01" | ConvertTo-Json -Depth 64

回應

注意

回應是 JSON 字串。 下列範例回應均列印清晰,很容易閱讀。

{
    "azEnvironment": "AZUREPUBLICCLOUD",
    "extendedLocation": {
      "type": "edgeZone",
      "name": "microsoftlosangeles"
    },
    "evictionPolicy": "",
    "additionalCapabilities": {
        "hibernationEnabled": "false"
    },
    "hostGroup": {
      "id": "testHostGroupId"
    },
    "isHostCompatibilityLayerVm": "true",
    "licenseType":  "Windows_Client",
    "location": "westus",
    "name": "examplevmname",
    "offer": "WindowsServer",
    "osProfile": {
        "adminUsername": "admin",
        "computerName": "examplevmname",
        "disablePasswordAuthentication": "true"
    },
    "osType": "Windows",
    "placementGroupId": "f67c14ab-e92c-408c-ae2d-da15866ec79a",
    "plan": {
        "name": "planName",
        "product": "planProduct",
        "publisher": "planPublisher"
    },
    "platformFaultDomain": "36",
    "platformUpdateDomain": "42",
    "priority": "Regular",
    "publicKeys": [{
            "keyData": "ssh-rsa 0",
            "path": "/home/user/.ssh/authorized_keys0"
        },
        {
            "keyData": "ssh-rsa 1",
            "path": "/home/user/.ssh/authorized_keys1"
        }
    ],
    "publisher": "RDFE-Test-Microsoft-Windows-Server-Group",
    "resourceGroupName": "macikgo-test-may-23",
    "resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/virtualMachines/examplevmname",
    "securityProfile": {
        "secureBootEnabled": "true",
        "virtualTpmEnabled": "false",
        "encryptionAtHost": "true",
        "securityType": "TrustedLaunch"
    },
    "sku": "2019-Datacenter",
    "storageProfile": {
        "dataDisks": [{
            "bytesPerSecondThrottle": "979202048",
            "caching": "None",
            "createOption": "Empty",
            "diskCapacityBytes": "274877906944",
            "diskSizeGB": "1024",
            "image": {
              "uri": ""
            },
            "isSharedDisk": "false",
            "isUltraDisk": "true",
            "lun": "0",
            "managedDisk": {
              "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/MicrosoftCompute/disks/exampledatadiskname",
              "storageAccountType": "StandardSSD_LRS"
            },
            "name": "exampledatadiskname",
            "opsPerSecondThrottle": "65280",
            "vhd": {
              "uri": ""
            },
            "writeAcceleratorEnabled": "false"
        }],
        "imageReference": {
            "id": "",
            "offer": "WindowsServer",
            "publisher": "MicrosoftWindowsServer",
            "sku": "2019-Datacenter",
            "version": "latest",
            "communityGalleryImageId": "/CommunityGalleries/testgallery/Images/1804Gen2/Versions/latest",
            "sharedGalleryImageId": "/SharedGalleries/1P/Images/gen2/Versions/latest",
            "exactVersion": "1.1686127202.30113"
        },
        "osDisk": {
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "diskSizeGB": "30",
            "diffDiskSettings": {
                "option": "Local"
            },
            "encryptionSettings": {
              "enabled": "false",
              "diskEncryptionKey": {
                "sourceVault": {
                  "id": "/subscriptions/test-source-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                },
                "secretUrl": "https://test-disk.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
              },
              "keyEncryptionKey": {
                "sourceVault": {
                  "id": "/subscriptions/test-key-guid/resourceGroups/testrg/providers/Microsoft.KeyVault/vaults/test-kv"
                },
                "keyUrl": "https://test-key.vault.azure.net/secrets/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
              }
            },
            "image": {
                "uri": ""
            },
            "managedDisk": {
                "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampleosdiskname",
                "storageAccountType": "StandardSSD_LRS"
            },
            "name": "exampleosdiskname",
            "osType": "Windows",
            "vhd": {
                "uri": ""
            },
            "writeAcceleratorEnabled": "false"
        },
        "resourceDisk": {
            "size": "4096"
        }
    },
    "subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
    "tags": "baz:bash;foo:bar",
    "version": "15.05.22",
    "virtualMachineScaleSet": {
      "id": "/subscriptions/xxxxxxxx-xxxxx-xxx-xxx-xxxx/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachineScaleSets/virtual-machine-scale-set-name"
    },
    "vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6",
    "vmScaleSetName": "crpteste9vflji9",
    "vmSize": "Standard_A3",
    "zone": ""
}

範例 5:取得 VM 執行所在的 Azure 環境

Azure 有各種不同的主權雲端,例如 Azure Government。 有時候您會需要 Azure 環境來進行一些執行階段決策。 下列範例會說明如何實現此行為。

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/compute/azEnvironment?api-version=2018-10-01&format=text"

回應

AzurePublicCloud

以下列出雲端和 Azure 環境的值。

雲端 Azure 環境
所有正式推出的全域 Azure 區域 AzurePublicCloud
Azure Government AzureUSGovernmentCloud
由 21Vianet 營運的 Microsoft Azure AzureChinaCloud
Azure Germany AzureGermanCloud

範例 6:擷取網路資訊

要求

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/network?api-version=2017-08-01" | ConvertTo-Json  -Depth 64

回應

{
  "interface": [
    {
      "ipv4": {
        "ipAddress": [
          {
            "privateIpAddress": "10.1.0.4",
            "publicIpAddress": "X.X.X.X"
          }
        ],
        "subnet": [
          {
            "address": "10.1.0.0",
            "prefix": "24"
          }
        ]
      },
      "ipv6": {
        "ipAddress": []
      },
      "macAddress": "000D3AF806EC"
    }
  ]
}

範例 7:擷取公用 IP 位址

Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-08-01&format=text"

注意

證明資料

取得證明資料

IMDS 有助於保證所提供的資料來自 Azure。 Microsoft 簽署此資訊的一部分,因此您可以確認 Azure Marketplace 中的映像是您在 Azure 上執行的映像。

GET /metadata/attested/document

參數

名稱 必要/選用 描述
api-version 必要 用來服務要求的版本。
nonce 選擇性 作為密碼編譯 Nonce 的 10 位數字串。 如果未提供任何值,IMDS 會使用目前的 UTC 時間戳記。

回應

{
    "encoding":"pkcs7",
    "signature":"MIIEEgYJKoZIhvcNAQcCoIIEAzCCA/8CAQExDzANBgkqhkiG9w0BAQsFADCBugYJKoZIhvcNAQcBoIGsBIGpeyJub25jZSI6IjEyMzQ1NjY3NjYiLCJwbGFuIjp7Im5hbWUiOiIiLCJwcm9kdWN0IjoiIiwicHVibGlzaGVyIjoiIn0sInRpbWVTdGFtcCI6eyJjcmVhdGVkT24iOiIxMS8yMC8xOCAyMjowNzozOSAtMDAwMCIsImV4cGlyZXNPbiI6IjExLzIwLzE4IDIyOjA4OjI0IC0wMDAwIn0sInZtSWQiOiIifaCCAj8wggI7MIIBpKADAgECAhBnxW5Kh8dslEBA0E2mIBJ0MA0GCSqGSIb3DQEBBAUAMCsxKTAnBgNVBAMTIHRlc3RzdWJkb21haW4ubWV0YWRhdGEuYXp1cmUuY29tMB4XDTE4MTEyMDIxNTc1N1oXDTE4MTIyMDIxNTc1NlowKzEpMCcGA1UEAxMgdGVzdHN1YmRvbWFpbi5tZXRhZGF0YS5henVyZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAML/tBo86ENWPzmXZ0kPkX5dY5QZ150mA8lommszE71x2sCLonzv4/UWk4H+jMMWRRwIea2CuQ5RhdWAHvKq6if4okKNt66fxm+YTVz9z0CTfCLmLT+nsdfOAsG1xZppEapC0Cd9vD6NCKyE8aYI1pliaeOnFjG0WvMY04uWz2MdAgMBAAGjYDBeMFwGA1UdAQRVMFOAENnYkHLa04Ut4Mpt7TkJFfyhLTArMSkwJwYDVQQDEyB0ZXN0c3ViZG9tYWluLm1ldGFkYXRhLmF6dXJlLmNvbYIQZ8VuSofHbJRAQNBNpiASdDANBgkqhkiG9w0BAQQFAAOBgQCLSM6aX5Bs1KHCJp4VQtxZPzXF71rVKCocHy3N9PTJQ9Fpnd+bYw2vSpQHg/AiG82WuDFpPReJvr7Pa938mZqW9HUOGjQKK2FYDTg6fXD8pkPdyghlX5boGWAMMrf7bFkup+lsT+n2tRw2wbNknO1tQ0wICtqy2VqzWwLi45RBwTGB6DCB5QIBATA/MCsxKTAnBgNVBAMTIHRlc3RzdWJkb21haW4ubWV0YWRhdGEuYXp1cmUuY29tAhBnxW5Kh8dslEBA0E2mIBJ0MA0GCSqGSIb3DQEBCwUAMA0GCSqGSIb3DQEBAQUABIGAld1BM/yYIqqv8SDE4kjQo3Ul/IKAVR8ETKcve5BAdGSNkTUooUGVniTXeuvDj5NkmazOaKZp9fEtByqqPOyw/nlXaZgOO44HDGiPUJ90xVYmfeK6p9RpJBu6kiKhnnYTelUk5u75phe5ZbMZfBhuPhXmYAdjc7Nmw97nx8NnprQ="
}

簽章 Blob 是以 pkcs7 簽署的文件版本。 包含用來簽署的憑證以及特定 VM 專屬的詳細資料。

對於使用 Azure Resource Manager 建立的 VM,此文件包含 vmIdskunoncesubscriptionId、文件建立和到期的 timeStamp,以及映像的相關方案資訊。 Azure Marketplace 映像才會填入方案資訊。

對於使用傳統部署模型建立的 VM,只會保證填入 vmIdsubscriptionId。 您可以從回應擷取憑證,並且用來確認回應是有效的且來自 Azure。

解碼的文件包含下列欄位:

資料 描述 導入的版本
licenseType Azure Hybrid Benefit 的授權類型。 這僅適用於已啟用 AHB 的 VM。 2020-09-01
nonce 可以選擇性地隨要求提供的字串。 如果未提供 nonce,則會使用目前的 UTC 時間戳記。 2018-10-01
plan Azure Marketplace 映像方案。 包含方案識別碼 (名稱)、產品映像或供應項目 (產品),以及發行者識別碼 (發行者)。 2018-10-01
timestamp.createdOn 簽署文件建立時的 UTC 時間戳記 2018-20-01
timestamp.expiresOn 簽署文件到期時的 UTC 時間戳記 2018-10-01
vmId VM 的唯一識別碼 2018-10-01
subscriptionId 虛擬機器的 Azure 訂用帳戶 2019-04-30
sku VM 映像的特定 SKU (與執行個體中繼資料端點 [/metadata/instance] 的 compute/sku 屬性相互關聯) 2019-11-01

注意

對於傳統 (非 Azure Resource Manager) VM,只會保證填入 vmId。

範例文件︰

{
   "nonce":"20201130-211924",
   "plan":{
      "name":"planName",
      "product":"planProduct",
      "publisher":"planPublisher"
   },
   "sku":"Windows-Server-2012-R2-Datacenter",
   "subscriptionId":"8d10da13-8125-4ba9-a717-bf7490507b3d",
   "timeStamp":{
      "createdOn":"11/30/20 21:19:19 -0000",
      "expiresOn":"11/30/20 21:19:24 -0000"
   },
   "vmId":"02aab8a4-74ef-476e-8182-f6d2ba4166a6"
}

簽章驗證指引

驗證簽章時,您應該確認簽章使用來自 Azure 的憑證建立而成。 這可藉由驗證憑證的主體別名 (SAN) 來完成。

範例 SAN DNS Name=eastus.metadata.azure.com, DNS Name=metadata.azure.com

注意

公用雲端和各主權雲端的網域將會不同。

雲端 SAN 中的網域
所有正式推出的全域 Azure 區域 *.metadata.azure.com
Azure Government *.metadata.azure.us
由 21Vianet 營運的 Azure *.metadata.azure.cn
Azure Germany *.metadata.microsoftazure.de

注意

憑證可能與網域不完全相符。 基於這個理由,憑證驗證應該接受任何子網域 (例如,在公用雲端公開上市區域中接受 *.metadata.azure.com)。

我們不建議使用中繼憑證的憑證關聯。 如需進一步的指引,請參閱憑證關聯 - 憑證關聯和 Azure 服務。 請注意,Azure Instance Metadata Service 「不會」提供未來憑證授權單位變更的通知。 相反地,您必須遵循集中式 Azure 憑證授權單位詳細資料一文,取得所有未來的更新。

範例 1:驗證 VM 是在 Azure 中執行

Azure Marketplace 廠商想要確保其軟體是授權為只在 Azure 中執行。 如果有人將 VHD 複製到內部部署環境,廠商必須能夠偵測到該情況。 這些廠商可以透過 IMDS 取得簽署的資料,保證回應只來自 Azure。

注意

此範例需要安裝 jq 公用程式。

驗證

# Get the signature
$attestedDoc = Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri http://169.254.169.254/metadata/attested/document?api-version=2020-09-01
# Decode the signature
$signature = [System.Convert]::FromBase64String($attestedDoc.signature)

確認簽章來自 Microsoft Azure,並檢查憑證鏈結是否有錯誤。

# Get certificate chain
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]($signature)
$chain = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Chain
$chain.Build($cert)
# Print the Subject of each certificate in the chain
foreach($element in $chain.ChainElements)
{
    Write-Host $element.Certificate.Subject
}

# Get the content of the signed document
Add-Type -AssemblyName System.Security
$signedCms = New-Object -TypeName System.Security.Cryptography.Pkcs.SignedCms
$signedCms.Decode($signature);
$content = [System.Text.Encoding]::UTF8.GetString($signedCms.ContentInfo.Content)
Write-Host "Attested data: " $content
$json = $content | ConvertFrom-Json
# Do additional validation here

如果您在初始要求中提供 nonce 參數,則可以比較已簽署文件中的 nonce

受控識別

系統指派的受控識別可以在 VM 上啟用。 您也可以將一或多個使用者指派的受控識別指派給 VM。 然後,您可以從 IMDS 要求受控識別的權杖。 使用這些權杖向其他 Azure 服務進行驗證,例如 Azure Key Vault。

如需啟用這項功能的詳細步驟,請參閱取得存取權杖

Load Balancer 中繼資料

當您將虛擬機器或虛擬機器集執行個體放在 Azure Standard Load Balancer 後方時,您可以使用 IMDS 來擷取負載平衡器和執行個體相關的中繼資料。 如需詳細資訊,請參閱擷取負載平衡器資訊

排程的事件

您可以使用 IMDS 取得已排定事件的狀態。 然後,使用者可以指定要在這些事件上執行的一組動作。 如需詳細資訊,請參閱Linux 的已排定事件Windows 的已排定事件

不同語言的程式碼範例

下表列出在 VM 內使用不同語言呼叫 IMDS 的範例:

語言 範例
Bash https://github.com/Microsoft/azureimds/blob/master/IMDSSample.sh
C# https://github.com/Microsoft/azureimds/blob/master/IMDSSample.cs
Go https://github.com/Microsoft/azureimds/blob/master/imdssample.go
Java https://github.com/Microsoft/azureimds/blob/master/imdssample.java
NodeJS https://github.com/Microsoft/azureimds/blob/master/IMDSSample.js
Perl https://github.com/Microsoft/azureimds/blob/master/IMDSSample.pl
PowerShell https://github.com/Microsoft/azureimds/blob/master/IMDSSample.ps1
Puppet https://github.com/keirans/azuremetadata
Python https://github.com/Microsoft/azureimds/blob/master/IMDSSample.py
Ruby https://github.com/Microsoft/azureimds/blob/master/IMDSSample.rb

錯誤和偵錯

如果找不到資料元素或要求的格式錯誤,Instance Metadata Service 會傳回標準 HTTP 錯誤。 例如:

HTTP 狀態碼 原因
200 OK 要求成功。
400 Bad Request 查詢分葉節點時遺漏 Metadata: true 標頭或遺漏 format=json 參數
404 Not Found 要求的元素不存在
405 Method Not Allowed 端點不支援 HTTP 方法 (指令動詞)。
410 Gone 在一段時間後重試,上限為 70 秒
429 Too Many Requests 已超過 API 速率限制
500 Service Error 稍後重試

常見問題集

  • 我收到錯誤 400 Bad Request, Required metadata header not specified。 這表示什麼意思?

    • IMDS 需要在要求中傳遞標頭 Metadata: true。 在 REST 呼叫中傳遞此標頭可存取 IMDS。
  • 為什麼沒有收到我的 VM 計算資訊?

    • 目前 IMDS 僅支援使用 Azure Resource Manager 建立的執行個體。
  • 我先前已透過 Azure Resource Manager 建立 VM。 為什麼我看不到計算中繼資料資訊?

    • 如果您在 2016 年 9 月之後建立 VM,請新增標記以開始查看計算中繼資料。 如果您在 2016 年 9 月之前建立 VM,請新增或移除延伸模組或 VM 執行個體的資料磁碟,以重新整理中繼資料。
  • 使用者資料是否與自訂資料相同?

    • 使用者資料提供與自訂資料類似的功能,可讓您將自己的中繼資料傳遞至 VM 執行個體。 差別在於,使用者資料是透過 IMDS 擷取,而且在 VM 執行個體的存留期間會持續存在。 現有的自訂資料功能將繼續如本文所述運作。 不過,您只能透過本機系統資料夾取得自訂資料,而不能透過 IMDS 取得。
  • 為什麼看不到為新版本填入的所有資料?

    • 如果您在 2016 年 9 月之後建立 VM,請新增標記以開始查看計算中繼資料。 如果您在 2016 年 9 月之前建立 VM,請新增或移除延伸模組或 VM 執行個體的資料磁碟,以重新整理中繼資料。
  • 為什麼收到錯誤 500 Internal Server Error410 Resource Gone

    • 重試您的要求。 如需詳細資訊,請參閱暫時性錯誤處理。 如果問題持續發生,請在 Azure 入口網站中為 VM 建立支援問題。
  • 這是否適用於擴展集執行個體?

    • 是的,IMDS 適用於擴展集執行個體。
  • 我已更新擴展集中的標記,但是標記未出現在執行個體中 (與單一執行個體 VM 不同)。 我有哪裡做錯嗎?

    • 擴展集的標記目前只會在重新開機、重新安裝映像或磁碟變更為執行個體時向 VM 顯示。
  • 為什麼在 instance/compute 詳細資料中看不到 VM 的 SKU 資訊?

    • 針對從 Azure Marketplace 建立的自訂映像,Azure 平台不會保留自訂映像的 SKU 資訊,以及從自訂映像建立的任何 VM 詳細資料。 這是設計所致,因此不會顯示在 VM instance/compute 詳細資料中。
  • 為什麼呼叫服務的要求逾時 (或無法連線)?

    • 中繼資料呼叫必須從指派給 VM 主要網路卡的主要 IP 位址進行。 此外,如果您已變更路由,則您 VM 的本機路由表中必須有 169.254.169.254/32 位址的路由。

      1. 傾印本機路由表並尋找 IMDS 項目。 例如:

        route print
        
        IPv4 Route Table
        ===========================================================================
        Active Routes:
        Network Destination        Netmask          Gateway       Interface  Metric
                0.0.0.0          0.0.0.0      172.16.69.1      172.16.69.7     10
                127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
                127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
        127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
            168.63.129.16  255.255.255.255      172.16.69.1      172.16.69.7     11
        169.254.169.254  255.255.255.255      172.16.69.1      172.16.69.7     11
        ... (continues) ...
        
      2. 確認 169.254.169.254 的路由存在,並記下對應的網路介面 (例如,172.16.69.7)。

      3. 傾印介面組態,並尋找對應至路由表中所參考的介面,記下 MAC (實體) 位址。

        ipconfig /all
        
        ... (continues) ...
        Ethernet adapter Ethernet:
        
        Connection-specific DNS Suffix  . : xic3mnxjiefupcwr1mcs1rjiqa.cx.internal.cloudapp.net
        Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
        Physical Address. . . . . . . . . : 00-0D-3A-E5-1C-C0
        DHCP Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
        Link-local IPv6 Address . . . . . : fe80::3166:ce5a:2bd5:a6d1%3(Preferred)
        IPv4 Address. . . . . . . . . . . : 172.16.69.7(Preferred)
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        ... (continues) ...
        
      4. 確認介面對應至 VM 的主要 NIC 和主要 IP。 您可以在 Azure 入口網站中或使用 Azure CLI 查看網路組態,來尋找主要 NIC 和 IP。 記下私人 IP (如果您使用 CLI 則同時記下 MAC 位址)。 下列是 PowerShell CLI 範例:

        $ResourceGroup = '<Resource_Group>'
        $VmName = '<VM_Name>'
        $NicNames = az vm nic list --resource-group $ResourceGroup --vm-name $VmName | ConvertFrom-Json | Foreach-Object { $_.id.Split('/')[-1] }
        foreach($NicName in $NicNames)
        {
            $Nic = az vm nic show --resource-group $ResourceGroup --vm-name $VmName --nic $NicName | ConvertFrom-Json
            Write-Host $NicName, $Nic.primary, $Nic.macAddress
        }
        
        wintest767 True 00-0D-3A-E5-1C-C0
        
      5. 如果不相符,請更新路由表以便將主要 NIC 和 IP 設為目標。


  • Windows Server 中的容錯移轉叢集

    • 當您使用容錯移轉叢集查詢 IMDS 時,有時必須將路由新增至路由表。 方法如下:

      1. 使用系統管理員權限開啟命令提示字元。

      2. 執行下列命令,並記下 IPv4 路由表中的網路目的地介面 (0.0.0.0) 位址。

      route print
      

      注意

      下列範例輸出來自已啟用容錯移轉叢集的 Windows Server VM。 為了簡單起見,輸出只包含 IPv4 路由表。

      IPv4 Route Table
      ===========================================================================
      Active Routes:
      Network Destination        Netmask          Gateway       Interface  Metric
              0.0.0.0          0.0.0.0         10.0.1.1        10.0.1.10    266
              10.0.1.0  255.255.255.192         On-link         10.0.1.10    266
              10.0.1.10  255.255.255.255         On-link         10.0.1.10    266
              10.0.1.15  255.255.255.255         On-link         10.0.1.10    266
              10.0.1.63  255.255.255.255         On-link         10.0.1.10    266
              127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
              127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
      127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
          169.254.0.0      255.255.0.0         On-link     169.254.1.156    271
          169.254.1.156  255.255.255.255         On-link     169.254.1.156    271
      169.254.255.255  255.255.255.255         On-link     169.254.1.156    271
              224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
              224.0.0.0        240.0.0.0         On-link     169.254.1.156    271
      255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
      255.255.255.255  255.255.255.255         On-link     169.254.1.156    271
      255.255.255.255  255.255.255.255         On-link         10.0.1.10    266
      

      執行下列命令,並使用網路目的地介面 (0.0.0.0) 的位址,在此範例中為 (10.0.1.10)。

      route add 169.254.169.254/32 10.0.1.10 metric 1 -p
      

支援

如果您在多次嘗試之後仍無法取得中繼資料回應,您可以在 Azure 入口網站中提出支援問題。

產品意見反應

您可以在這裡的虛擬機器 > Instance Metadata Service 底下,向我們的使用者意見反應通道提供產品意見反應和想法

下一步