Skip to content

Commit 61a3251

Browse files
authored
run powershell using ExecuteScript (Azure#183)
1 parent a4e7f52 commit 61a3251

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

src/Common/AzurePSCmdlet.cs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected string PSVersion
135135
protected string ModuleVersion { get { return AzurePowerShell.AssemblyVersion; } }
136136

137137
/// <summary>
138-
/// The context for management cmdlet requests - includes account, tenant, subscription,
138+
/// The context for management cmdlet requests - includes account, tenant, subscription,
139139
/// and credential information for targeting and authorizing management calls.
140140
/// </summary>
141141
protected abstract IAzureContext DefaultContext { get; }
@@ -647,7 +647,7 @@ protected void LogQosEvent()
647647
}
648648

649649
/// <summary>
650-
/// Guards execution of the given action using ShouldProcess and ShouldContinue. This is a legacy
650+
/// Guards execution of the given action using ShouldProcess and ShouldContinue. This is a legacy
651651
/// version forcompatibility with older RDFE cmdlets.
652652
/// </summary>
653653
/// <param name="force">Do not ask for confirmation</param>
@@ -677,10 +677,10 @@ protected virtual void ConfirmAction(bool force, string continueMessage, string
677677
}
678678

679679
/// <summary>
680-
/// Guards execution of the given action using ShouldProcess and ShouldContinue. The optional
681-
/// useSHouldContinue predicate determines whether SHouldContinue should be called for this
682-
/// particular action (e.g. a resource is being overwritten). By default, both
683-
/// ShouldProcess and ShouldContinue will be executed. Cmdlets that use this method overload
680+
/// Guards execution of the given action using ShouldProcess and ShouldContinue. The optional
681+
/// useSHouldContinue predicate determines whether SHouldContinue should be called for this
682+
/// particular action (e.g. a resource is being overwritten). By default, both
683+
/// ShouldProcess and ShouldContinue will be executed. Cmdlets that use this method overload
684684
/// must have a force parameter.
685685
/// </summary>
686686
/// <param name="force">Do not ask for confirmation</param>
@@ -714,8 +714,8 @@ protected virtual void ConfirmAction(bool force, string continueMessage, string
714714
}
715715

716716
/// <summary>
717-
/// Prompt for confirmation depending on the ConfirmLevel. By default No confirmation prompt
718-
/// occurs unless ConfirmLevel >= $ConfirmPreference. Guarding the actions of a cmdlet with this
717+
/// Prompt for confirmation depending on the ConfirmLevel. By default No confirmation prompt
718+
/// occurs unless ConfirmLevel >= $ConfirmPreference. Guarding the actions of a cmdlet with this
719719
/// method will enable the cmdlet to support -WhatIf and -Confirm parameters.
720720
/// </summary>
721721
/// <param name="processMessage">The change being made to the resource</param>
@@ -865,54 +865,48 @@ public virtual bool IsTerminatingError(Exception ex)
865865
//If there is no Az module, the version is "0.0.0"
866866
public static string AzVersion { set; get; }
867867

868-
//Initialized once AzVersion is loadded.
868+
//Initialized once AzVersion is loaded.
869869
//Format: AzurePowershell/Az0.0.0;%AZUREPS_HOST_ENVIROMENT%
870870
public static string UserAgent { set; get; }
871871

872872
protected string LoadAzVersion()
873873
{
874-
Version defautVersion = new Version("0.0.0");
874+
Version defaultVersion = new Version("0.0.0");
875875
if (this.Host == null)
876876
{
877877
WriteDebug("Cannot fetch Az version due to no host in current environment");
878-
return defautVersion.ToString();
878+
return defaultVersion.ToString();
879879
}
880880

881-
Version latestAz = defautVersion;
881+
Version latestAz = defaultVersion;
882882
string latestSuffix = "";
883-
using (var powershell = System.Management.Automation.PowerShell.Create())
883+
884+
try
884885
{
885-
try
886+
var outputs = this.ExecuteScript<PSObject>("Get-Module -Name Az -ListAvailable");
887+
foreach (PSObject obj in outputs)
886888
{
887-
powershell.Runspace = RunspaceFactory.CreateRunspace(this.Host);
888-
powershell.AddCommand("Get-Module");
889-
powershell.AddParameter("Name", "Az");
890-
powershell.AddParameter("ListAvailable", true);
891-
powershell.Runspace.Open();
892-
Collection<PSObject> outputs = powershell.Invoke();
893-
foreach (PSObject obj in outputs)
889+
string psVersion = obj.Properties["Version"].Value.ToString();
890+
int pos = psVersion.IndexOf('-');
891+
string currentSuffix = (pos == -1 || pos == psVersion.Length - 1) ? "" : psVersion.Substring(pos + 1);
892+
Version currentAz = (pos == -1) ? new Version(psVersion) : new Version(psVersion.Substring(0, pos));
893+
if (currentAz > latestAz)
894894
{
895-
string psVersion = obj.Properties["Version"].Value.ToString();
896-
int pos = psVersion.IndexOf('-');
897-
string currentSuffix = (pos == -1 || pos == psVersion.Length - 1) ? "" : psVersion.Substring(pos + 1);
898-
Version currentAz = (pos == -1) ? new Version(psVersion) : new Version(psVersion.Substring(0, pos));
899-
if (currentAz > latestAz)
900-
{
901-
latestAz = currentAz;
902-
latestSuffix = currentSuffix;
903-
}
904-
else if (currentAz == latestAz)
905-
{
906-
latestSuffix = String.Compare(latestSuffix, currentSuffix) > 0 ? latestSuffix : currentSuffix;
907-
}
895+
latestAz = currentAz;
896+
latestSuffix = currentSuffix;
897+
}
898+
else if (currentAz == latestAz)
899+
{
900+
latestSuffix = String.Compare(latestSuffix, currentSuffix) > 0 ? latestSuffix : currentSuffix;
908901
}
909-
}
910-
catch (Exception e)
911-
{
912-
WriteDebug(string.Format("Cannot fetch Az version due to exception: {0}", e.Message));
913-
return defautVersion.ToString();
914902
}
915903
}
904+
catch (Exception e)
905+
{
906+
WriteDebug(string.Format("Cannot fetch Az version due to exception: {0}", e.Message));
907+
return defaultVersion.ToString();
908+
}
909+
916910
string ret = latestAz.ToString();
917911
if (!String.IsNullOrEmpty(latestSuffix))
918912
{

0 commit comments

Comments
 (0)