共用方式為


Azure 自動化中的內容切換

環境切換是指某個程序中的環境改變了另一個程序中的環境。 Azure 內容是一組資訊,可定義 Azure PowerShell Cmdlet 的目標。 內容包含下列屬性:

屬性 描述
Name 內容的名稱。
客戶​​ 與 Azure 通訊時用於驗證的使用者名稱或服務主體。
Environment 代表 Azure 全域或其中一個國家 Azure 雲端,例如 Azure Government。 您也可以指定混合式雲端平台,例如 Azure Stack。
訂用帳戶 代表 Azure 訂用帳戶 (其中包含您想要管理的資源)。
租用戶 代表單一組織的專用且受信任的 Microsoft Entra ID 執行個體。
認證 Azure 用來確認您的身分識別,並確定您有權對 Azure 中的資源進行存取的資訊。

當一個可以存取數個訂用帳戶的帳戶登入時,這些訂用帳戶中的任何一個都可以新增至使用者的環境。 若要保證正確的訂用帳戶,必須在連線時宣告它。 例如,使用 Add-AzAccount -Credential $Cred -subscription 'cd4dxxxx-xxxx-xxxx-xxxx-xxxxxxxx9749'。 不過,若管理某個訂用帳戶的 Runbook 與管理相同自動化帳戶中另一個訂用帳戶所含資源的其他 Runbook 執行於相同的沙箱程序中,就可能會發生問題。 對某個 Runbook 所做的內容變更會影響使用預設內容的其他 Runbook。 因為內容中含有要使用的認證以及要作為目標的訂用帳戶等資訊,Cmdlet 可能會將目標設為錯誤的訂用帳戶,而導致 not found 或權限錯誤。 此問題稱為內容切換

管理 Azure 內容

若要避免 Runbook 對錯誤的訂用帳戶中的資源執行,請檢閱下列建議:

  1. 在每個 Runbook 的開頭使用下列命令,藉以停用在自動化 Runbook 內儲存沙箱內容的功能:Disable-AzContextAutosave -Scope Process
  2. Azure PowerShell Cmdlet 支援 -DefaultProfile 參數。 此參數已新增至所有 Az 和 Azure Resource Manager (AzureRM) Cmdlet,以支援在相同程序中執行多個指令碼,讓您可以為每個 Cmdlet 指定要使用的內容。 在建立內容物件以及每次變更時,都將其儲存在 Runbook 中。 然後在使用 Az 或 AzureRM Cmdlet 進行的每個呼叫中引用它。 例如: $AzureContext = Set-AzContext -SubscriptionId $subID
  3. 將內容物件傳遞至 PowerShell Cmdlet,例如 Get-AzVM -ResourceGroupName "myGroup" -DefaultProfile $AzureContext

以下 PowerShell Runbook 程式碼片段使用系統指派的受控識別,其後提供如何避免內容切換的建議。

# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process

# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext

# Pass context object - even though the context had just been set
# This is the step that guarantees the context will not be switched.
Get-AzVM -ResourceGroupName "resourceGroupName" -DefaultProfile $AzureContext | Select Name

可能的徵狀

雖然未採行這些建議不一定會遇到問題,但還是有可能的。 這種情況的根本問題是時機;這取決於個別 Runbook 在另一個 Runbook 切換其內容時正在做什麼。 以下是一些可能的錯誤訊息。 不過,這些錯誤訊息有可能是由非內容切換情況造成的。

The subscription named <subscription name> cannot be found.

Get-AzVM : The client '<clientid>' with object id '<objectid>' does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/<subcriptionIdOfSubscriptionWichDoesntContainTheVM>/resourceGroups/REsourceGroupName/providers/Microsoft.Compute/virtualMachines/VMName '.
   ErrorCode: AuthorizationFailed
   StatusCode: 403
   ReasonPhrase: Forbidden Operation
   ID : <AGuidRepresentingTheOperation> At line:51 char:7 + $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $UNBV... +
Get-AzureRmResource : Resource group "SomeResourceGroupName" could not be found.
... resources = Get-AzResource -ResourceGroupName $group.ResourceGro ...
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzResource], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet

下一步