gerardog/gsudo

Issue: Can't elevate execution of a PS1 script

trparky opened this issue · 2 comments

Issue Description

I'm trying to elevate the execution of a PS1 script.

Steps to Reproduce

  1. Execute... sudo & '.\Backup VMs Batch File.ps1'.
  2. It doesn't work.

Screenshots

image

Context:

  • Windows version: 22H2 (OS Build 22621.1778)
  • gsudo version: gsudo v2.0.9 (Branch.tags-v2.0.9.Sha.0a8889d483c3224321ba48acec5e3b9fe87c62f3)

I use:

Invoke-Gsudo -ScriptBlock { script }

# or with path to script

Invoke-Gsudo -ScriptBlock { some\path\to\script.ps1 }
Get-Command -Module gsudomodule

Hi @trparky, I see your problem. Take a look at the docs where it is explained how to use gsudo on pwsh.
I would use gsudo { scriptblock }, as is the shorter syntax to use gsudo on powershell. Invoke-gsudo also works, it is slower in some scenarios but being a proper pwsh function adds tab auto complete and parameter bindings that helps (with gsudo module).
In both cases, you need the curly braces {}, so pwsh does not interpret your & as the background job operator, and lets gsudo receive and elevate the complete command.