不知道为什么尝试自我提升时出现此错误。

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

最佳答案

问题是您没有使用有效的参数集。这是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。您可以以其他用户身份运行该流程,也可以以同一用户但已提升身份的身份运行该流程,但是不能一次执行全部操作。您必须以其他用户身份运行它,然后从那里提升。

关于powershell - 尝试自我提升时启动过程抛出错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57632142/

10-15 04:05