[英]How do you import runbook to azure resource group from the command line
[英]How to login from an Azure Resource Manager Runbook?
使用新的Azure門戶,我試圖添加將啟動特定VM的Powershell Runbook。 這不是可以從我的PC在Powershell中運行的東西,而是作為ARM作業運行的。 我似乎找不到成功登錄的方法。
如果在Powershell中從桌面運行,我可以調用Login-AzureRmAccount,它將在運行任何其他步驟之前啟動登錄對話框。 從我在網絡上閱讀的內容來看,我似乎需要做的是向我的自動化帳戶添加憑據,對其進行檢索,然后調用相同的Login方法。 我現在已經完成了,但是仍然無法登錄。
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:未知用戶類型:未知用戶類型在線:10 char:1 + Login-AzureRmAccount -Credential $ Cred + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + + CategoryInfo:未指定:(:) [Add-AzureRmAccount],AadAuthenticationFailedException + FullyQualifiedErrorId:Microsoft.Azure.Common.Authentication.AadAuthenticationFailedException,Microsoft.Azure.Com要求配置文件。 AddAzureRMAccountCommand
如果我不嘗試首先登錄,則會收到一條消息,告訴我先致電Login-AzureRmAccount。
如何從運行手冊中進行身份驗證,以便可以運行自動化任務? 我究竟做錯了什么?
隨后,我們發現自動化帳戶在創建時創建了可用於登錄的連接:
$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
}
}
猜測是您嘗試使用Microsoft帳戶登錄,該帳戶只能以交互方式進行(因為它需要通過live.com進行重定向)。 您將需要在要驗證身份的租戶(Active Directory)中創建一個用戶,以便非交互式登錄能夠正常工作。
進行此工作的最簡單方法是在舊門戶中創建一個帳戶(新門戶尚不支持Active Directory管理),然后在“設置”>“管理員”中將該用戶添加為共同管理員。
您可以通過Powershell創建用戶,並分配更多的細化權限,但是在處理各種事情時,留在門戶中可能會更容易。
通過舊門戶創建的用戶與通過AzureRm命令創建的用戶之間沒有顯着差異。
我只是遇到了相同的問題,盡管此處發布的信息很有幫助,但並不能完全解決問題。
我需要的主要見解是,要使用Azure Cmdlet,必須配置“以帳戶身份運行”。 (請參閱https://docs.microsoft.com/zh-cn/azure/automation/automation-sec-configure-azure-runas-account )可以在Azure自動帳戶的“ Account Settings
部分下進行Account Settings
。
一旦擁有“以帳戶身份運行”,就可以使用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
希望這可以幫助某人。
官方建議是使用ServicePrincipal進行自動化-您可以將Secret或Certificate憑據與服務主體一起使用,並且證書效果最好。
仍然可以使用工作或學校帳戶進行自動登錄(僅使用-Credential登錄),但這要求您的組織不需要兩步驗證。 不幸的是,不能為此使用Microsoft帳戶-microsoft帳戶需要用戶交互才能進行任何登錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.