r/PowerShell • u/fordaytimestuff • Jul 23 '24
How to run an app as the current standard user from a script opened as an administrator?
I am trying to run an app as a standard user from a script executed as an administrator.
The script needs to request administrator privileges at the start and then displays a window with options to execute.
The problem arises when I try to open an app as a standard user instead of as an administrator.
For a more specific example: From the window, "desk.cpl ,5" is executed, which opens the window to add icons to the desktop, but since it is run as an administrator, it does not place the icons on the current user's desktop, who is a standard user.
Can someone guide me, please? Thank you.
View all comments
3
u/IJustKnowStuff Jul 23 '24 edited Jul 29 '24
There's a PowerShell script/function I use specifically for this. I beleive it needs to be run as SYSTEM, which is fine and easy if triggered from SCCM. It will run whatever you configure as the currently logged on user.
UPDATE:
Ok the source of the C# code that needs to be used is from murrayju: https://github.com/murrayju/CreateProcessAsUser
Specifically this C# code file is the $Source in the below code snippet (It's too large to include in this reddit comment): https://github.com/murrayju/CreateProcessAsUser/blob/master/ProcessExtensions/ProcessExtensions.cs
The piped out-null when running StartProcessAsCurrentUser stops "True" from being returned everytime.
Be aware this won't wait for the process to finish executing. If you trigger a script that takes a few minutes, this script won't be aware of it and return immediatly. You'll have to figure out some logic to check for that. One way is I get it to keep an eye on a log file to a specifc string. Also you can run a several PS commands at once.
e.g. This is a trimmed down version of something I use it for. This example execute fast, but the one run has additional UserParams that take longer
Update 2 [2024-07-30 0041+10]: Updated IF statement at the beginning to correct name. I actually have [murrayju.ProcessExtensions.ProcessExtensions] renamed as [RunAsCurrentUser.ProcessExtensions.ProcessExtensions] in my code, and I reverted it to back to the orignal make to copying from murrayju's code easier for you guys, but missed updating that bit.