Fix the CLR internal error and null ref exception when running show-command with PowerShell API (#26669)

This commit is contained in:
Dongbo Wang
2026-01-12 10:44:55 -08:00
committed by GitHub
parent 5ebba3ca33
commit bf03a14b6d
3 changed files with 10 additions and 8 deletions
@@ -679,7 +679,8 @@ Function PSGetSerializedShowCommandInfo
/// <returns>The host window, if it is present or null if it is not.</returns>
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;
@@ -179,7 +179,7 @@
To update these Help topics, start PowerShell by using the "Run as Administrator" command, and try running Update-Help again.</value>
</data>
<data name="GraphicalHostAssemblyIsNotFound" xml:space="preserve">
<value>To use the {0}, install Windows PowerShell ISE by using Server Manager, and then restart this application. ({1})</value>
<value>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})</value>
</data>
<data name="RemotingNotSupportedForFeature" xml:space="preserve">
<value>{0} does not work in a remote session.</value>
@@ -55,7 +55,7 @@ namespace System.Management.Automation.Internal
/// <exception cref="RuntimeException">When it was not possible to load Microsoft.PowerShell.GraphicalHost.dlly.</exception>
internal static GraphicalHostReflectionWrapper GetGraphicalHostReflectionWrapper(PSCmdlet parentCmdlet, string graphicalHostHelperTypeName)
{
return GraphicalHostReflectionWrapper.GetGraphicalHostReflectionWrapper(parentCmdlet, graphicalHostHelperTypeName, parentCmdlet.CommandInfo.Name);
return GetGraphicalHostReflectionWrapper(parentCmdlet, graphicalHostHelperTypeName, parentCmdlet.CommandInfo.Name);
}
/// <summary>
@@ -73,9 +73,9 @@ namespace System.Management.Automation.Internal
[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 @@ namespace System.Management.Automation.Internal
}
// 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 @@ namespace System.Management.Automation.Internal
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,