Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
本教程介绍如何在 Azure 自动化中创建 PowerShell Runbook,该 Runbook 使用 托管标识 与资源交互。 PowerShell Runbook 是基于 Windows PowerShell 的。 借助 Microsoft Entra ID 的托管标识,runbook 可以轻松访问其他受 Microsoft Entra 保护的资源。
本教程介绍如何执行下列操作:
- 向托管身份分配权限
- 创建 PowerShell 运行手册
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
先决条件
在向托管标识分配权限之前,请确保满足以下先决条件:
- 至少具有一个用户分配的托管标识的 Azure 自动化帐户。 有关详细信息,请参阅对 Azure 自动化帐户使用用户分配的托管标识。
- 导入到自动化帐户中的 Az 模块:
Az.Accounts、Az.Automation、Az.ManagedServiceIdentity和Az.Compute。 有关详细信息,请参阅导入 Az 模块。 - 在计算机上安装 Azure Az PowerShell 模块。 若要安装或升级,请参阅如何安装 Azure Az PowerShell 模块。
Az.ManagedServiceIdentity是预览模块,不作为 Az 模块的一部分安装。 若要安装该模块,请运行Install-Module -Name Az.ManagedServiceIdentity。 - Azure 虚拟机。 因为这个虚拟机需要频繁停止和启动,所以不适合用作生产环境的 VM。
- 大致熟悉 自动化运行手册。
向托管身份分配权限
向托管标识分配权限,以允许它们停止和启动虚拟机。
若要向托管标识分配权限,请执行以下步骤:
使用 Connect-AzAccount cmdlet 以交互方式登录到 Azure,并按照说明作:
# Sign in to your Azure subscription $sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not ($sub)) { Connect-AzAccount -Subscription -Environment AzureChinaCloud } # If you have multiple subscriptions, set the one to use # Select-AzSubscription -SubscriptionId <SUBSCRIPTIONID>为下面的变量输入适当的值,然后执行脚本。
$resourceGroup = "resourceGroupName" # These values are used in this tutorial $automationAccount = "xAutomationAccount" $userAssignedManagedIdentity = "xUAMI"使用 PowerShell cmdlet New-AzRoleAssignment 将角色分配给系统分配的托管标识。
$role1 = "DevTest Labs User" $SAMI = (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccount).Identity.PrincipalId New-AzRoleAssignment ` -ObjectId $SAMI ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName $role1用户指定的托管身份需要相同的角色分配。
$UAMI = (Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup -Name $userAssignedManagedIdentity).PrincipalId New-AzRoleAssignment ` -ObjectId $UAMI ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName $role1要执行本教程中使用的 cmdlet
Get-AzUserAssignedIdentity和Get-AzAutomationAccount,系统分配的托管标识需要获得额外权限。$role2 = "Reader" New-AzRoleAssignment ` -ObjectId $SAMI ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName $role2
创建 PowerShell 运维脚本
创建一个允许由任一托管标识执行的 runbook。 Runbook 将启动已停止的 VM 或停止正在运行的 VM。
若要创建 PowerShell Runbook,请执行以下步骤:
登录 Azure 门户,导航到你的自动化帐户。
在“ 概述 ”页中,选择“ 试用运行时环境”体验(如果尚未在新体验中)。
在“过程自动化”下,选择“Runbook”。
选择“创建 Runbook”并执行以下操作:
- 将 runbook 命名为
miTesting。 - 从“Runbook 类型”下拉列表中,选择“PowerShell”。
- 在 “运行时环境 ”下拉列表 中,选择现有 运行时环境或使用运行时 PowerShell 和版本 7.4 创建新 环境。
- 输入适用的“描述”。
- 将 runbook 命名为
选择“创建”以创建 runbook。
在 runbook 编辑器中粘贴以下代码:
Param( [string]$ResourceGroup, [string]$VMName, [string]$Method, [string]$UAMI ) $automationAccount = "xAutomationAccount" # Ensures you do not inherit an AzContext in your runbook $null = Disable-AzContextAutosave -Scope Process # Connect using a Managed Service Identity try { $AzureConnection = (Connect-AzAccount -Identity).context } catch { Write-Output "There is no system-assigned user identity. Aborting." exit } # set and store context $AzureContext = Set-AzContext -SubscriptionName $AzureConnection.Subscription -DefaultProfile $AzureConnection if ($Method -eq "SA") { Write-Output "Using system-assigned managed identity" } elseif ($Method -eq "UA") { Write-Output "Using user-assigned managed identity" # Connects using the Managed Service Identity of the named user-assigned managed identity $identity = Get-AzUserAssignedIdentity -ResourceGroupName $ResourceGroup -Name $UAMI -DefaultProfile $AzureContext # validates assignment only, not perms $AzAutomationAccount = Get-AzAutomationAccount -ResourceGroupName $ResourceGroup -Name $automationAccount -DefaultProfile $AzureContext if ($AzAutomationAccount.Identity.UserAssignedIdentities.Values.PrincipalId.Contains($identity.PrincipalId)) { $AzureConnection = (Connect-AzAccount -Identity -AccountId $identity.ClientId).context # set and store context $AzureContext = Set-AzContext -SubscriptionName $AzureConnection.Subscription -DefaultProfile $AzureConnection } else { Write-Output "Invalid or unassigned user-assigned managed identity" exit } } else { Write-Output "Invalid method. Choose UA or SA." exit } # Get current state of VM $status = (Get-AzVM -ResourceGroupName $ResourceGroup -Name $VMName -Status -DefaultProfile $AzureContext).Statuses[1].Code Write-Output "`r`n Beginning VM status: $status `r`n" # Start or stop VM based on current state if ($status -eq "Powerstate/deallocated") { Start-AzVM -Name $VMName -ResourceGroupName $ResourceGroup -DefaultProfile $AzureContext } elseif ($status -eq "Powerstate/running") { Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroup -DefaultProfile $AzureContext -Force } # Get new state of VM $status = (Get-AzVM -ResourceGroupName $ResourceGroup -Name $VMName -Status -DefaultProfile $AzureContext).Statuses[1].Code Write-Output "`r`n Ending VM status: $status `r`n `r`n" Write-Output "Account ID of current context: " $AzureContext.Account.Id在编辑器中的第 8 行上,根据需要修改
$automationAccount变量的值。选择“保存”,然后选择“测试窗格” 。
使用适当的值填充参数
RESOURCEGROUP和VMNAME。 对于SA参数输入METHOD,对于xUAMI参数输入UAMI。 该 runbook 会尝试使用系统分配的托管标识更改 VM 的电源状态。选择“启动”。 该运行手册完成后,输出应类似于以下内容:
Beginning VM status: PowerState/deallocated OperationId : 5b707401-f415-4268-9b43-be1f73ddc54b Status : Succeeded StartTime : 8/3/2021 10:52:09 PM EndTime : 8/3/2021 10:52:50 PM Error : Name : Ending VM status: PowerState/running Account ID of current context: MSI@50342将
METHOD参数的值更改为UA。选择“启动”。 该运行手册将尝试通过用户指定的分配托管标识来更改您的 VM 的电源状态。 runbook 执行完成后,输出应类似于下述:
Using user-assigned managed identity Beginning VM status: PowerState/running OperationId : 679fcadf-d0b9-406a-9282-66bc211a9fbf Status : Succeeded StartTime : 8/3/2021 11:06:03 PM EndTime : 8/3/2021 11:06:49 PM Error : Name : Ending VM status: PowerState/deallocated Account ID of current context: 9034f5d3-c46d-44d4-afd6-c78aeab837ea
清理资源
若要删除不再需要的任何资源,请运行以下 Runbook:
#Remove runbook
Remove-AzAutomationRunbook `
-ResourceGroupName $resourceGroup `
-AutomationAccountName $automationAccount `
-Name "miTesting" `
-Force
# Remove role assignments
Remove-AzRoleAssignment `
-ObjectId $UAMI `
-ResourceGroupName $resourceGroup `
-RoleDefinitionName $role1
Remove-AzRoleAssignment `
-ObjectId $SAMI `
-ResourceGroupName $resourceGroup `
-RoleDefinitionName $role2
Remove-AzRoleAssignment `
-ObjectId $SAMI `
-ResourceGroupName $resourceGroup `
-RoleDefinitionName $role1
后续步骤
在本教程中,你在 Azure 自动化中创建了一个PowerShell runbook,它使用 托管标识(而不是运行方式帐户)与资源进行交互。 有关 PowerShell Workflow 运行手册的信息,请参阅:
- 若要在 PowerShell 运行手册创建期间排查托管标识相关问题,请参阅 排查 Azure 自动化 托管标识问题。