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.
7
u/Jmoste Jul 23 '24
Have the script create a scheduled task to run as builtin/users and set it to run once at a specific time period from now. It's not elegant but it works. It's a good idea to then have something to delete the scheduled task after it runs
3
u/freebase1ca Jul 23 '24
I'm not sure about targeting a user session you don't have credentials for, but there are ways of coping. If a script started in the user session before spawning a new script/session with an elevated token, just don't let the initial script terminate. Have it stay open and wait for some kind of signal from the elevated script to do something new like run an app or create a shortcut. You're basically running two scripts in parallel at that point.
If you don't have that initial user session, but need to target a different user or perhaps all users on a machine, you don't necessarily need that user's token. As admin you have access to the whole machine. Catalog the users on the system. Target their registry hives and user folders and make whatever changes an app would have made yourself via the script as admin.
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.
2
u/BlackV Jul 23 '24 edited Jul 23 '24
Run it NOT elevated and just elevate the specific tasks that need it, would that be a solution
7
u/pjkm123987 Jul 23 '24
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File
"$PSCommandPath"" -Verb RunAs; exit }4
u/PeeCee1 Jul 23 '24
PsappDeployToolkit has functions for that. Or write your own using scheduled Tasks.
3
u/red_the_room Jul 24 '24
https://github.com/KelvinTegelaar/RunAsUser
2
u/yaboiWillyNilly Jul 24 '24 edited Jul 24 '24
To bypass the problem you’re having of placing icons on the wrong users desktop, use the get-wmiobject cmdlet to get the instance of the app that’s running, and then from that you should be able to find your user, somewhere under the properties; I forget what it’s called rn but I’ll look at it tomorrow and update. Store that user into a variable and then use that variable to carry out your function that stores the icon in the desktop.
Edit: I have misunderstood what you’re trying to do…
I guess it would help to know a little more about the specific use case, there’s more than likely a pretty easy way to try to do what you’re doing. Pm if you want to discuss.
2
u/icepyrox Jul 24 '24
I think I'd go the other way around and launch as a user and then let the script launch powershell as admin for the other parts that need it.
I did find this.
https://stackoverflow.com/questions/31449633/how-can-i-make-powershell-run-a-program-as-a-standard-user
Basically, launch it with
psexec.exe -l "desk.cpl,5". That thread is 9 years old, but the sysinternals tools haven't really changed so could work.1
u/Natural_Raise4005 Nov 26 '24
Bro easy way in C# (just use explorer.exe privilages):
public static void RunAsStandardUser(string applicationPath)
{
try
{
// Użyj explorer.exe jako nadrzędnego procesu
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = $"\"{applicationPath}\"",
UseShellExecute = true,
CreateNoWindow = false
};
Process.Start(psi
Console.WriteLine($"Successfully started: {applicationPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error starting application: {ex.Message}");
}
}