Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Last active August 17, 2020 14:59
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 jhochwald/203983e77fd7d0f87810e0558091e7e2 to your computer and use it in GitHub Desktop.
Save jhochwald/203983e77fd7d0f87810e0558091e7e2 to your computer and use it in GitHub Desktop.
Speeds up the interactive login for Az PowerShell by opening the DeviceLogin page and then alt-tabs back to the PowerShell Window
#requires -Version 2.0 -Modules Az.Accounts
function Connect-AzDeviceLogin
{
<#
.SYNOPSIS
Speeds up the interactive login for Az PowerShell
.DESCRIPTION
Speeds up the interactive login for Az PowerShell by opening the DeviceLogin page and then alt-tabs back to the PowerShell Window
.EXAMPLE
PS C:\> Connect-AzDeviceLogin
.NOTES
Idea was found on Twitter, thanks to @markwragg for that
.LINK
https://twitter.com/markwragg/status/1295343270843867137?s=21
#>
[CmdletBinding(ConfirmImpact = 'None')]
param ()
$CheckAzContext = $null
$CheckAzContext = (Get-AzContext -ErrorAction SilentlyContinue)
if ($CheckAzContext)
{
Write-Verbose -Message 'Connected'
$CheckAzContext = $null
}
else
{
Write-Verbose -Message 'Connecting'
Start-Process -FilePath 'https://microsoft.com/devicelogin'
$null = (Add-Type -AssemblyName System.Windows.Forms)
[Windows.Forms.SendKeys]::SendWait('%{TAB}')
$null = (Connect-AzAccount -Force)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment