-
Notifications
You must be signed in to change notification settings - Fork 199
Description
Is your feature request related to a problem? Please describe the problem.
I utilize Connect-MgGraph
within several helper scripts. These scripts redirect their object outputs to a variable, allowing for subsequent processing in the main script. In scenarios requiring device code authentication for interactive login, such as GitHub Codespaces, the device code fails to display to the user since the output is captured by the variable. Consequently, this prevents the completion of the authentication process.
Describe the solution you'd like.
Similar to the Connect-AzAccount
command, the Connect-MgGraph
command ought to direct its output to the host console rather than the output stream during device code authentication.
This guarantees that users can consistently view the instructions and device code necessary to finalize the authentication process as intended.
Enhancing visibility, it would be beneficial to prepend a prefix to the authentication instructions, akin to the following:
Write-Host "Please select the account you want to login with.`n" -ForegroundColor Yellow
Write-Host -NoNewline "`e[1;37;44m[Login to Graph]`e[0m "
$message = 'To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code LK6RW4ZSN to authenticate'
$code = $($message -cmatch ' ([A-Z0-9]{9}) ' | Out-Null; $Matches[1])
Write-Host ($message -replace $code, "`e[4m$code`e[24m")
This example demonstrates the behavior similar to that provided by the new Az.Accounts
v3.0.0 module.
It is sufficient to alter only the Device Code output behavior. Other text outputs should continue to be directed to the output stream, allowing individuals the choice to redirect or suppress them as they can currently.
Additional context?
For readers seeking a workaround, the following may serve as inspiration to tailor to your specific needs:
$params = @{
UseDeviceCode = $true
NoWelcome = $true
ContextScope = 'Process'
ErrorAction = 'Stop'
}
if ($params.UseDeviceCode) {
Write-Host "Please select the account you want to login with.`n" -ForegroundColor Yellow
Write-Host -NoNewline "`e[1;37;44m[Login to Graph]`e[0m "
Connect-MgGraph @params | ForEach-Object {
if ($_ -is [string] -and $_ -cmatch ' ([A-Z0-9]{9}) ') {
$_ -replace $Matches[1], "`e[4m$($Matches[1])`e[24m"
}
else {
$_
}
} | Out-Host
}
else {
Connect-MgGraph @params 1> $null
}