Issue: Gsudo changes the value of `$PSScriptRoot\..\` to your home drive instead of the directory before it.
FlawlessCasual17 opened this issue · 4 comments
FlawlessCasual17 commented
Issue Description
I was doing some PowerShell scripting. During some testing, I found out that $PSScriptRoot\..\
has its value changed to your home drive instead of the directory before it when I used Invoke-Gsudo
. I was hoping the value of $PSScriptRoot\..\
would be the directory before it.
Steps to Reproduce
- Download this file, test.zip, and extract it to any directory.
- Open PowerShell/pwsh/Windows Terminal, and use
cd
(Set-Location
) to change your terminal's directory to where you extracted the files to. - Run,
& ".\test2.ps1"
first (This one does not useInvoke-Gsudo
) - Then run,
& ".\test.ps1"
afterwards (This one usesInvoke-Gsudo
)
Screenshots
NOTE: This is in being done in the Downloads directory.
test2.ps1
> & '.\test2.ps1'
Directory: C:\Users\krist
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 4/11/2023 6:37 PM .config
d---- 4/24/2023 11:49 AM .Harmonoid
d--h- 4/11/2023 6:32 PM .local
d---- 5/2/2023 8:17 PM .ssh
d---- 4/14/2023 2:20 PM .VirtualBox
d---- 3/31/2023 4:10 PM .wingetui
d-r-- 10/4/2022 8:21 PM 3D Objects
d--h- 10/4/2022 8:01 PM AppData
l--hs 10/4/2022 8:01 PM Application Data C:\Users\krist\AppData\Roaming
d-r-- 11/20/2022 9:51 PM Contacts
l--hs 10/4/2022 8:01 PM Cookies C:\Users\krist\AppData\Local\Microsoft\Windows\INetCookies
d-r-- 4/30/2023 2:33 PM ﲾ Desktop
d-r-- 5/2/2023 9:02 PM Documents
d-r-- 5/3/2023 2:02 PM Downloads
d-r-- 10/4/2022 8:21 PM ﮛ Favorites
d-r-- 10/5/2022 10:35 PM Links
l--hs 10/4/2022 8:01 PM Local Settings C:\Users\krist\AppData\Local
d-r-- 5/2/2023 6:43 PM MEGA
d-r-- 4/24/2023 2:34 PM Music
l--hs 10/4/2022 8:01 PM My Documents C:\Users\krist\Documents
d---- 4/12/2023 11:50 AM MyScoop
l--hs 10/4/2022 8:01 PM NetHood C:\Users\krist\AppData\Roaming\Microsoft\Windows\Network Shortcuts
d-r-- 4/24/2023 2:30 PM Pictures
l--hs 10/4/2022 8:01 PM PrintHood C:\Users\krist\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
l--hs 10/4/2022 8:01 PM Recent C:\Users\krist\AppData\Roaming\Microsoft\Windows\Recent
d-r-- 10/5/2022 11:11 PM Recorded Calls
d-r-- 3/29/2023 2:31 PM Saved Games
d---- 3/14/2023 9:43 PM scoop
d-r-- 10/4/2022 8:23 PM Searches
l--hs 10/4/2022 8:01 PM SendTo C:\Users\krist\AppData\Roaming\Microsoft\Windows\SendTo
d---- 1/2/2023 10:56 AM SpotiFlyer
l--hs 10/4/2022 8:01 PM Start Menu C:\Users\krist\AppData\Roaming\Microsoft\Windows\Start Menu
d---- 4/27/2023 11:37 AM StartupDis
l--hs 10/4/2022 8:01 PM Templates C:\Users\krist\AppData\Roaming\Microsoft\Windows\Templates
d-r-- 4/13/2023 4:03 PM Videos
l---- 4/12/2023 6:49 PM VM Shared Folder C:\Users\krist\MEGA\ScoopInstaller & Winget (DO THESE FIRST!)
-a--- 5/2/2023 3:36 PM 197 .bash_history
-a--- 10/14/2022 12:52 PM 174 .gitconfig
-a--- 10/18/2022 8:55 PM 64 KillAllFacists
-a-h- 5/3/2023 10:48 AM 3407872 NTUSER.DAT
-a-hs 10/4/2022 8:01 PM 98304 ntuser.dat.LOG1
-a-hs 10/4/2022 8:01 PM 1138688 ntuser.dat.LOG2
-a-hs 10/4/2022 8:21 PM 65536 NTUSER.DAT{b0ab53b5-4455-11ed-9ffb-d8bbc119d854}.TM.blf
-a-hs 10/4/2022 8:01 PM 524288 NTUSER.DAT{b0ab53b5-4455-11ed-9ffb-d8bbc119d854}.TMContainer00000000000000000001.regtrans-ms
-a-hs 10/4/2022 8:01 PM 524288 NTUSER.DAT{b0ab53b5-4455-11ed-9ffb-d8bbc119d854}.TMContainer00000000000000000002.regtrans-ms
---hs 10/4/2022 8:01 PM 20 ntuser.ini
Press Enter to continue...:
test.ps1
> & '.\test.ps1'
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d--hs 10/8/2022 9:26 PM
d--h- 10/4/2022 9:27 PM
d--h- 4/12/2023 12:17 PM
l--hs 10/4/2022 8:30 PM
d---- 10/4/2022 9:22 PM
d-r-- 5/1/2023 12:22 PM
d-r-- 4/2/2023 4:54 PM
d--h- 4/27/2023 11:37 AM
d--hs 10/4/2022 9:26 PM
d--hs 5/3/2023 10:47 AM
d-r-- 12/15/2022 4:48 PM
d---- 1/1/2023 10:03 PM
d---- 5/2/2023 7:33 PM
-a-hs 5/3/2023 10:48 AM 8192
-a-hs 5/3/2023 10:48 AM 6822289408
-a-hs 5/3/2023 10:48 AM 6979321856
-a-hs 5/3/2023 10:48 AM 16777216
Press Enter to continue...:
Context:
- Windows version: Win10 22H2 (OS Build 119045.2846)
- gsudo version: gsudo v2.0.9 (Branch.tags-v2.0.9.Sha.0a8889d483c3224321ba48acec5e3b9fe87c62f3)
gerardog commented
Do not expect $PSScriptRoot
value to be transparent across elevation boundaries. It is an automatic variable, so I can't manipulate it and it changes when transitioning to an elevated process.
You must work around it, like
$myFolder = $PSScriptRoot;
gsudo {echo $args} -args $myFolder
Invoke-Gsudo {echo $args[0]} -argumentList $myFolder
Invoke-Gsudo {echo $using:myFolder}
gerardog commented
Or call gsudo { ./script.ps1 }
PS C:\Temp> '
>> echo "Welcome to $PSCommandPath"
>> echo "My value of `$PSScriptRoot is $PSScriptRoot"
>> echo "My value of gsudo {`$PSScriptRoot} is $(gsudo{$PSScriptRoot})"
>> echo "My value of invoke-gsudo {`$PSScriptRoot} is $(invoke-gsudo{$PSScriptRoot})"
>> echo "My value of invoke-command {`$PSScriptRoot} is $(invoke-command{$PSScriptRoot})"
>> ' | Out-File .\test.ps1
PS C:\Temp> .\test.ps1
Welcome to C:\Temp\test.ps1
My value of $PSScriptRoot is C:\Temp
My value of gsudo {$PSScriptRoot} is
My value of invoke-gsudo {$PSScriptRoot} is
My value of invoke-command {$PSScriptRoot} is C:\Temp
PS C:\Temp> gsudo .\test.ps1
Welcome to C:\Temp\test.ps1
My value of $PSScriptRoot is C:\Temp
My value of gsudo {$PSScriptRoot} is
My value of invoke-gsudo {$PSScriptRoot} is
My value of invoke-command {$PSScriptRoot} is C:\Temp
FlawlessCasual17 commented
Thanks, @gerardog, I figured it out thanks to your workaround tips.
gerardog commented
Sure, no problem