Given a powershell script that runs as Local System (nt authority\system
).
Is there a way to execute a command as the currently logged in user (without specifying the user password of course) ?
From what I've experimented so far is the Register-ScheduledTask
cmdlet which takes an -User
param. The task was scheduled and run successfully, but only works when the user is logged in.
Is there a better way to do it ?
相关问题
- How to Debug/Register a Permanent WMI Event Which
- How can I do variable substitution in a here-strin
- How to use a default value with parameter sets in
- Does powershell have a method_missing()?
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- EscapeDataString having differing behaviour betwee
- PowerShell Change owner of files and folders
- Command line escaping single quote for PowerShell
- Is there a simple way to pass specific *named* Pow
- How do I access an element with xpath with a names
- How to 'Grant Permissions' Using Azure Act
Here is an article that has code to run notepad as the currently logged in user: http://rzander.azurewebsites.net/create-a-process-as-loggedon-user/
Note: This solution looks like it requires the command be run from a system service due to how privileges are passed to child processes. Additionally the example does not work running from the system account because the working directory is not specified. Using the full path to the executable does work.
There is a complicated and round-about way of doing exactly this. It involves the API function
CreateProcessAsUser
. In order to call it, you need to obtain theTOKEN
associated with the current console session, which can be done with theWTSQueryUserToken
API function. This takes a session ID, which is obtained with theWTSGetActiveConsoleSessionId
API function. (Many examples then show the token being duplicated, but from what I've read, this is unnecessary sinceWTSQueryUserToken
already returns a primary token.)You also need to initialize an environment block for the process you're going to create. For that, you use the
CreateEnvironmentBlock
API function. Make sure you specify theCREATE_UNICODE_ENVIRONMENT
flag.Of course, PowerShell cannot call these functions directly -- so you will have to dynamically compile some C# helper code and load it into the PowerShell environment.
I use schtasks.exe. I am not sure if this can be done in pure PS.
You can use the portable powershell app deployment kit (Link). You can edit the deploy-application.ps1 with your code and run it elevated use/add Execute-ProcessAsUser in the script to run applications/scripts with the current user without a prompt for credentials.