Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[CmdletBinding(DefaultParameterSetName = 'Credential')]
param (
[Parameter(ParameterSetName = 'Credential')]
[System.Management.Automation.PSCredential] $Credential,
[Parameter(ParameterSetName = 'ClearText')]
[string] $ApplicationId,
[Parameter(ParameterSetName = 'ClearText')]
[string] $Password,
[Parameter(ParameterSetName = 'Credential')]
[Parameter(ParameterSetName = 'ClearText')]
[string] $TenantId
try {
#region Azure Authentication
# If credential object is supplied we are set to go
if ($PSCmdlet.MyInvocation.BoundParameters['Credential']) {
Write-Verbose 'Using supplied credentials to authenticate.'
# If ApplicationID and Password are supplied, use these to create a credential object
elseif ($ApplicationId -and $Password) {
Write-Verbose 'Using supplied ApplicationID and Password to authenticate'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($ApplicationId, (ConvertTo-SecureString $Password -AsPlainText -Force))
# Else use interactive login
else {
Write-Verbose 'Using interactive login to authenticate.'
$Credential = $null
# Run Azure Login
if ($Credential) {
# Ask for Tenant ID if not supplied
if (-not ($TenantId)) {
$TenantId = Read-Host -Prompt 'Please enter Azure Tenant ID'
Login-AzureRmAccount -ServicePrincipal -Credential $Credential -TenantId $TenantId -ErrorAction Stop
else {
Login-AzureRmAccount -ErrorAction Stop
catch {
Write-Warning $_.Exception.Message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment