有没有办法启动与非管理员PowerShell控制台连接到同一控制台的PowerShell Admin SubShell?然后在完成后键入“exit”并返回“非管理员”PowerShell。我需要一些与“su”linux命令类似的东西。
目前我正在这样做的方式会弹出一个新的窗口,一段时间后会变得烦人,例如:
PS> start-process -Verb RunAs Powershell
更新:
我几乎可以按如下方式工作:
PS> start-process -nnw -wait powershell
这可以在同一个控制台中打开子shell。除非我添加“-Verb RunAs”以将其转换为Admin shell。它失败。
PS C:\Users\john> start-process -nnw -wait -Verb RunAs powershell
Start-Process : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ start-process -nnw -wait -Verb RunAs powershell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
UPDATE2:
我在微软网页上发现“-Verb RunAs”无法与-nnw一起使用..我的感觉是可以使用-Credential标志来指定管理员帐户。但是,由于我只是使用家用电脑,我认为我没有。升级到“RunAs”是否意味着您可以登录的用户帐户?我的感觉是我需要创建一个Admin帐户来执行此操作,以便我可以获得如下凭据:
PS> Set-Alias -name su -value start-process -nnw -wait -Credential(Get-Credential -User Admin)powershell
PS>用
????
UPDATE2:
尝试使用.NET Api,如下所示。它可以启动子shell,但仍然没有授予子shell的管理员权限:
function su {
$s = [System.Diagnostics.ProcessStartInfo] @{}
$s.FileName = "powershell"
$s.Verb = "runas"
$s.UseShellExecute = $false
$c = [System.Diagnostics.Process] @{}
$c.StartInfo = $s
$c.Start() | out-null
$c.WaitForExit()
}
function isadmin {
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
UPDATE3:
我的结论是,对于Diagnotics.Process,“Verb”参数要求UseShellExecute = $ true。因为Verb是一个提供给Windows Shell Launcher的Action。当UseShellExecute = $ false时,直接调用Execuatable,绕过Windows Shell Launcher及其动词操作。