Last active
August 17, 2020 14:59
-
-
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
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
#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