我想使用powershell.exe
来模仿cmd.exe
的{{1}}命令,并具有通过UAC提升特权的额外好处。
但是,如果我同时向runas
提供了-Credential
和-Verb Runas
参数,则会出现以下错误:
Start-Process
仅使用以下参数之一不会产生错误:
Start-Process powershell.exe -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'username',(ConvertTo-SecureString 'password' -AsPlainText -Force)) -ArgumentList '-NoProfile' -Verb RunAs
Start-Process : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Start-Process powershell.exe -Credential (New-Object -TypeName System ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
那是为什么? Start-Process -Verb RunAs powershell.exe -ArgumentList "-NoProfile"
的两种语法形式都接受Start-Process
所属的[<CommonParameters>]
?
答案 0 :(得分:3)
-Verb
参数仅在其中一个参数集中可用(如果执行Get-Help Start-Process
,则它会明确显示在第二个参数集中):
SYNTAX
Start-Process [-FilePath] <String> [[-ArgumentList] <String[]>] [-Credential <PSCredential>] [-LoadUserProfile] [-NoNewWindow] [-PassThru] [-RedirectStandardError <String>] [-RedirectStandardInput <String>] [-RedirectStandardOutput <String>] [-UseNewEnvironment] [-Wait] [-WindowStyle {Normal | Hidden | Minimized | Maximized}] [-WorkingDirectory <String>]
[<CommonParameters>]
Start-Process [-FilePath] <String> [[-ArgumentList] <String[]>] [-PassThru] [-Verb <String>] [-Wait] [-WindowStyle {Normal | Hidden | Minimized | Maximized}] [-WorkingDirectory <String>] [<CommonParameters>]
它不是CommonParameters
的一部分,只包括-Debug
,-Verbose
,-ErrorAction
等(请参见完整列表here)。
这似乎是一种可能的解决方法:
Start-Process powershell -Credential mydomain\myuser -ArgumentList '-noprofile -command &{Start-Process powershell -verb runas}'