[英]Connect-AzAccount - how to avoid azure device authentication?
我已經安裝了 PowerShell 6.1.3 版本,我想使用以下 Azure PowerShell 命令連接到 Azure 帳戶:
Connect-AzAccount -Tenant <tenantId> -Subscription <subId>
輸入此命令后,我收到帶有 url 和一些代碼的警告。 然后我必須轉到 URL 並在那里輸入代碼。 之后,我連接到 Azure 帳戶。
有沒有辦法避免這種確認?
我也嘗試使用以下命令來做到這一點:
az login -u <username> -p <password>
此命令僅返回一些帳戶信息(subscriptionId、tenantId 等),但不會安裝到此帳戶的連接。
1.要使用用戶帳戶登錄,請嘗試以下命令,確保您的帳戶未啟用 MFA(多重身份驗證)。
$User = "xxx@xxxx.onmicrosoft.com"
$PWord = ConvertTo-SecureString -String "<Password>" -AsPlainText -Force
$tenant = "<tenant id>"
$subscription = "<subscription id>"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
Connect-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription
2.您也可以使用服務主體登錄,使用如下命令。
$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal
請參閱我在此處回答的類似問題,它使用舊的AzureRM
模塊,對於Az
,只需更改最后一行。
如果您不熟悉服務主體,另請參閱: 如何:使用門戶創建可以訪問資源的 Azure AD 應用程序和服務主體, 應用程序 ID 和身份驗證密鑰是您需要的Azure AD Application Id
和strong password
。
您有 2 個選擇。
使用憑據登錄(需要 Az.Accounts v 1.2.0 或更高版本)
還可以使用授權連接到 Azure 的PSCredential
對象登錄。 獲取憑據對象的最簡單方法是使用 Get-Credential cmdlet。 運行時,此 cmdlet 將提示您輸入用戶名/密碼憑據對。
$creds = Get-Credential
Connect-AzAccount -Credential $creds
使用服務主體登錄
服務主體是非交互式 Azure 帳戶。 與其他用戶帳戶一樣,他們的權限由 Azure Active Directory 管理。 通過僅授予服務主體所需的權限,您的自動化腳本將保持安全。
若要了解如何創建用於 Azure PowerShell 的服務主體,請參閱使用 Azure PowerShell 創建 Azure 服務主體。
來源: https : //docs.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azps-1.3.0
$clientId = "***********************"
$clientSecret = "********************"
$tenantId = "***********************"
$tempPassword = ConvertTo-SecureString "$clientSecret" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($clientId ,
$tempPassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.