From 8aa7520ed6a9438f3865d25cc5ab6744f5e5da27 Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Mon, 2 Mar 2020 14:23:07 +0800 Subject: [PATCH 1/2] Catch exception if host is not available --- src/Common/AzurePSCmdlet.cs | 54 ++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 3b1de0c4f3..552e63f426 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -24,6 +24,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Linq.Expressions; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Text; @@ -870,32 +871,47 @@ public virtual bool IsTerminatingError(Exception ex) protected string LoadAzVersion() { - Version latestAz = new Version("0.0.0"); + Version defautVersion = new Version("0.0.0"); + if (this.Host == null) + { + WriteDebug("Cannot fetch Az version due to no host in current environment"); + return defautVersion.ToString(); + } + + Version latestAz = defautVersion; string latestSuffix = ""; using (var powershell = System.Management.Automation.PowerShell.Create()) { - powershell.Runspace = RunspaceFactory.CreateRunspace(this.Host); - powershell.AddCommand("Get-Module"); - powershell.AddParameter("Name", "Az"); - powershell.AddParameter("ListAvailable", true); - powershell.Runspace.Open(); - Collection outputs = powershell.Invoke(); - foreach (PSObject obj in outputs) + try { - string psVersion = obj.Properties["Version"].Value.ToString(); - int pos = psVersion.IndexOf('-'); - string currentSuffix = (pos == -1 || pos == psVersion.Length - 1) ? "" : psVersion.Substring(pos + 1); - Version currentAz = (pos == -1) ? new Version(psVersion) : new Version(psVersion.Substring(0, pos)); - if (currentAz > latestAz) - { - latestAz = currentAz; - latestSuffix = currentSuffix; - } - else if (currentAz == latestAz) + powershell.Runspace = RunspaceFactory.CreateRunspace(this.Host); + powershell.AddCommand("Get-Module"); + powershell.AddParameter("Name", "Az"); + powershell.AddParameter("ListAvailable", true); + powershell.Runspace.Open(); + Collection outputs = powershell.Invoke(); + foreach (PSObject obj in outputs) { - latestSuffix = String.Compare(latestSuffix, currentSuffix) > 0 ? latestSuffix : currentSuffix; + string psVersion = obj.Properties["Version"].Value.ToString(); + int pos = psVersion.IndexOf('-'); + string currentSuffix = (pos == -1 || pos == psVersion.Length - 1) ? "" : psVersion.Substring(pos + 1); + Version currentAz = (pos == -1) ? new Version(psVersion) : new Version(psVersion.Substring(0, pos)); + if (currentAz > latestAz) + { + latestAz = currentAz; + latestSuffix = currentSuffix; + } + else if (currentAz == latestAz) + { + latestSuffix = String.Compare(latestSuffix, currentSuffix) > 0 ? latestSuffix : currentSuffix; + } } } + catch (Exception e) + { + WriteDebug(string.Format("Cannot fetch Az version due to exception: {0}", e.Message)); + return defautVersion.ToString(); + } } string ret = latestAz.ToString(); if (!String.IsNullOrEmpty(latestSuffix)) From 5ae2a881934cb05bacab199e4ed7b2489740ddcd Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Mon, 2 Mar 2020 14:47:58 +0800 Subject: [PATCH 2/2] remove and sort usings --- src/Common/AzurePSCmdlet.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 552e63f426..26743c995b 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -24,7 +24,6 @@ using System.Diagnostics; using System.IO; using System.Linq; -using System.Linq.Expressions; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Text;