Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Microsoft.WinGet.Client to run in any PowerShell session running as system #3816

Merged
merged 9 commits into from Nov 10, 2023

Conversation

msftrubengu
Copy link
Contributor

@msftrubengu msftrubengu commented Oct 27, 2023

This PR adds support for running the Microsoft.WinGet.Client module in system context without the need to start pwsh.exe -MTA. Running as MTA is required using inproc Microsoft.Management.Deployment.

If the module is running inproc and the current thread is not an MTA, it will create a new MTA thread and execute there. Otherwise, non inproc or already an MTA will use the current thread.

This was done by sharing AsyncCommand (renamed to PowerShellCmdlet) from Microsoft.WinGet.Configuration. Originally, I wanted to create a new shared lib, but decided to just share the files between the projects.

All cmdlets must inherit from PowerShellCmdlet. All cmdlets that use Microsoft.Management.Deployment must inherit ManagementDeploymentCommand and use Execute at the command engine entry point. As a safe mechanism, if any call to PackageManagerWrapper will verify the thread is not an STA if running inproc and fail.

I verified the cmdlets work in system context locally. A future PR will add running our pester tests using psexe.exe

Microsoft Reviewers: Open in CodeFlow

@msftrubengu msftrubengu marked this pull request as ready for review October 31, 2023 00:45
@msftrubengu msftrubengu requested a review from a team as a code owner October 31, 2023 00:45
@msftrubengu msftrubengu changed the title Draft PR Allow Microsoft.WinGet.Client to run in any PowerShell session running as system Oct 31, 2023
JohnMcPMS
JohnMcPMS previously approved these changes Nov 10, 2023

/// <summary>
/// Gets or sets how to match against package fields.
/// </summary>
protected string[] Query { get; set; }
#pragma warning disable SA1011 // Closing square brackets should be spaced correctly
protected string[]? Query { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Academic question: Does string?[] mean a non-null array of nullable strings, while string[]? means a nullable array of non-null strings? If so, then string?[]? is a lovingly confusing type name 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup. There's another place where I needed a non-nullable array with nullable objects, so it was object?[]

@msftrubengu msftrubengu merged commit 09c8771 into microsoft:master Nov 10, 2023
8 checks passed
@msftrubengu msftrubengu deleted the mtathread branch November 14, 2023 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants