Last active
September 15, 2021 04:20
-
-
Save joymon/f4cb03f6ecf312f1b59ebca45327488b to your computer and use it in GitHub Desktop.
Connect-AzAccount using Service principal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ Azure login params ################### | |
$ServicePrincipalCertificateThumbprint = '' | |
$TenantId = '' #This is AAD Tenant Id where the below app registration is created, not Subscription Id | |
$ApplicationId = '' #THis is the application registration id sometimes called clientId. Make sure this app registration has permission to send message. | |
################# One time install #################### | |
$azAccountsInstalled = Get-InstalledModule Az.Accounts | |
if( ! $azAccountsInstalled) { | |
Install-Module Az.Accounts -AllowClobber | |
} | |
else{ | |
Write-Host "Az.Accounts already present" -ForegroundColor Yellow | |
} | |
####################################################### | |
# Connect using service principal and validations | |
# If running from Linux use certificate path https://docs.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-6.4.0#example-9--connect-using-certificate-file | |
Connect-AzAccount -CertificateThumbprint $ServicePrincipalCertificateThumbprint -SendCertificateChain -ApplicationId $ApplicationId -Tenant $TenantId -ServicePrincipal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Change the -ResourceUrl based on scenario. Below is for ServiceBus | |
$accessToken = Get-AzAccessToken -ResourceUrl https://servicebus.azure.net/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment