gpt4 book ai didi

powershell - 尝试 self 提升时启动过程抛出错误

转载 作者:行者123 更新时间:2023-12-02 23:18:51 25 4
gpt4 key购买 nike

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

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 - 尝试 self 提升时启动过程抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com