Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.25k stars 3.85k forks source link

Az.Accounts 3.0.0 - How to disable output such as the announcement message "With the new Azure PowerShell login experience, you can select the subscription you want to use more easily" #24980

Open NikolaiKleppe opened 5 months ago

NikolaiKleppe commented 5 months ago

Description

We are using some PowerShell scripts with the Terraform External provider, which requires that anything sent to the output stream must be JSON

This new announcement (and the "Retrieving subscriptions for the selection" text) is captured and crashes the pipeline and I haven't figured out how to disable it.

Authentication is by Service Principal so this shouldn't be relevant anyway, but I've disabled the LoginExperienceV2 just in case and that doesn't do anything either.

Not sure if this issue should be for Azure PowerShell or the Terraform team.

Example:

$azConfig = Set-AzConfig -LoginExperienceV2 'Off' -EnableLoginByWam $false -DisplaySurveyMessage $false -DisplayRegionIdentified $false -CheckForUpgrade $false -WarningAction Ignore | Out-Null

$con = Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credential -Subscription $subscriptionId -WarningAction Ignore | Out-Null

Then in our pipeline agent I see this is captured:

Retrieving subscriptions for the selection...
[Announcements]
With the new Azure PowerShell login experience, you can select the subscription you want to use more easily. Learn more about it and its configuration at https://go.microsoft.com/fwlink/?linkid=2271909.

If you encounter any problem, please open an issue at: https://aka.ms/azpsissue

Notice the reference to the R character from the pipeline output, which is from "Retrieving.."

The data source received unexpected results after executing the program.
│ 
│ Program output must be a JSON encoded map of string keys and string values.
│ 
│ If the error is unclear, the output can be viewed by enabling Terraform's
│ logging at TRACE level. Terraform documentation on logging:
│ https://www.terraform.io/internals/debugging
│ 
│ Program: /usr/bin/pwsh
│ Result Error: invalid character 'R' looking for beginning of value

Issue script & Debug output

-

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Ubuntu 20.04.6 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     3.0.0                 Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}

Error output

No response

NikolaiKleppe commented 5 months ago

Hi, managed to suppress the output by redirecting the stream like this instead of piping to Out-Null:

$con = Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credential -Subscription $subscriptionId -WarningAction Ignore *>&1 | Write-Verbose

*> $null also works

robinmalik commented 5 months ago

Thanks for finding a workaround @NikolaiKleppe. Given you've closed this issue, I may open a new one referencing this, as there should surely be a way to disable such notices (without having to use an undocumented stream redirection). If I remember correctly the AzureRm modules did things like this, but there was a documented way to prevent it with an environment variable switch.

NikolaiKleppe commented 5 months ago

Hi @robinmalik, indeed I might have closed this issue a bit prematurely. There should definitely be a native way to disable these messages, especially when using Service Principal Authentication.

I suppose I can re-open this issue as well

isra-fel commented 5 months ago

Thanks for reporting @NikolaiKleppe . Just to clarify a little bit, by "requires that anything sent to the output stream must be JSON" do you mean (a) you don't rely on any output of Azure PowerShell at all or (b) you convert the output of Azure PowerShell to JSON format then consume it?

cc @BethanyZhou

BethanyZhou commented 5 months ago

Hi @NikolaiKleppe , a simple way to disable the announcement message is ignoring the information message by Connect-AzAccount -InformationAction Ignore.

o-l-a-v commented 5 months ago

What about adding a Update-AzConfig setting to disable announcements?

@BethanyZhou: Unless $InformationPreferences = 'Continue' or -InformationAction 'Continue' cmdlets should not output it?

NikolaiKleppe commented 5 months ago

Thanks for reporting @NikolaiKleppe . Just to clarify a little bit, by "requires that anything sent to the output stream must be JSON" do you mean (a) you don't rely on any output of Azure PowerShell at all or (b) you convert the output of Azure PowerShell to JSON format then consume it?

cc @BethanyZhou

Hi @isra-fel , option (b) - The desired output is converted to JSON then consumed. The last line of the script is basically $Object | ConvertTo-Json and that's the only thing that should be in stdout.

NikolaiKleppe commented 5 months ago

Hi @NikolaiKleppe , a simple way to disable the announcement message is ignoring the information message by Connect-AzAccount -InformationAction Ignore.

Thanks @BethanyZhou , this works as well and is better than redirecting the output stream

o-l-a-v commented 4 months ago

Next challenge / logical flaw: Hide announcement and use -UseDeviceAuthentication.

Please do not set InformationAction or $InformationPreference to Ignore if you want to use device code authentication.