diff --git a/src/Common/Commands.Common/AzurePSCmdlet.cs b/src/Common/Commands.Common/AzurePSCmdlet.cs index fece0560f2a5..81c56d19c428 100644 --- a/src/Common/Commands.Common/AzurePSCmdlet.cs +++ b/src/Common/Commands.Common/AzurePSCmdlet.cs @@ -254,28 +254,27 @@ protected void SaveDataCollectionProfile() protected bool CheckIfInteractive() { - if (this.Host == null || this.Host.UI == null || this.Host.UI.RawUI == null) - { - return false; - } - bool interactive = true; - try + if (this.Host == null || this.Host.UI == null || this.Host.UI.RawUI == null) { - var test = this.Host.UI.RawUI.KeyAvailable; + interactive = false; } - catch (HostException ex) + else { - if (ex.Message.StartsWith("A command that prompts the user failed")) + try { - interactive = false; + var test = this.Host.UI.RawUI.KeyAvailable; } - else + catch { - throw ex; + interactive = false; } } + if (!interactive && !_dataCollectionProfile.EnableAzureDataCollection.HasValue) + { + _dataCollectionProfile.EnableAzureDataCollection = false; + } return interactive; } @@ -288,7 +287,7 @@ protected void PromptForDataCollectionProfileIfNotExists() // Initialize it from the environment variable or profile file. InitializeDataCollectionProfile(); - if (CheckIfInteractive() && !_dataCollectionProfile.EnableAzureDataCollection.HasValue) + if (!_dataCollectionProfile.EnableAzureDataCollection.HasValue && CheckIfInteractive()) { WriteWarning(Resources.DataCollectionPrompt);