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

Fix for admin permissions elevation method #3223

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

emilwojcik93
Copy link
Contributor

  • use join method to parse $argList
  • use $PSCommandPath instead of $MyInvocation.MyCommand.Path
  • use ScriptBlock method to create request for latest winutil.ps1 with join $argList
  • condition for $processCmd because in case when wt.exe is not available, there was double/redundant declaration of shell which lead to code mishmash (incorrect interpretation of quotes from Start-Process ArgumentList parameters
  • changes in quotes and escape characters because command where not interpreted correctly in all possible cases of $powershellCmd and $processCmd

Type of Change

  • New feature
  • Bug fix
  • Documentation update
  • Refactoring
  • Hotfix
  • Security patch
  • UI/UX improvement

Description

I've encountered the same issue in multiple installation of Windows, in pwsh and powershell as described in issue #3099 when trying to run the winutil script:

> irm "https://christitus.com/win" | iex
Winutil needs to be run as Administrator. Attempting to relaunch.
Start-Process : This command cannot be run due to the error: The parameter is incorrect.
At line:68 char:5
+     Start-Process $processCmd -ArgumentList "$powershellcmd -Executio ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Testing

I was able to fix this issue by updating the logic for elevation of permissions. Here is a link to my test script for elevation of permissions on GitHub Gist:
emilwojcik93/admin-script.ps1

I even tested this method by editing the winutil script with my updated logic for elevation of permissions, which is demonstrated in the demo video.

emilwojcik93/new-winutil.ps1

Please refer to the provided script and video for a detailed solution to the elevation of permissions issue.

Demo from test

2025-03-02.00-24-03.mp4
2025-03-01.12-45-56.mp4

Impact

Script might startup differently in different kind of terminals/shells (pwsh/powershell/wt)

Issue related to PR

Relaunching as Administrator is not working #3099

Additional Information

I checked this solution only with and without wt.exe with pwsh (powershell 7) and powershell 5. I didn't verify how it's behaving on Win10, nor with additional pass parameters for script (e.g. config file/debug param)

Checklist

  • My code adheres to the coding and style guidelines of the project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no errors/warnings/merge conflicts.

- use join method to parse $argList
- use $PSCommandPath instead of $MyInvocation.MyCommand.Path
- use ScriptBlock method to create request for latest winutil.ps1 with join $argList 
- condition for $processCmd because in case when wt.exe is not available, there was double/redundant declaration of shell which lead to code mishmash (incorrect interpretation of quotes from Start-Process ArgumentList parameters 
- changes in quotes and escape characters because command where not interpreted correctly in all possible cases of $powershellCmd and $processCmd
Update start.ps1

- use join method to parse $argList
- use $PSCommandPath instead of $MyInvocation.MyCommand.Path
- use ScriptBlock method to create request for latest winutil.ps1 with join $argList 
- condition for $processCmd because in case when wt.exe is not available, there was double/redundant declaration of shell which lead to code mishmash (incorrect interpretation of quotes from Start-Process ArgumentList parameters 
- changes in quotes and escape characters because command where not interpreted correctly in all possible cases of $powershellCmd and $processCmd
@emilwojcik93
Copy link
Contributor Author

@zQueal It might have an issues related to execution without pwsh or wt.exe available in system because of incorrectly declared quotes for Start-Process ArgumentList parameters. But now I was able test each case of execution using dedicated gist file mentioned in PR which is also visible on demo video in PR.

Best,
emilwojcik93

@ChrisTitusTech
Copy link
Owner

@emilwojcik93 thanks for this I'll get this merged in the new few days. This problem has been approached by a few people now, but I like this implementation.

@ChrisTitusTech ChrisTitusTech merged commit 52de469 into ChrisTitusTech:main Mar 5, 2025
1 check passed
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.

2 participants