2

使用新的 Azure 门户,我正在尝试添加一个将启动特定 VM 的 powershell runbook。这不是我的 PC 在 powershell 中运行的东西,而是作为 ARM 作业运行。我似乎找不到成功登录的方法。

如果在 powershell 中从我的桌面运行,我可以调用 Login-AzureRmAccount,它会在运行任何进一步的步骤之前启动一个登录对话框。从我在网上读到的内容看来,我需要做的是向我的自动化帐户添加一个凭据,检索它,然后调用相同的登录方法。我现在已经这样做了,但仍然无法登录。

Import-Module AzureRM.Compute
$AutomationCredentialAssetName = "automation"
$Cred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName
Write-Output $Cred
Login-AzureRmAccount -Credential $Cred
Start-AzureRmVm -Name 'myvmname' -ResourceGroupName 'myresourcegroupname'

正在正确检索凭据(获取写入输出),但对 Login-AzureRmAccount 的调用失败,并显示:

Login-AzureRmAccount : unknown_user_type: Unknown User Type At line:10 char:1 + Login-AzureRmAccount -Credential $Cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Add-AzureRmAccount], AadAuthenticationFailedException + FullyQualifiedErrorId : Microsoft.Azure.Common.Authentication.AadAuthenticationFailedException,Microsoft.Azure.Commands.Profile。添加AzureRMAccount命令

如果我不尝试先登录,我会收到一条消息,告诉我先调用 Login-AzureRmAccount。

如何从 Runbook 中进行身份验证,以便我可以运行自动化任务?我究竟做错了什么?

4

4 回答 4

8

我们随后发现自动化帐户在创建时创建了一个可用于登录的连接:

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

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint   $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
} 
于 2016-07-07T10:51:32.783 回答
1

猜测您正在尝试使用 Microsoft 帐户登录,这只能以交互方式完成(因为它需要通过 live.com 重定向)。您需要在要对其进行身份验证的租户 (Active Directory) 中创建一个用户,以便非交互式登录正常工作。

完成这项工作的最简单方法是在旧门户中创建一个帐户(新门户尚不支持 Active Directory 管理),然后在设置 > 管理员中将该用户添加为共同管理员。

您可以通过 Powershell 创建用户,并分配更精细的权限,但是当您按照自己的方式解决问题时,留在门户中可能更容易。

通过旧门户创建的用户与通过 AzureRm 命令创建的用户之间没有显着差异。

于 2016-01-21T21:21:23.237 回答
0

我刚刚遇到了同样的问题,虽然这里发布的信息很有帮助,但并没有完全解决问题。

我需要的关键见解是,为了使用 Azure Cmdlet,必须配置一个“以帐户身份运行”。(请参阅https://docs.microsoft.com/en-us/azure/automation/automation-sec-configure-azure-runas-accountAccount Settings )可以在 azure 自动化帐户部分下进行设置。

拥有“以帐户身份运行”后,您可以使用 BlackSpy 提出的方法登录。即:

# Get the connection
$servicePrincipalConnection = Get-AutomationConnection -Name AzureRunAsConnection         

"Logging in to Azure..."
Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint   $servicePrincipalConnection.CertificateThumbprint 

希望这可能对某人有所帮助。

于 2017-02-24T12:44:57.223 回答
0

官方建议是使用 ServicePrincipal 进行自动化 - 您可以将 Secret 或 Certificate 凭证与服务主体一起使用,并且证书效果最好。

仍然可以使用工作或学校帐户进行自动登录(仅使用 -Credential 登录),但这要求您的组织不需要双重身份验证。不幸的是,不能为此使用 Microsoft 帐户 - microsoft 帐户需要用户交互才能进行任何登录。

于 2017-06-17T21:35:08.750 回答