diff --git a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs
index 32c68e961c6..10690b65dc7 100644
--- a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs
+++ b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs
@@ -679,7 +679,8 @@ internal static string SingleQuote(string str)
/// The host window, if it is present or null if it is not.
internal static Window GetHostWindow(PSCmdlet cmdlet)
{
- PSPropertyInfo windowProperty = cmdlet.Host.PrivateData.Properties["Window"];
+ // The value of 'PrivateData' property may be null for the default host or a custom host.
+ PSPropertyInfo windowProperty = cmdlet.Host.PrivateData?.Properties["Window"];
if (windowProperty == null)
{
return null;
diff --git a/src/System.Management.Automation/resources/HelpErrors.resx b/src/System.Management.Automation/resources/HelpErrors.resx
index 27634de2995..ae797e8af12 100644
--- a/src/System.Management.Automation/resources/HelpErrors.resx
+++ b/src/System.Management.Automation/resources/HelpErrors.resx
@@ -179,7 +179,7 @@
To update these Help topics, start PowerShell by using the "Run as Administrator" command, and try running Update-Help again.
- To use the {0}, install Windows PowerShell ISE by using Server Manager, and then restart this application. ({1})
+ To use the {0}, make sure your application uses 'Microsoft.NET.Sdk.WindowsDesktop' as the project SDK and the corresponding assembly 'Microsoft.PowerShell.GraphicalHost' is available. ({1})
{0} does not work in a remote session.
diff --git a/src/System.Management.Automation/utils/GraphicalHostReflectionWrapper.cs b/src/System.Management.Automation/utils/GraphicalHostReflectionWrapper.cs
index a0b12f986de..ec780222345 100644
--- a/src/System.Management.Automation/utils/GraphicalHostReflectionWrapper.cs
+++ b/src/System.Management.Automation/utils/GraphicalHostReflectionWrapper.cs
@@ -55,7 +55,7 @@ private GraphicalHostReflectionWrapper()
/// When it was not possible to load Microsoft.PowerShell.GraphicalHost.dlly.
internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper(PSCmdlet parentCmdlet, string graphicalHostHelperTypeName)
{
- return GraphicalHostReflectionWrapper.GetGraphicalHostReflectionWrapper(parentCmdlet, graphicalHostHelperTypeName, parentCmdlet.CommandInfo.Name);
+ return GetGraphicalHostReflectionWrapper(parentCmdlet, graphicalHostHelperTypeName, parentCmdlet.CommandInfo.Name);
}
///
@@ -73,9 +73,9 @@ internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Assembly.Load has been found to throw unadvertised exceptions")]
internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper(PSCmdlet parentCmdlet, string graphicalHostHelperTypeName, string featureName)
{
- GraphicalHostReflectionWrapper returnValue = new GraphicalHostReflectionWrapper();
+ GraphicalHostReflectionWrapper returnValue = new();
- if (GraphicalHostReflectionWrapper.IsInputFromRemoting(parentCmdlet))
+ if (IsInputFromRemoting(parentCmdlet))
{
ErrorRecord error = new ErrorRecord(
new NotSupportedException(StringUtil.Format(HelpErrors.RemotingNotSupportedForFeature, featureName)),
@@ -87,9 +87,10 @@ internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper
}
// Prepare the full assembly name.
- AssemblyName graphicalHostAssemblyName = new AssemblyName();
+ AssemblyName smaAssemblyName = typeof(PSObject).Assembly.GetName();
+ AssemblyName graphicalHostAssemblyName = new();
graphicalHostAssemblyName.Name = "Microsoft.PowerShell.GraphicalHost";
- graphicalHostAssemblyName.Version = new Version(3, 0, 0, 0);
+ graphicalHostAssemblyName.Version = smaAssemblyName.Version;
graphicalHostAssemblyName.CultureInfo = new CultureInfo(string.Empty); // Neutral culture
graphicalHostAssemblyName.SetPublicKeyToken(new byte[] { 0x31, 0xbf, 0x38, 0x56, 0xad, 0x36, 0x4e, 0x35 });
@@ -124,7 +125,7 @@ internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper
returnValue._graphicalHostHelperType = returnValue._graphicalHostAssembly.GetType(graphicalHostHelperTypeName);
- Diagnostics.Assert(returnValue._graphicalHostHelperType != null, "the type exists in Microsoft.PowerShell.GraphicalHost");
+ Diagnostics.Assert(returnValue._graphicalHostHelperType != null, "the type should exist in Microsoft.PowerShell.GraphicalHost");
ConstructorInfo constructor = returnValue._graphicalHostHelperType.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null,