如何在 Azure 中自动执行 PIM 登录?

How to automate PIM login in Azure?

如何在 Azure 中自动执行 PIM 登录。每天激活登录需要 3-5 分钟,有效期为 8 小时,因此无论何时需要,我都需要重新激活它。请建议 PIM 登录的任何自动化方法。

要自动激活 PIM,您可以按照以下步骤操作。

1.Create automation account(need to create Run As account) and runbook(powershell 类型).

2.Navigate 到门户中的自动化帐户-> Modules gallery -> 搜索 AzureADPreview 模块并导入它。

3.Follow 这个 docGlobal Administrator 分配给 运行 作为帐户即服务主体,只需搜索您的自动化帐户的名称,名称服务主体的格式为 automationaccount_xxxxxxxx.

4.In 您的 Runbook,使用下面的脚本使用服务主体登录,使用 Open-AzureADMSPrivilegedRoleAssignmentRequest 为您需要的用户激活符合条件的分配。

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Connect-AzureAD `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

    Open-AzureADMSPrivilegedRoleAssignmentRequest -ProviderId 'aadRoles' -ResourceId '926d99e7-117c-4a6a-8031-0cc481e9da26' -RoleDefinitionId 'f55a9a68-f424-41b7-8bee-cee6a442d418' -SubjectId 'f7d1887c-7777-4ba3-ba3d-974488524a9d' -Type 'UserAdd' -AssignmentState 'Active' -schedule $schedule -reason "dsasdsas"

}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

5.Navigate 到门户中的运行手册 -> 计划 -> 创建和 link 运行手册的重复计划,例如每一天,细节取决于你。