psutils icon indicating copy to clipboard operation
psutils copied to clipboard

'sudo' ignores profile scripts?

Open SharakPL opened this issue 6 years ago • 4 comments

I have my powershell profile in C:\Users\Username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. I added some custom functions and aliases there that need admin privileges which I can access without problems while running powershell as administrator and typing MyFunction but on non-elevated powershell sudo MyFunction doesn't even recognize the function name and throws error:

MyFunction : The term 'MyFunction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ MyFunction
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MyFunction:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SharakPL avatar Jun 03 '19 22:06 SharakPL

This seems to have been the case for quite a while now. -noprofile option was added in https://github.com/lukesampson/psutils/commit/e03d8dbedc58f79ff6ce641cfaebb5ed16a24d19#diff-5f76b86dab02c0a00ffbf73bf10c19caR17, not sure if intentional, though.

daniel-liuzzi avatar Jun 22 '19 18:06 daniel-liuzzi

So how should I import my powershell scripts to be able to use it with sudo?

SharakPL avatar Jun 25 '19 13:06 SharakPL

https://github.com/lukesampson/psutils/blob/c7116ac143ca81f223e6091d0974f45ac241eb1d/sudo.ps1#L32 @lukesampson why does it use -noprofile? Seems to work just fine without it.

SharakPL avatar Jan 27 '20 13:01 SharakPL

Was going to open an Issue exactly about this. Shouldn't it just use PS Profiles if present? It might delay the command execution, though.

brunovieira97 avatar May 26 '20 19:05 brunovieira97