我想使用 start-job 运行需要参数的 .ps1 脚本。这是脚本文件:
#Test-Job.ps1
Param (
[Parameter(Mandatory=$True)][String]$input
)
$output = "$input to output"
return $output
这是我运行它的方式:
$input = "input"
Start-Job -FilePath 'C:\PowerShell\test_job.ps1' -ArgumentList $input -Name "TestJob"
Get-Job -name "TestJob" | Wait-Job | Receive-Job
Get-Job -name "TestJob" | Remove-Job
像这样运行,它返回“到输出”,所以 $input 在作业运行的脚本中为空。
我见过与此类似的其他问题,但他们大多使用 -Scriptblock 代替 -FilePath。通过 Start-Job 将参数传递给文件是否有不同的方法?