Created
June 12, 2017 13:22
-
-
Save gravejester/150ec6ff22d2df568025664e1356420a to your computer and use it in GitHub Desktop.
This file contains 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
[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 | |
} | |
#endregion | |
# YOUR CODE GOES HERE | |
} | |
catch { | |
Write-Warning $_.Exception.Message | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment