你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Microsoft Entra ID 在 Azure 中进行身份验证

Microsoft Entra ID 服务支持多种管理任务,例如用户管理、域管理和单一登录配置。 本文介绍如何使用 Azure 自动化中的 Microsoft Entra ID 作为向 Azure 进行身份验证的提供程序。

安装 Microsoft Entra 模块

可以通过以下 PowerShell 模块启用 Microsoft Entra ID:

  • 用于 Graph 的 Azure Active Directory PowerShell。 Azure 自动化随 Az 模块一起提供。 功能包括使用基于 Microsoft Entra 用户 (OrgId) 凭据的身份验证向 Azure 进行非交互式身份验证。 请参阅 Azure AD 2.0.2.182

  • Microsoft Windows PowerShell 的 Entra ID。 此模块可启用与 Microsoft Online(包括 Microsoft 365)的交互。

对 PSCredential 的安装支持

Azure Automation uses the PSCredential class to represent a credential asset. 脚本使用 PSCredential cmdlet 检索 Get-AutomationPSCredential 对象。 有关详细信息,请参阅 Azure 自动化中的凭据资产

分配订阅管理员

必须为 Azure 订阅分配一个管理员。 此管理员拥有订阅范围的所有者角色。 请参阅 Azure 自动化中基于角色的访问控制

更改 Microsoft Entra 用户的密码

若要更改 Microsoft Entra 用户的密码,请执行以下操作:

  1. 从 Azure 注销。

  2. 让管理员使用完整的用户名(包括域)和临时密码,以刚刚创建的 Microsoft Entra 用户身份登录到 Azure。

  3. 让管理员在出现提示时更改密码。

配置 Azure 自动化以管理 Azure 订阅

要使 Azure 自动化能够与 Microsoft Entra ID 通信,你必须检索与 Azure 和 Microsoft Entra ID 之间的连接相关联的凭据。 这些凭据的示例包括租户 ID、订阅 ID 等。 有关 Azure 与 Microsoft Entra ID 之间的连接的详细信息,请参阅将组织连接到 Microsoft Entra ID

创建凭据资产

获取 Microsoft Entra 的 Azure 凭据后,可以创建一个用于安全存储 Microsoft Entra 凭据的 Azure 自动化凭据资产,以便 Runbook 和 Desired State Configuration (DSC) 脚本可以访问这些凭据。 可以使用 Azure 门户或 PowerShell cmdlet 来执行此操作。

在 Azure 门户中创建凭据资产

可以使用 Azure 门户来创建凭据资产。 Do this operation from your Automation account using Credentials under Shared Resources. 请参阅 Azure 自动化中的凭据资产

使用 Windows PowerShell 创建凭据资产

若要在 Windows PowerShell 中准备新的凭据资产,脚本应首先使用分配的用户名和密码创建 PSCredential 对象。 The script then uses this object to create the asset through a call to the New-AzureAutomationCredential cmdlet. Alternatively, the script can call the Get-Credential cmdlet to prompt the user to type in a name and password. 请参阅 Azure 自动化中的凭据资产

通过 Azure 自动化 runbook 管理 Azure 资源

可以使用凭据资产通过 Azure 自动化 runbook 管理 Azure 资源。 下面是一个示例 PowerShell runbook,它收集用于停止和启动 Azure 订阅中的虚拟机的凭据资产。 此 runbook 首先使用 Get-AutomationPSCredential 来检索用于向 Azure 进行身份验证的凭据。 It then calls the Connect-AzAccount cmdlet to connect to Azure using the credential.

Workflow Workflowname
{ 
    Param 
    (    
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
        [String] 
        $AzureSubscriptionId, 
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
        [String] 
        $AzureVMList="All", 
        [Parameter(Mandatory=$true)][ValidateSet("Start","Stop")] 
        [String] 
        $Action 
    ) 
     
    # 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 

    # get credential
    $credential = Get-AutomationPSCredential -Name "AzureCredential"

    # Connect to Azure with credential
    $AzureContext = (Connect-AzAccount -Credential $credential -TenantId $AzureContext.Subscription.TenantId).context 

    # set and store context
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
        -TenantId $AzureContext.Subscription.TenantId `
        -DefaultProfile $AzureContext
 
    if($AzureVMList -ne "All") 
    { 
        $AzureVMs = $AzureVMList.Split(",") 
        [System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs 
    } 
    else 
    { 
        $AzureVMs = (Get-AzVM -DefaultProfile $AzureContext).Name 
        [System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs 
    } 
 
    foreach($AzureVM in $AzureVMsToHandle) 
    { 
        if(!(Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM})) 
        { 
            throw " AzureVM : [$AzureVM] - Does not exist! - Check your inputs " 
        } 
    } 
 
    if($Action -eq "Stop") 
    { 
        Write-Output "Stopping VMs"; 
        foreach -parallel ($AzureVM in $AzureVMsToHandle) 
        { 
            Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM} | Stop-AzVM -DefaultProfile $AzureContext -Force 
        } 
    } 
    else 
    { 
        Write-Output "Starting VMs"; 
        foreach -parallel ($AzureVM in $AzureVMsToHandle) 
        { 
            Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM} | Start-AzVM -DefaultProfile $AzureContext
        } 
    } 
}

将 Microsoft Graph 与 Powershell 配合使用

请参阅 Microsoft Graph PowerShell SDK 入门

Next steps