Beej126 / SingleInstanceAccumulator

Useful for multi-file shell context menu actions
10 stars 2 forks source link

powershell piping nuances #11

Open Beej126 opened 5 days ago

Beej126 commented 5 days ago

(bringing this question over from commit comment... @vicomannen, no big deal here, but i've been coached it's typically best to ask questions in issues vs commit comments, so everybody can see & learn)

Hi Beej126! I would like to report you some bugs with SIA. I'm using the latest version and I'm trying to get the acls of selected files with powershell (in regedit, in the command key). I have discovered that you can't pipe anything (with |). Simple commands like using | Select-Object doesn't work =/ are you aware about it?

An example: [HKEY_CLASSES_ROOT*\shell\OwnPermChangeMenu\shell\a_Change_Owner\shell\01View_Owner\Command] "%ENVAR%\SingleInstanceAccumulator.exe" -f "-c:hstart64 /RUNAS /NOUAC ""cmd nircmd elevatecmd runassystem /c powershell -NoExit -Command $tmp = [System.Collections.ArrayList]@(Get-ACL (GC '$files')); $tmp | Format-List""" "%1"

hstart64 and nircmd are something that I use to bypass UAC and view system protected files, it works fine until you pipe something. It closes the terminal immediately, feel free to try it out yourself to reproduce the behaviour. Just wanted to let you know, maybe my commands works if I make a script of it instead... hmm? I will test it, have a nice day =) BR Vico

after poking around on this i'm going to label as "not bug", but i'm always open to further enlightenment =)

here's a working facsimile to the repro you provided:

"%bin%\SingleInstanceAccumulator.exe" -v -f "-c:hstart64 ""cmd nircmd elevatecmd runassystem /c powershell -NoExit -Command ""& { $tmp = [System.Collections.ArrayList]@(Get-ACL (GC '$files')); $tmp | Format-List }""""" "%1"

the main fix is enclosing all the powershell commands, including the pipe, in a script block (i.e. {...}) this also requires using "&" (powershell call operator) when executed from cmd.exe

here's a decent thread elaborating on call operator - script block

the hstart trial popup is handy to verify what is going to execute:

and this looks like a successful result from me passing in two files:

for future troubleshooting:

vicomannen commented 5 days ago

Oh! Thank you very much man ❤️ I ended up making a script, it took me a while but I managed to make it work. Thank you for putting your effort to bug check it xD I was thinking that you wouldn't read your users comments that's why i e-mailed you and thb I don't even know how to send comments on GitHub (I'm a GitHub noob sry). I don't mind you pasting it as comments so it's cool ^.^ Thanks for the tip about the verbose/debug thing, I just forgot that option, I hope I didn't annoy you lol. I'm going to test your code and report it back to you as a comment next time. Have a wonderful day man 😄 \Vicomannen


Från: Beej @.> Skickat: den 23 juni 2024 02:58 Till: Beej126/SingleInstanceAccumulator @.> Kopia: vicomannen @.>; Mention @.> Ämne: [Beej126/SingleInstanceAccumulator] powershell piping nuances (Issue #11)

(bringing this question over from commit commenthttps://github.com/Beej126/SingleInstanceAccumulator/commit/466499954b9acf13551d8638ccffe8d2ba29e213#commitcomment-143427487... @vicomannenhttps://github.com/vicomannen, no big deal here, but i've been coached it's typically best to ask questions in issues vs commit comments, so everybody can see & learn)

Hi Beej126! I would like to report you some bugs with SIA. I'm using the latest version and I'm trying to get the acls of selected files with powershell (in regedit, in the command key). I have discovered that you can't pipe anything (with |). Simple commands like using | Select-Object doesn't work =/ are you aware about it?

An example: [HKEY_CLASSES_ROOT*\shell\OwnPermChangeMenu\shell\a_Change_Owner\shell\01View_Owner\Command] "%ENVAR%\SingleInstanceAccumulator.exe" -f "-c:hstart64 /RUNAS /NOUAC ""cmd nircmd elevatecmd runassystem /c powershell -NoExit -Command $tmp = [System.Collections.ArrayList]@(Get-ACL (GC '$files')); $tmp | Format-List""" "%1"

hstart64 and nircmd are something that I use to bypass UAC and view system protected files, it works fine until you pipe something. It closes the terminal immediately, feel free to try it out yourself to reproduce the behaviour. Just wanted to let you know, maybe my commands works if I make a script of it instead... hmm? I will test it, have a nice day =) BR Vico

after poking around on this i'm going to label as "not bug", but i'm always open to further enlightenment =)

here's a working facsimile to the repro you provided:

"%bin%\SingleInstanceAccumulator.exe" -v -f "-c:hstart64 ""cmd nircmd elevatecmd runassystem /c powershell -NoExit -Command ""& { $tmp = [System.Collections.ArrayList]@(Get-ACL (GC '$files')); $tmp | Format-List }""""" "%1"

the main fix is enclosing all the powershell commands, including the pipe, in a script block (i.e. {...}) this also requires using "&" (powershell call operator) when executed from cmd.exe

here's a decent thread elaborating on call operator - script blockhttps://superuser.com/questions/1804902/run-powershell-command-from-cmd-with-parameters/1815230#1815230

the hstart trial popup is handy to verify what is going to execute:

image.png (view on web)https://github.com/Beej126/SingleInstanceAccumulator/assets/6301228/79e96e96-191e-4ddc-a17c-3ef8efbe5b29

and this looks like a successful result from me passing in two files:

image.png (view on web)https://github.com/Beej126/SingleInstanceAccumulator/assets/6301228/faf5bdf1-acc9-4ede-ba20-83dd81e1df89

for future troubleshooting:

C:\Users\Beej>hstart64 powershell -NoExit -Command $tmp = [System.Collections.ArrayList]@(Get-ACL (GC 'C:\Users\Beej\AppData\Local\Temp\tmp7DD6.tmp')); $tmp | Format-List

'Format-List' is not recognized as an internal or external command, operable program or batch file.

— Reply to this email directly, view it on GitHubhttps://github.com/Beej126/SingleInstanceAccumulator/issues/11, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AYHCI3GMICXMLOWSZ5BDCETZIYMTDAVCNFSM6AAAAABJX3ZKWSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM3DQMJQGE4DEOI. You are receiving this because you were mentioned.Message ID: @.***>

Beej126 commented 5 days ago

@vicomannen, you're very welcome. you're no bother at all. it's always nice to see something from my hands being useful to someone else. from the looks of your email header words it looks like i should say varmaste hälsningar =)

vicomannen commented 5 days ago

Thanks m8! =) I experimented with different codes and I have discovered new things like the fact that you don't even need to use cmd /c in the beginning, nircmd elevatecmd runassystem powershell.. that's enough. One important thing with writing your code in regedit (in the command key) if you want to do some advanced things like using backtick letters like "n", "t" etc is that regedit require you to escape backtick letters with back slash x2 and one more quotation marks, e.g. \""nn`tstring-text\"" (i was using it to get more space between the lines), so remember all folks :p writing codes in regedit is not as simple as typing it in ps console. Good luck with your coding guys and varma hälsningar ;)