简体   繁体   English

如何从提升的 PowerShell 控制台以非管理员身份运行进程?

[英]How to run a process as non-admin from an elevated PowerShell console?

Maybe there is a way to do it with Start-Process cmdlet that I cannot find?也许有一种方法可以使用我找不到的Start-Process cmdlet 来完成? The other related Q/A's I found on StackOverflow such as this , this and this all give a solution to do this with a custom C# code.我在 StackOverflow 上发现的其他相关问答,例如thisthisthis都提供了使用自定义 C# 代码执行此操作的解决方案。 My question is specifically, is there any straightforward way to do this in PowerShell?我的问题是,在 PowerShell 中是否有任何直接的方法可以做到这一点? ie you are in an elevated PS console and want to run a process as non-admin.即您在一个提升的 PS 控制台中并且想要以非管理员身份运行一个进程。

You can specify the TrustLevel with runas.exe , effectively running "restricted"您可以使用runas.exe指定TrustLevel ,有效地运行“受限”

runas /trustlevel:0x20000 "powershell.exe -command 'whoami /groups |clip'"

You should see in the output from whoami that the Administrators group in your token is marked as "Used for Deny only"您应该在whoami的输出中看到您令牌中的Administrators组被标记为“仅用于拒绝”


在此处输入图片说明

When you dig into this problem, as mentioned by the linked tasks, there is no way to run a UAC "non" elevated process from a elevated process.当您深入研究此问题时,如链接任务所述,无法从提升的进程运行 UAC“非”提升的进程。 Since this is exactly what I required and the runas solution didn't work for me I converted the code workaround supplied by Microsoft to use a scheduled task to Start a "non" elevated process.由于这正是我所需要的,并且 runas 解决方案对我不起作用,因此我将 Microsoft 提供的代码解决方法转换为使用计划任务来启动“非”提升进程。

Example of running powershell.exe as a "non" elevated process from a elevated powershell prompt:从提升的 powershell 提示符将 powershell.exe 作为“非”提升进程运行的示例:

$apppath = "powershell.exe"
$taskname = "Launch $apppath"
$action = New-ScheduledTaskAction -Execute $apppath
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname | Out-Null
Start-ScheduledTask -TaskName $taskname
Start-Sleep -s 1
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false

The above powershell commands only work on Windows Server 2012 / Windows 8 and greater only.上述 powershell 命令仅适用于 Windows Server 2012 / Windows 8 及更高版本。

Or you can use the SCHTASKS.EXE application instead to cover most versions of windows:或者您可以使用SCHTASKS.EXE应用程序来覆盖大多数版本的 Windows:

$apppath = "powershell.exe"
$taskname = "Launch $apppath"
schtasks /create /SC ONCE /ST 23:59 /TN $taskname /TR $apppath
schtasks /run /tn $taskname
Start-Sleep -s 1
schtasks /delete /tn $taskname /F

You can run a non-admin process from an elevated session by passing in the credential of the user you want to run as.您可以通过传入您要作为其运行的用户的凭据,从提升的会话中运行非管理员进程。 You can use Get-Credential if you want to run interactively, or you can use Import-Clixml or SecretStore or some other established mechanism for storing and retrieving credentials if you want a script to run unattended.如果您想以交互方式运行,您可以使用Get-Credential如果您希望脚本在无人参与的情况下运行,您可以使用Import-ClixmlSecretStore或其他一些已建立的机制来存储和检索凭证。 Eg:例如:

$credential = Get-Credential -UserName $Env:USERNAME
# or
$credential = Import-Clixml -Path 'C:\MyCredential.cred'

Start-Process -FilePath pwsh.exe -ArgumentList '-noprofile' -Credential $credential -Wait

Another, limited, way: Make Windows Explorer launch it for you另一种有限的方式:让 Windows 资源管理器为您启动它

PS C:\> explorer.exe "C:\windows\system32\cmd.exe"

Just use the full path and extension.只需使用完整路径和扩展名。 But it does NOT accept parameters.它不接受参数。

I tried creating a batch file.我尝试创建一个批处理文件。 But explorer refuses to launch .BAT or .CMD .但是资源管理器拒绝启动.BAT.CMD

in start-process exist switch runas likestart-process存在开关runas类的

start-process powershell -verb runAs

but still uac check you if in your system uac on you should first bypass uac there are many way exist for bypass uac but all ways doesn't work in all windows like windows 8 if you write script for run process then compile to exe you can use program like runasadmin for run as admin your exe in system but still not work in windows 8但仍然 uac 检查你是否在你的系统 uac 上你应该首先绕过uac有很多方法可以绕过 uac 但如果你为运行进程编写脚本然后编译为 exe你可以使用像runasadmin这样的程序在系统中以管理员身份运行您的 exe,但仍然无法在 Windows 8 中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安装程序以管理员权限运行后如何在非管理员窗口中打开应用程序 - How to open the app in non-admin window after the installer run with Admin privileges 限制非管理员用户在任务管理器中停止进程 C++ - Restricting Non-Admin Users From Stopping a Process in Task Manager C++ 如何在Windows 10上从提升的管理员上下文启动非提升的管理员进程? - How to launch non-elevated administrator process from elevated administrator context on Windows 10? 允许非管理员用户进程读取文件 - Allow File to be Read by Non-Admin User Process 为以非管理员用户帐户运行的进程创建防火墙规则 - Creating Firewall rule for a process running in a Non-admin user account 如何从提升的批处理文件运行Powershell以使用当前用户? - How to run powershell from an elevated batch file to use current user? 我如何以Windows非管理员身份从管理员帐户登录而没有密码 - How do I login as a windows non-admin from Admin account without a password 使用CreateProcessWithTokenW()从管理员帐户以非提升模式启动进程 - Launching process in non-elevated mode from an admin account using CreateProcessWithTokenW() 以非管理员用户身份在Windows中运行重复任务的最佳方法? - Best way to run a recurring task in windows as a non-admin user? VBS脚本以标准(非管理员)用户身份运行 - VBS script to run as standard (non-admin) user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM