Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line 将powershell中的命令行$args从一个函数传递到另一个函数_Command Line_Powershell_Nested_Arguments - Fatal编程技术网

Command line 将powershell中的命令行$args从一个函数传递到另一个函数

Command line 将powershell中的命令行$args从一个函数传递到另一个函数,command-line,powershell,nested,arguments,Command Line,Powershell,Nested,Arguments,这是我面临的一个棘手问题。如果它有一个简单的解决方案,我也不会感到惊讶,只是它在逃避我 我有2个批处理文件,我必须将其转换为powershell脚本 file1.bat --------- echo %1 echo %2 echo %3 file2.bat %* file2.bat -------- echo %1 echo %2 echo %3 在命令行中,我调用它作为 C:>file1.bat一二三 我看到的输出与预期一致 一 二 三 一 二 三 (这是一个粗略的代码示例) 当我转换到

这是我面临的一个棘手问题。如果它有一个简单的解决方案,我也不会感到惊讶,只是它在逃避我

我有2个批处理文件,我必须将其转换为powershell脚本

file1.bat
---------

echo %1
echo %2
echo %3
file2.bat %*

file2.bat
--------
echo %1
echo %2
echo %3
在命令行中,我调用它作为 C:>file1.bat一二三 我看到的输出与预期一致 一 二 三 一 二 三

(这是一个粗略的代码示例)

当我转换到Powershell时,我

file1.ps1
---------
Write-Host "args[0] " $args[0]
Write-Host "args[1] " $args[1]
Write-Host "args[2] " $args[2]
. ./file2.ps1 $args

file2.ps1
---------
Write-Host "args[0] " $args[0]
Write-Host "args[1] " $args[1]
Write-Host "args[2] " $args[2]

When I invoke this on powershell command line, I get
$> & file1.ps1 one two three
args[0] one
args[1] two
args[2] three
args[0] one two three 
args[1] 
args[2] 
我理解这是因为file1.ps中使用的$args是System.Object[]而不是3个字符串

我需要一种方法将file1.ps1接收到的$args传递到file2.ps1,与.bat文件中的%*实现的方法大致相同

我担心,如果它是一个跨函数调用,那么现有的方式将收支平衡,就像我的示例中的跨文件调用一样

我试过几种组合,但都不起作用


请帮忙。非常感谢。

PowerShell V2中,它与飞溅无关。酒吧变成了:

# use the pipe, Luke!

file1.ps1
---------
$args | write-host
$args | .\file2.ps1    

file2.ps1
---------
process { write-host $_ }
function bar { foo @args }
Splatting将数组成员视为单个参数,而不是将其作为单个数组参数传递

在PowerShell V1中,它很复杂,有一种方法可以用于位置参数。给定一个函数foo:

function foo { write-host args0 $args[0] args1 $args[1] args2 $args[2]   }
现在使用foo函数的scriptblock上的
Invoke()
方法从bar调用它

function bar { $OFS=',';  "bar args: $args";  $function:foo.Invoke($args) }
看起来像

PS (STA) (16) > bar 1 2 3 bar args: 1,2,3 args0 1 args1 2 args2 3 私人秘书(STA)(16)>酒吧1 2 3 条形参数:1,2,3 args0 1 args1 2 args2 3
当您使用它时。

您能解释一下这是怎么做的吗?它会打印传递到file1.ps1的参数,然后将这些参数传递到file2.ps1,并在那里再次打印(以显示它们按预期到达)