简体   繁体   English

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

[英]Why can't I use PowerShell's Start-Process with both -Credential and -Verb parameters?

Using powershell.exe , I want to emulate cmd.exe 's runas command with the additional benefit of escalating privileges through UAC. 使用powershell.exe ,我想模拟cmd.exerunas命令,并具有通过UAC升级特权的额外好处。

However, if I both supply both -Credential and -Verb Runas parameters to Start-Process , I get the error below: 但是,如果我同时向Start-Process提供-Credential-Verb Runas参数,则会出现以下错误:

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

Using only one of these parameters yields no errors: 仅使用以下参数之一不会产生任何错误:

Start-Process -Verb RunAs powershell.exe -ArgumentList "-NoProfile"

Why is that? 这是为什么? Both syntax forms of Start-Process accept [<CommonParameters>] , which -Verb Runas belongs to? Start-Process两种语法形式都接受[<CommonParameters>] ,- [<CommonParameters>] -Verb Runas属于哪个?

The -Verb parameter is only available in one of the parameter sets (if you do Get-Help Start-Process you can see it explictly listed in the second set): -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>]

It's not a part of CommonParameters , that just includes things like -Debug , -Verbose , -ErrorAction etc. (see the full list here ). 它不是CommonParameters的一部分,仅包含-Debug-Verbose-ErrorAction等(请参见此处的完整列表)。

This seems to be a possible workaround: 这似乎是一个可能的解决方法:

Start-Process powershell -Credential mydomain\myuser -ArgumentList '-noprofile -command &{Start-Process powershell -verb runas}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM