Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Connect-MgGraph] -UseDeviceCode parameter should write output to host console #2798

Open
jpawlowski opened this issue Jun 15, 2024 · 0 comments
Labels

Comments

@jpawlowski
Copy link

jpawlowski commented Jun 15, 2024

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")

Connect-MgGraph -UseDeviceCode

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
}
@jpawlowski jpawlowski added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:feature New experience request labels Jun 15, 2024
@timayabi2020 timayabi2020 removed the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants