From 0436e55edbf4b8d9e21bf3e238b8be95c792686c Mon Sep 17 00:00:00 2001 From: maddieclayton Date: Thu, 23 Aug 2018 18:41:47 -0700 Subject: [PATCH 1/3] fix alias --- .../Commands.Profile/AzureRmAlias/AliasHelper.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs b/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs index 09308d9fec85..fce9d79306e5 100644 --- a/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs +++ b/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs @@ -45,14 +45,22 @@ public static string GetProfilePath(string Scope, SessionState sessionState) var userprofile = ""; if (Scope != null && Scope.Equals("CurrentUser")) { - var editionType = sessionState.PSVariable.GetValue("PSEdition") as string; - var psFolder = string.Equals(editionType, "Desktop", StringComparison.OrdinalIgnoreCase) ? "WindowsPowerShell" : "PowerShell"; - userprofile = Path.Combine(sessionState.PSVariable.GetValue("env:USERPROFILE").ToString(), "Documents", psFolder, "profile.ps1"); + var powershellProfile = sessionState.PSVariable.GetValue("PROFILE") as PSObject; + if (powershellProfile == null || !powershellProfile.Members.ToList().Any(a => a.Name.Equals("CurrentUserAllHosts"))) + { + throw new NullReferenceException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.CurrentUserAllHosts")); + } + userprofile = powershellProfile.Members.ToList().Where(a => a.Name.Equals("CurrentUserAllHosts")).First().Value.ToString(); } else if (Scope != null && Scope.Equals("LocalMachine")) { - userprofile = Path.Combine(sessionState.PSVariable.GetValue("PSHOME").ToString(), "profile.ps1"); + var powershellProfile = sessionState.PSVariable.GetValue("PROFILE") as PSObject; + if (powershellProfile == null || !powershellProfile.Members.ToList().Any(a => a.Name.Equals("AllUsersAllHosts"))) + { + throw new NullReferenceException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.AllUsersAllHosts")); + } + userprofile = powershellProfile.Members.ToList().Where(a => a.Name.Equals("AllUsersAllHosts")).First().Value.ToString(); } return userprofile; From fb910a9f96e8b591085c1697089f4b9e6a05c5d4 Mon Sep 17 00:00:00 2001 From: maddieclayton Date: Thu, 23 Aug 2018 18:43:50 -0700 Subject: [PATCH 2/3] add resource string --- .../Commands.Profile/Properties/Resources.Designer.cs | 9 +++++++++ .../Profile/Commands.Profile/Properties/Resources.resx | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs index 9882f7445298..d1b0b8714e3d 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs @@ -573,6 +573,15 @@ internal static string ProfileCurrentWrite { } } + /// + /// Looks up a localized string similar to Unable to set profile because environment variable '${0}' is null.. + /// + internal static string ProfilePathNull { + get { + return ResourceManager.GetString("ProfilePathNull", resourceCulture); + } + } + /// /// Looks up a localized string similar to Personally identifiable information and secrets may be written to the file at '{0}'. Please ensure that the saved file is assigned appropriate access controls. /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx index 81dfdb145deb..be3c9f10c0d9 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx @@ -420,4 +420,7 @@ LocalMachine scope can only be set in PowerShell administrative mode. + + Unable to set profile because environment variable '${0}' is null. + \ No newline at end of file From 1df60d227f552a6bbcdd112580a47ed7fd7cce38 Mon Sep 17 00:00:00 2001 From: maddieclayton Date: Thu, 23 Aug 2018 18:47:20 -0700 Subject: [PATCH 3/3] change exception type --- .../Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs b/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs index fce9d79306e5..00bdcb2a98ae 100644 --- a/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs +++ b/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs @@ -48,7 +48,7 @@ public static string GetProfilePath(string Scope, SessionState sessionState) var powershellProfile = sessionState.PSVariable.GetValue("PROFILE") as PSObject; if (powershellProfile == null || !powershellProfile.Members.ToList().Any(a => a.Name.Equals("CurrentUserAllHosts"))) { - throw new NullReferenceException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.CurrentUserAllHosts")); + throw new PSInvalidOperationException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.CurrentUserAllHosts")); } userprofile = powershellProfile.Members.ToList().Where(a => a.Name.Equals("CurrentUserAllHosts")).First().Value.ToString(); } @@ -58,7 +58,7 @@ public static string GetProfilePath(string Scope, SessionState sessionState) var powershellProfile = sessionState.PSVariable.GetValue("PROFILE") as PSObject; if (powershellProfile == null || !powershellProfile.Members.ToList().Any(a => a.Name.Equals("AllUsersAllHosts"))) { - throw new NullReferenceException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.AllUsersAllHosts")); + throw new PSInvalidOperationException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.AllUsersAllHosts")); } userprofile = powershellProfile.Members.ToList().Where(a => a.Name.Equals("AllUsersAllHosts")).First().Value.ToString(); }