尝试自我提升时启动进程抛出错误
Start-Process throwing Error When trying to Self Elevate
不确定为什么我在尝试自行提升时收到此错误。
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
。您可以 运行 以另一个用户的身份执行该过程,或者您可以以同一用户的身份 运行 它但已提升,但您不能同时执行这两项操作。您必须像其他用户一样 运行 它,然后从那里提升。
不确定为什么我在尝试自行提升时收到此错误。
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
。您可以 运行 以另一个用户的身份执行该过程,或者您可以以同一用户的身份 运行 它但已提升,但您不能同时执行这两项操作。您必须像其他用户一样 运行 它,然后从那里提升。