ale*_*042 6 powershell uac admin
我想要一种简单的方法来运行具有来自同一用户的不同权限的进程,而无需询问或知道他/她的密码.如有必要,可以使用对话框.我宁愿不启动PowerShell子流程来实现这一目标.
方案1: PowerShell脚本以管理模式运行.我想在没有管理员权限但是在同一个用户上启动脚本或.exe.
方案2: PowerShell脚本在正常模式下运行.我想在同一个用户上启动具有管理员权限的脚本或.exe.
我们将其分为三个部分.
首先确定当前会话是否以管理员权限运行:
$CurrentID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$CurrentPrincipal = new-object System.Security.Principal.WindowsPrincipal($CurrentID)
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if session is currently with admin privileges
if ($CurrentPrincipal.IsInRole($adminRole)) {
write-host "Yes we are running elevated."
}else{
write-host "No this is a normal user session."
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我们使用或不使用提升运行,您可以使用提升的权限启动新进程,如下所示:
$newProc = new-object System.Diagnostics.ProcessStartInfo "PowerShell"
# Specify what to run
$newProc.Arguments = "powershell.exe"
# If you set this, process will be elevated
$newProc.Verb = "runas"
[System.Diagnostics.Process]::Start($newProc)
Run Code Online (Sandbox Code Playgroud)
最后,如果我们拥有提升的权限,但想要在没有...的情况下启动新流程
我不知道.将不得不试图找到答案,但由于这不是一个常见的情况,我到目前为止没有运气.
编辑:我现在已经看到了这个场景的几个"解决方案".在.NET/PowerShell中没有本地方法可以执行此操作.有些非常复杂(调用12个COM对象).这个vista-7-uac-how-to-lower-process-privileges是一个很好的参考.
对我来说似乎最优雅的是利用explorer.exe中的"bug".只需使用explorer.exe启动.exe,生成的进程将再次运行,不会提升权限.
$newProc = new-object System.Diagnostics.ProcessStartInfo "PowerShell"
# Specify what to run, you need the full path after explorer.exe
$newProc.Arguments = "explorer.exe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
[System.Diagnostics.Process]::Start($newProc)
Run Code Online (Sandbox Code Playgroud)
编辑#2:我刚刚发现从已经提升的环境中启动一个新的非提升进程的另一种方法是使用具有0x20000(基本用户)信任级别的runas.exe:
C:\> runas /showtrustlevels
The following trust levels are available on your system:
0x20000 (Basic User)
C:\> runas /trustlevel:0x20000 devenv