-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make Powershell completion work in constrained mode #2196
Make Powershell completion work in constrained mode #2196
Conversation
Cool! |
Hi @marckhouzam , the test steps I'm using are:
$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
my-cobra-program.exe completion powershell | Out-String | Invoke-Expression
If i build my program with unmodified cobra 1.8.1 nothing happens, I don't get any completions (I assume Powershell tries to run the completion scriptblock and aborts once that hits an error). If I use my modified version, I get the expected completions just like in the normal FullLanguage mode. To confirm that CompletionResult is the problem, I also tried manually creating one in a constrained session ( |
@lstemplinger Which version of pwsh are you running? I have 7.4.1 and the change does not work for me. After some debugging I see that there is more code affected by the language restrictions
I'm not familiar much with powershell syntax, but maybe this can help you figure how to fix it? To help you debug you can set the |
I was testing with v5.1 since that's what i expect most of my users will be on, and I didn't see any changes to constrained mode in later versions. It definitely works there, so seems like something changed with sort-object between those version. I'll do some testing with 7.4 and see if i can figure out a fix. |
Creating CompletionResult objects is not allowed in Powershell constrained mode, so return results as strings if constrained mode is enabled Store results as PsCustomObjects instead of hashtables. This prevents Sort-Object from trying to convert the hashtable to a object, which is blocked in constrained mode. PsCustomObjects are created using New-Object to work around PowerShell/PowerShell#20767
6ec249b
to
04d768e
Compare
Storing results as PSCustomObjects instead of Hashtables fixed the issue for me. Sort-Object doesn't have to do any conversions that get blocked, and for the rest of the script accessing attributes works the same. Tested using the same method as before on:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for this!
Creating CompletionResult objects is not allowed in Powershell constrained mode, so return results as strings if constrained mode is enabled Store results as PsCustomObjects instead of hashtables. This prevents Sort-Object from trying to convert the hashtable to a object, which is blocked in constrained mode. PsCustomObjects are created using New-Object to work around PowerShell/PowerShell#20767
Currently the Powershell completion script generated by Cobra does not work in constrained mode because creating CompletionResult Objects is blocked there.
This adds a check to the script that returns results as plain strings if constrained mode is enabled. Results in FullLanguage mode are unchanged.
So far I've tested with Powershell 5.1 on Windows 10, all three completion modes seem to work as expected even in constrained mode.