为什么不能同时使用-Credential和-Verb参数的PowerShell的Start-Process?

时间:2019-06-04 09:02:14

标签: powershell

我想使用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>]

1 个答案:

答案 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}'