使用 REST API 呼叫在虛擬機器擴展集上設定 Azure 資源的受控識別

Azure 資源的受控識別會在 Microsoft Entra ID 中提供自動受控系統識別的 Azure 服務。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。

在本文中,您將瞭解如何使用 CURL 對 Azure Resource Manager REST 端點進行呼叫,以針對虛擬機器擴展集上的 Azure 資源作業執行下列受控識別:

  • 在 Azure 虛擬機器擴展集上啟用和停用系統指派的受控識別
  • 在 Azure 虛擬機器擴展集上新增和移除使用者指派的受控識別

如果您還沒有 Azure 帳戶, 請先註冊免費帳戶 ,再繼續進行。

必要條件

系統指派的受控識別

在本節中,您會瞭解如何使用 CURL 在虛擬機器擴展集上啟用和停用系統指派的受控識別,以呼叫 Azure Resource Manager REST 端點。

在建立虛擬機器擴展集期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的虛擬機器擴展集,您需要建立虛擬機器擴展集並擷取存取權杖,以使用 CURL 呼叫具有系統指派受控識別類型值的 Resource Manager 端點。

  1. 使用 az group create ,為 虛擬機器擴展集及其相關資源的內含專案和部署建立資源群組 。 如果您已經有想要改用的資源群組,則可以略過此步驟:

    az group create --name myResourceGroup --location westus
    
  2. 為您的虛擬機器擴展集建立 網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  4. 使用 Azure Cloud Shell,使用 CURL 建立虛擬機器擴展集來呼叫 Azure Resource Manager REST 端點。 下列範例會使用系統指派的受控識別,在 myResourceGroup 建立名為 myVMSS 的 虛擬機器擴展集,如 值 "identity":{"type":"SystemAssigned"} 在要求主體中識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取權杖 <SUBSCRIPTION ID> 和適合您環境的值。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"SystemAssigned"},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"SystemAssigned"
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }  
    

在現有的虛擬機器擴展集上啟用系統指派的受控識別

若要在現有的虛擬機器擴展集上啟用系統指派的受控識別,您需要取得存取權杖,然後使用 CURL 呼叫 Resource Manager REST 端點來更新身分識別類型。

  1. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,在虛擬機器擴展集上啟用系統指派的受控識別,如要求主體中所識別的虛擬機器擴展集值 {"identity":{"type":"SystemAssigned"} ,名為 myVMSS 。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取權杖 <SUBSCRIPTION ID> 和適合您環境的值。

    重要

    若要確保您不會刪除任何指派給虛擬機器擴展集的現有使用者指派受控識別,您必須使用此 CURL 命令列出使用者指派的受控識別: curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>" 。 如果您已將任何使用者指派的受控識別指派給回應中的值所 identity 識別的虛擬機器擴展集,請跳至步驟 3,示範如何在虛擬機器擴展集上啟用系統指派的受控識別時保留使用者指派的受控識別。

     curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned"
        }
     }
    
  3. 若要在具有現有使用者指派受控識別的虛擬機器擴展集上啟用系統指派的受控識別,您必須將 新增 SystemAssignedtype 值。

    例如,如果您的虛擬機器擴展集具有使用者指派的受控識別 ID1ID2 指派給它,而且您想要將系統指派的受控識別新增至虛擬機器擴展集,請使用下列 CURL 呼叫。 將 和 <SUBSCRIPTION ID> 取代 <ACCESS TOKEN> 為適合您環境的值。

    API 版本 2018-06-01 會將使用者指派的受控識別儲存在 userAssignedIdentities 字典格式的值中,而不是 identityIds API 版本 2017-12-01 中使用的陣列格式值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從虛擬機器擴展集停用系統指派的受控識別

若要在現有的虛擬機器擴展集上停用系統指派的身分識別,您需要取得存取權杖,然後使用 CURL 呼叫 Resource Manager REST 端點,將身分識別類型更新為 None

  1. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用 CURL 更新虛擬機器擴展集,以呼叫 Azure Resource Manager REST 端點,以停用系統指派的受控識別。 下列範例會藉由名為 myVMSS 的虛擬機器擴展集的值 {"identity":{"type":"None"}} ,停用要求主體中所識別的系統指派受控識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取權杖 <SUBSCRIPTION ID> 和適合您環境的值。

    重要

    若要確保您不會刪除任何指派給虛擬機器擴展集的現有使用者指派受控識別,您必須使用此 CURL 命令列出使用者指派的受控識別: curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>" 。 如果您有指派給虛擬機器擴展集的任何使用者指派受控識別,請跳至步驟 3,說明如何在從虛擬機器擴展集移除系統指派的受控識別時保留使用者指派的受控識別。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"None"
        }
     }
    

    若要從具有使用者指派受控識別的虛擬機器擴展集移除系統指派的受控識別,請在使用 API 2018-06-01 版時 UserAssigned ,從 {"identity":{"type:" "}} 值中移除 SystemAssigned 值和 userAssignedIdentities 字典值。 如果您使用 API 2017-12-01 版或更早版本,請保留 identityIds 陣列。

使用者指派的受控識別

在本節中,您將瞭解如何使用 CURL 在虛擬機器擴展集上新增和移除使用者指派的受控識別,以呼叫 Azure Resource Manager REST 端點。

在建立虛擬機器擴展集期間指派使用者指派的受控識別

  1. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  2. 為您的虛擬機器擴展集建立 網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  4. 使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別

  5. 使用 CURL 建立虛擬機器擴展集來呼叫 Azure Resource Manager REST 端點。 下列範例會使用使用者指派的受控識別 ID1 ,在資源群組 myResourceGroup 中建立名為 myVMSS 的 虛擬機器擴展集,如 值 "identity":{"type":"UserAssigned"} 在要求本文中識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取權杖 <SUBSCRIPTION ID> 和適合您環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"UserAssigned","userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{}}},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              }
           }
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"UserAssigned","identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }
    

將使用者指派的受控識別指派給現有的 Azure 虛擬機器擴展集

  1. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別

  3. 若要確保您不會刪除指派給虛擬機器擴展集的現有使用者或系統指派受控識別,您必須使用下列 CURL 命令列出指派給虛擬機器擴展集的識別類型。 如果您已將受控識別指派給虛擬機器擴展集,則會列在 值中 identity

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    授權 必要。 設定為有效的 Bearer 存取權杖。
  4. 如果您沒有指派給虛擬機器擴展集的任何使用者或系統指派受控識別,請使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,將第一個使用者指派的受控識別指派給虛擬機器擴展集。 如果您有指派給虛擬機器擴展集的使用者或系統指派受控識別,請跳至步驟 5,示範如何將多個使用者指派的受控識別新增至虛擬機器擴展集,同時維護系統指派的受控識別。

    下列範例會將使用者指派的受控識別 ID1 指派給資源群組 myResourceGroup 中名為 myVMSS 的 虛擬機器擴展集。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取權杖 <SUBSCRIPTION ID> 和適合您環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-12-01' -X PATCH -d '{"identity":{"type":"userAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"userAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"userAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"userAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    
  5. 如果您有指派給虛擬機器擴展集的現有使用者指派或系統指派的受控識別:

    API 版本 2018-06-01

    將使用者指派的受控識別新增至 userAssignedIdentities 字典值。

    例如,如果您有系統指派的受控識別,以及目前指派給虛擬機器規模的使用者指派受控識別 ID1 ,而且想要將使用者指派的受控識別 ID2 新增至虛擬機器:

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    在新增使用者指派的受控識別時,保留您想要保留在陣列值中的 identityIds 使用者指派受控識別。

    例如,如果您有系統指派的身分識別和目前指派給虛擬機器擴展集的使用者指派受控識別 ID1 ,而且想要將使用者指派的受控識別 ID2 新增至虛擬機器擴展集:

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從虛擬機器擴展集移除使用者指派的受控識別

  1. 擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立虛擬機器擴展集。

    az account get-access-token
    
  2. 若要確保您不會刪除您想要繼續指派給虛擬機器擴展集或移除系統指派受控識別的任何現有使用者指派受控識別,您必須使用下列 CURL 命令列出受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>" 
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    授權 必要。 設定為有效的 Bearer 存取權杖。

    如果您已將受控識別指派給 VM,則會列在 值中的回應中 identity

    例如,如果您有使用者指派的受控識別 ID1ID2 指派給虛擬機器擴展集,而且您只想保留 ID1 指派並保留系統指派的受控識別:

    API 版本 2018-06-01

    將 新增 null 至您想要移除的使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null
           }
        }
     }
    

    API 版本 2017-12-01

    只保留您想要保留在陣列中的 identityIds 使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    

如果您的虛擬機器擴展集同時具有系統指派和使用者指派的受控識別,您可以切換為僅使用下列命令來移除所有使用者指派的受控識別:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取權杖。

要求本文

{
   "identity":{
      "type":"SystemAssigned"
   }
}

如果您的虛擬機器擴展集只有使用者指派的受控識別,而且您想要將其全部移除,請使用下列命令:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取權杖。

要求本文

{
   "identity":{
      "type":"None"
   }
}

下一步

如需如何使用 REST 建立、列出或刪除使用者指派的受控識別的相關資訊,請參閱: