Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created June 12, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/150ec6ff22d2df568025664e1356420a to your computer and use it in GitHub Desktop.
Save gravejester/150ec6ff22d2df568025664e1356420a to your computer and use it in GitHub Desktop.
[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