为什么我不能将 PowerShell 的 Start-Process 与 -Credential 和 -Verb 参数一起使用?

Shu*_*eng 5 powershell

使用powershell.exe,我想通过 UAC 提升权限的额外好处来模拟cmd.exerunas命令。

但是,如果我同时向 提供-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
Run Code Online (Sandbox Code Playgroud)

仅使用这些参数之一不会产生错误:

Start-Process -Verb RunAs powershell.exe -ArgumentList "-NoProfile"
Run Code Online (Sandbox Code Playgroud)

这是为什么?Start-Processaccept 的两种语法形式[<CommonParameters>]-Verb Runas属于哪一种?

Mar*_*agg 5

-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>]
Run Code Online (Sandbox Code Playgroud)

这不是的一部分CommonParameters,只是包括像-Debug-Verbose-ErrorAction等等(见的完整列表在这里)。

这似乎是一种可能的解决方法:

Start-Process powershell -Credential mydomain\myuser -ArgumentList '-noprofile -command &{Start-Process powershell -verb runas}'
Run Code Online (Sandbox Code Playgroud)