diff --git a/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs b/src/ResourceManager/Profile/Commands.Profile/AzureRmAlias/AliasHelper.cs
index 09308d9fec85..00bdcb2a98ae 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 PSInvalidOperationException(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 PSInvalidOperationException(string.Format(Properties.Resources.ProfilePathNull, "PROFILE.AllUsersAllHosts"));
+ }
+ userprofile = powershellProfile.Members.ToList().Where(a => a.Name.Equals("AllUsersAllHosts")).First().Value.ToString();
}
return userprofile;
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