问题
I need to use Powershell for some automated Jenkins jobs. To do the work, I need to authenticate as a Service Principal in a non-interactive fashion.
With the Azure CLI, this can be accomplished non-interactively using the following command:
az login --service-principal -u "$client_id" -p "$client_secret" -t "$tenant_id"
However, accomplishing this using the Connect-AzAccount
cmdlet for Powershell is proving to be tough. Is there anyway to achieve the same result as above using a Powershell cmdlet? All of the options described here seem to be interactive only.
Any help would be greatly appreciated.
回答1:
To use Connect-AzAccount
to login non-interactively, try the command as below, the strong password
is your client secret, it works fine on my side.
$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
来源:https://stackoverflow.com/questions/55582300/automate-authentication-to-azure-as-service-principal-using-windows-powershell