Skip to content

Instantly share code, notes, and snippets.

@joymon
Last active September 15, 2021 04:20
Show Gist options
  • Save joymon/f4cb03f6ecf312f1b59ebca45327488b to your computer and use it in GitHub Desktop.
Save joymon/f4cb03f6ecf312f1b59ebca45327488b to your computer and use it in GitHub Desktop.
Connect-AzAccount using Service principal
################ 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
# 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