Skip to content

Instantly share code, notes, and snippets.

@ErikHen
Created April 3, 2019 19:40
Show Gist options
  • Save ErikHen/2f357d8245dafd3541b46bbeaa44b5da to your computer and use it in GitHub Desktop.
Save ErikHen/2f357d8245dafd3541b46bbeaa44b5da to your computer and use it in GitHub Desktop.
Azure Automation Powershell Runbook that logs in to Azure
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment