首页 > 解决方案 > 无法注销并使用 Azure power shell 进行非交互式登录

问题描述

我已经打开powershell并通过交互式登录登录到azure帐户。然后我想做一个非交互式登录,我有代码。PFB 是我的代码。

但是当我使用下面的代码时,它仍然从缓存中获取用户,即使我也输入了错误的密码,它也不会抛出任何错误。

$subscriptionId="Subscription id here"
$tenantid="tenant id here"
$clientid="clinent id here" #appid
$password="password" #i have given the wrong password here
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientid, $securePassword
#Connect-AzureRmAccount -ServicePrincipal -Credential $credential -TenantId $tenantid -Subscription $subscriptionId
Add-AzureRmAccount -Credential $credential -TenantId $tenantID -ServicePrincipal -Subscription $subscriptionId

在这里,我想从 Azure 电源外壳注销并使用服务主体(Azure 应用程序)的非交互式登录。有人可以帮帮我吗。

标签: azurepowershellazure-powershell

解决方案


要注销帐户,您可以使用此命令Remove-AzureRmAccount,或关闭 powershell 并打开一个新的。

要使用服务主体使用非交互式登录,您可以使用以下命令,密码是您的 AD 应用程序的密码。

$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal 

在此处输入图像描述


推荐阅读