尝试自我提升时启动过程抛出错误

时间:2019-08-23 19:40:22

标签: powershell powershell-4.0

不确定为什么尝试自我提升时会出现此错误。

param(
    $adminUser
)

# Self-elevate the script to administrator if required

if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
 if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
  $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
  Start-Process -FilePath PowerShell.exe -Verb Runas -Credential "Chrysalis\$adminUser" -ArgumentList $CommandLine
  Exit
 }
}

错误:

Start-Process : Parameter set cannot be resolved using the specified named parameters.
At M:\IT Department\Scripts\Newest Client Script (Monthly Printing)\MonthlyPrintingFoldersAndShortcuts.ps1:10 char:3
+   Start-Process -FilePath PowerShell.exe -Verb Runas -Credential "Chr ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand

1 个答案:

答案 0 :(得分:3)

问题是您没有使用有效的参数集。这是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>]

您会注意到第一个选项具有-Credential(尽管您需要一个PSCredential对象,而不仅仅是一个用户名),但是它没有-Verb参数。第二个选项确实有-Verb,但没有-Credential。您可以以其他用户身份运行该进程,也可以以同一用户但已提升身份的身份运行该进程,但是不能一次执行全部操作。您必须以其他用户身份运行它,然后从那里进行提升。