Skip to content

Conversation

@daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Jan 7, 2026

PR Summary

Fix #26665

Two issues are addressed in this PR:

  1. GraphicalHostReflectionWrapper always tries to load Microsoft.PowerShell.GraphicalHost with the assembly version v3.0.0.0, which is the version of the Windows PowerShell 5.1 assemblies.

    // Prepare the full assembly name.
    AssemblyName graphicalHostAssemblyName = new AssemblyName();
    graphicalHostAssemblyName.Name = "Microsoft.PowerShell.GraphicalHost";
    graphicalHostAssemblyName.Version = new Version(3, 0, 0, 0);
    graphicalHostAssemblyName.CultureInfo = new CultureInfo(string.Empty); // Neutral culture
    graphicalHostAssemblyName.SetPublicKeyToken(new byte[] { 0x31, 0xbf, 0x38, 0x56, 0xad, 0x36, 0x4e, 0x35 });

    I believe this is the cause of the reported CLR internal error, because even though the SDK package doesn't contain the Microsoft.PowerShell.GraphicalHost assembly, the v3.0.0.0 version of the assembly is available in GAC (as part of Windows PS v5.1), and PowerShell engine does look for assembly in GAC as the last resort. So, in your case, the assembly gets loaded from GAC, which targets .NET Framework and causes the CLR internal error.

    • The fix is to use the correct version corresponding to the S.M.A.dll that is currently running.
  2. Show-Command fails when running it in PowerShell API from within PowerShell:

    PS> ($ps=[powershell]::Create()).AddScript('Show-Command').Invoke(); $ps.Streams.Error
    Show-Command: Object reference not set to an instance of an object.
    PS> $ps.Streams.Error[0].Exception
    
    TargetSite     : System.Windows.Window
                     GetHostWindow(System.Management.Automation.PSCmdlet)
    Message        : Object reference not set to an instance of an object.
    Data           : {}
    InnerException :
    HelpLink       :
    Source         : Microsoft.PowerShell.GraphicalHost
    HResult        : -2147467261
    StackTrace     :    at Microsoft.PowerShell.Commands.ShowCommandInternal.ShowCommandHel
                     per.GetHostWindow(PSCmdlet cmdlet)
                        at Microsoft.PowerShell.Commands.ShowCommandInternal.ShowCommandHel
                     per.CallShowDialog(PSCmdlet cmdlet)
                        at System.RuntimeMethodHandle.InvokeMethod(Object target, Void**
                     arguments, Signature sig, Boolean isConstructor)
                        at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object
                     obj, BindingFlags invokeAttr, Binder binder, Object[] parameters,
                     CultureInfo culture)
    

    When running in a PowerShell object created by PowerShell.Create() the default host is in use. The value of cmdlet.Host.PrivateData is null for the default host and it also could be null for a custom host.

    • The fix is to handle the null check.

PR Checklist

Copilot AI review requested due to automatic review settings January 7, 2026 23:15
@daxian-dbw daxian-dbw requested a review from a team as a code owner January 7, 2026 23:15
@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jan 7, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a null reference exception that occurs when running Show-Command within a PowerShell API context using PowerShell.Create(). The issue arises because the default host (and potentially custom hosts) can have a null PrivateData property.

Key Changes:

  • Added null-conditional operator when accessing cmdlet.Host.PrivateData.Properties
  • Added explanatory comment about why PrivateData may be null

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@daxian-dbw daxian-dbw changed the title Fix the null ref exception when running show-command in API from within PowerShell Fix the CLR internal error and null ref exception when running show-command with PowerShell API Jan 8, 2026
@iSazonov
Copy link
Collaborator

iSazonov commented Jan 9, 2026

Interesting why is a reflection and manual dll loading used at all? We could create and reference psevdo Microsoft.PowerShell.GraphicalHost project with all needed placeholders for methods and properties and replace the psevdo dll with real one in bin folder. Strong typing and automatic loading should work.

@daxian-dbw
Copy link
Member Author

I think the loose dependency is what we want. Not all environment and applications support/care about WPF/WinForm, so there shouldn't be a hard dependency between Uitlity and GraphicalHost.

@iSazonov
Copy link
Collaborator

Yes, the reflection wrapper is for avoiding project reference to WPF in SMA. My ask is about why not use strong typing instead of reflection. If we created dummy placeholder project (referenced in SMA) with all needed API we could replace the dll of the project with real Microsoft.PowerShell.GraphicalHost.dll (we have to copy it to bin folder in any case). Since both dll are signed by one key this should works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Powershell SDK - Fatal ExecutionEngineException when executing Show-Command on .NET 10

2 participants