将参数传递给 PowerShell 作业
Passing parameters to a PowerShell job
我一直在研究传递给 powershell 作业的该死参数。
我需要在调用作业的脚本中获取两个变量,并将其放入作业中。首先,我尝试使用 -ArgumentList,然后在我提供的 -ScriptBlock 中使用 $args[0] 和 $args[1]。
function Job-Test([string]$foo, [string]$bar){
Start-Job -ScriptBlock {#need to use the two args in here
} -Name "Test" -ArgumentList $foo, $bar
}
但是我意识到 -ArgumentList 将这些作为参数提供给 -FilePath,所以我将脚本块中的代码移到它自己的需要两个参数的脚本中,然后将 -FilePath 指向该脚本。
function Job-Test([string]$foo, [string]$bar){
$myArray = @($foo,$bar)
Start-Job -FilePath .\Prog\august\jobScript.ps1 -Name 'Test' -ArgumentList $myArray
}
#\Prog\august\jobScript.ps1 :
Param(
[array]$foo
)
#use $foo[0] and $foo[1] here
还是不行。我尝试将信息放入一个数组,然后只传递一个参数,但仍然不知道有用。
当我说无济于事时,我得到了我需要的数据,但它似乎都被压缩到第一个元素中。
例如,假设我将文件名作为 $foo 传递,它的路径作为 $bar,对于我尝试的每种方法,我都会得到 args[0] 作为 "filename path" 和 args[1]将是空的。
即:
function Job-Test([string]$foo, [string]$bar){
$myArray = @($foo,$bar)
Start-Job -FilePath .\Prog\august\jobScript.ps1 -Name 'Test' -ArgumentList $myArray
}
然后我打电话给:
$foo = "hello.txt"
$bar = "c:\users\world"
Job-Test($foo,$bar)
我有 jobScript。ps1 只需将两个变量输出到不同行的日志中,它看起来像这样:
log.txt:
hello.txt c:\users\world
#(empty line)
它应该在的地方:
hello.txt
c:\users\world
您不需要像在 java 中那样调用该函数。只需将两个变量附加到函数调用的末尾 Job-Test $foo $bar
我一直在研究传递给 powershell 作业的该死参数。
我需要在调用作业的脚本中获取两个变量,并将其放入作业中。首先,我尝试使用 -ArgumentList,然后在我提供的 -ScriptBlock 中使用 $args[0] 和 $args[1]。
function Job-Test([string]$foo, [string]$bar){
Start-Job -ScriptBlock {#need to use the two args in here
} -Name "Test" -ArgumentList $foo, $bar
}
但是我意识到 -ArgumentList 将这些作为参数提供给 -FilePath,所以我将脚本块中的代码移到它自己的需要两个参数的脚本中,然后将 -FilePath 指向该脚本。
function Job-Test([string]$foo, [string]$bar){
$myArray = @($foo,$bar)
Start-Job -FilePath .\Prog\august\jobScript.ps1 -Name 'Test' -ArgumentList $myArray
}
#\Prog\august\jobScript.ps1 :
Param(
[array]$foo
)
#use $foo[0] and $foo[1] here
还是不行。我尝试将信息放入一个数组,然后只传递一个参数,但仍然不知道有用。
当我说无济于事时,我得到了我需要的数据,但它似乎都被压缩到第一个元素中。
例如,假设我将文件名作为 $foo 传递,它的路径作为 $bar,对于我尝试的每种方法,我都会得到 args[0] 作为 "filename path" 和 args[1]将是空的。
即:
function Job-Test([string]$foo, [string]$bar){
$myArray = @($foo,$bar)
Start-Job -FilePath .\Prog\august\jobScript.ps1 -Name 'Test' -ArgumentList $myArray
}
然后我打电话给:
$foo = "hello.txt"
$bar = "c:\users\world"
Job-Test($foo,$bar)
我有 jobScript。ps1 只需将两个变量输出到不同行的日志中,它看起来像这样:
log.txt:
hello.txt c:\users\world
#(empty line)
它应该在的地方:
hello.txt
c:\users\world
您不需要像在 java 中那样调用该函数。只需将两个变量附加到函数调用的末尾 Job-Test $foo $bar