@@ -870,32 +870,47 @@ public virtual bool IsTerminatingError(Exception ex)
870870
871871 protected string LoadAzVersion ( )
872872 {
873- Version latestAz = new Version ( "0.0.0" ) ;
873+ Version defautVersion = new Version ( "0.0.0" ) ;
874+ if ( this . Host == null )
875+ {
876+ WriteDebug ( "Cannot fetch Az version due to no host in current environment" ) ;
877+ return defautVersion . ToString ( ) ;
878+ }
879+
880+ Version latestAz = defautVersion ;
874881 string latestSuffix = "" ;
875882 using ( var powershell = System . Management . Automation . PowerShell . Create ( ) )
876883 {
877- powershell . Runspace = RunspaceFactory . CreateRunspace ( this . Host ) ;
878- powershell . AddCommand ( "Get-Module" ) ;
879- powershell . AddParameter ( "Name" , "Az" ) ;
880- powershell . AddParameter ( "ListAvailable" , true ) ;
881- powershell . Runspace . Open ( ) ;
882- Collection < PSObject > outputs = powershell . Invoke ( ) ;
883- foreach ( PSObject obj in outputs )
884+ try
884885 {
885- string psVersion = obj . Properties [ "Version" ] . Value . ToString ( ) ;
886- int pos = psVersion . IndexOf ( '-' ) ;
887- string currentSuffix = ( pos == - 1 || pos == psVersion . Length - 1 ) ? "" : psVersion . Substring ( pos + 1 ) ;
888- Version currentAz = ( pos == - 1 ) ? new Version ( psVersion ) : new Version ( psVersion . Substring ( 0 , pos ) ) ;
889- if ( currentAz > latestAz )
890- {
891- latestAz = currentAz ;
892- latestSuffix = currentSuffix ;
893- }
894- else if ( currentAz == latestAz )
886+ powershell . Runspace = RunspaceFactory . CreateRunspace ( this . Host ) ;
887+ powershell . AddCommand ( "Get-Module" ) ;
888+ powershell . AddParameter ( "Name" , "Az" ) ;
889+ powershell . AddParameter ( "ListAvailable" , true ) ;
890+ powershell . Runspace . Open ( ) ;
891+ Collection < PSObject > outputs = powershell . Invoke ( ) ;
892+ foreach ( PSObject obj in outputs )
895893 {
896- latestSuffix = String . Compare ( latestSuffix , currentSuffix ) > 0 ? latestSuffix : currentSuffix ;
894+ string psVersion = obj . Properties [ "Version" ] . Value . ToString ( ) ;
895+ int pos = psVersion . IndexOf ( '-' ) ;
896+ string currentSuffix = ( pos == - 1 || pos == psVersion . Length - 1 ) ? "" : psVersion . Substring ( pos + 1 ) ;
897+ Version currentAz = ( pos == - 1 ) ? new Version ( psVersion ) : new Version ( psVersion . Substring ( 0 , pos ) ) ;
898+ if ( currentAz > latestAz )
899+ {
900+ latestAz = currentAz ;
901+ latestSuffix = currentSuffix ;
902+ }
903+ else if ( currentAz == latestAz )
904+ {
905+ latestSuffix = String . Compare ( latestSuffix , currentSuffix ) > 0 ? latestSuffix : currentSuffix ;
906+ }
897907 }
898908 }
909+ catch ( Exception e )
910+ {
911+ WriteDebug ( string . Format ( "Cannot fetch Az version due to exception: {0}" , e . Message ) ) ;
912+ return defautVersion . ToString ( ) ;
913+ }
899914 }
900915 string ret = latestAz . ToString ( ) ;
901916 if ( ! String . IsNullOrEmpty ( latestSuffix ) )
0 commit comments