在Powershell中找不到接受参数ERROR的位置参数

时间:2018-12-05 18:16:35

标签: powershell

我是Powershell的新手,并尝试使用另一个PS脚本运行一个PS脚本以在Jenkins中以管理员身份运行它,该脚本给出了所需的输出,但同时也给出了输出错误。 尽快需要帮助

  

Powershell:启动过程:找不到位置参数   在第4行接受参数char:1   + Powershell-命令“ Start-Process powershell”“ cd” F:\ RiskLink \ RiskL ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:未指定:(启动过程:... cepts参数:String)[],RemoteException       + FullyQualifiedErrorId:NativeCommandError

Set-Location -Path F:\abd\abc
$path = ".\xyz.ps1"
Powershell -Command "Start-Process powershell ""cd "F:\abd\abc"; & $path"" -Verb RunAs"

1 个答案:

答案 0 :(得分:1)

很久以前,我创建了一个简单的命令行解析器(脚本cliparser.ps1cli parser.ps1):

PS D:\PShell> Get-Content ".\cliparser.ps1"
    if ( $args -ne $null ) { $aLen=$args.Count } else { $aLen=0 }
    "$(Split-Path -Path $PSCommandPath -Leaf): $aLen parameters"
    for ( $i = 0; $i -lt $aLen ; $i++ ) { "{0}. [{1}]" -f $i, $args[$i] }

使用而不是您的xyz.ps1,它表明不仅Start-Process arguments以及引号都必须出错(显示提供的值-VerbRunAs)。稍作修改即可…改进了路径和文件名中的空格,请参见以下脚本:

$cd = (Get-Location -PSProvider Filesystem).Path
$path = ".\cliparser.ps1"
'... original (known error)'
Write-Host Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
pause
'... fixed'
Write-Host Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
$path = ".\cli parser.ps1"
Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
'done'

请注意,PShell的参数-NoProfile-NoExit仅用于调试目的。

结果

PS D:\PShell> D:\PShell\SO\53638416.ps1
... original (known error)
Powershell -Command Start-Process powershell "pushd  D:\PShell; & .\cliparser.ps1" -Verb RunAs
Start-Process : A positional parameter cannot be found that accepts argument 'D:\PShell'.
At line:1 char:1
+ Start-Process powershell pushd  D:\PShell; & .\cliparser.ps1 -Verb Ru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.Start
   ProcessCommand

cliparser.ps1: 2 parameters
0. [-Verb]
1. [RunAs]
Press Enter to continue...:
... fixed
Powershell -NoProfile -Command Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'';' -Verb RunAs
done
PS D:\PShell>

已调用的(提升的)Powershell窗口如下所示(Pop-Location用于确保popd运行良好):

administrator PShell

要在cmd提示符下运行并将参数传递给被调用的脚本,

powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"

enter image description here

适用于pwsh而不是powershell

pwsh -NoProfile -Command "Start-Process pwsh -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"