Powershell等效于$@&引用;来自bash
我对powershell了解不多,但我想从powershell运行另一个脚本,并传递所有未使用的参数。在bash中,我是这样做的(简化): 我在powershell中尝试了很多东西,但都没有效果。在我的python脚本中,我要么接收到“Powershell等效于$@&引用;来自bash,bash,powershell,Bash,Powershell,我对powershell了解不多,但我想从powershell运行另一个脚本,并传递所有未使用的参数。在bash中,我是这样做的(简化): 我在powershell中尝试了很多东西,但都没有效果。在我的python脚本中,我要么接收到“System.Object[]”(或者“System.Collections.Generic.List`1[System.Object]”),要么接收到作为第一个参数包装在单个字符串中的所有参数(当然还有各种错误消息)。我试过: python otherscri
System.Object[]
”(或者“System.Collections.Generic.List`1[System.Object]
”),要么接收到作为第一个参数包装在单个字符串中的所有参数(当然还有各种错误消息)。我试过:
python otherscript.py$args
调用表达式“python otherscript.py$args”
- 我还尝试使用
或$MyInvocation.Line
$MyInvocation.UnboundArguments
作为注释,使用
python otherscript.py$args
从“全局范围”调用python脚本的工作与我预期的一样
我实际上想做的是从函数中调用python脚本:
param (
[string]$command = ""
)
function Invoke-Other() {
python otherscript.py $args
}
switch ($command) {
"foo" {
Invoke-Other $args
}
}
Invoke-Other @args
这就是设置,当我得到一个“包装”参数“bar baz”时,当我使用
\myscript.ps1 foo bar baz调用我的powershell脚本时,当您调用调用其他$args
时,参数列表作为单个(数组)参数传递,因此所有脚本参数最终作为$args[0]中的嵌套数组
函数内部。您可以通过检查函数内部和外部的$args.Count
值来验证这一点。然后,当您使用函数的参数列表调用Python脚本时,嵌套数组会被破坏成字符串
用于将脚本的参数列表作为单个参数传递给函数:
param (
[string]$command = ""
)
function Invoke-Other() {
python otherscript.py $args
}
switch ($command) {
"foo" {
Invoke-Other $args
}
}
Invoke-Other @args
python otherscript.py$args
在从PowerShell(PS 5.1、python 3.6、Win10)调用时非常适合我。您如何调用powershell脚本以及向其传递哪些参数?