Private/Complete-LoginIfNotLoggedIn.ps1
function Global:Complete-LoginIfNotLoggedIn { [CmdletBinding()] <# .SYNOPSIS Prompts for login-dialog if not already logged in. .DESCRIPTION The Login-IfNotLoggedIn cmdlet checks if there is already an established Azure-session, otherwise it'll prompt for login. #> param() process { $needLogin = $true Try { $context = Get-AzContext if ($context) { $needLogin = ([string]::IsNullOrEmpty($context.Account)) } } Catch { if ($_ -like "*Login-AzAccount to login*") { $needLogin = $true } else { throw } } if ($needLogin) { Login-AzAccount -Force } } } |