这应该是一个非常明显的问题。。。我已经研究了提议的"重复"答案。。。不可能。。。只有我的"硬编码解决方案有效:-(我只想在StartJob-ScriptBlock{}命令中有一个参数(路径(。这是我的场景:
$MyVariable = "C:TestPathLoad.ps1"
Start-Job -ScriptBlock { C:TestPathLoad.ps1 ; while($true) { Get-DataX | Add-ToTable "XXX" ; Start-sleep -s 120}}
这很好用,但是硬编码的;以下是我尝试过但没有成功的许多解决方案中的几个:
Start-Job -ScriptBlock { Get-ChildItem "$MyVariable" ; while($true) { Get-DataX | Add-ToTable "XXX" ; Start-sleep -s 120}}
Start-Job -ScriptBlock { param($MyVariable) Get-ChildItem "$MyVariable" ; while($true) { Get-DataX | Add-ToTable "XXX"; Start-Sleep -s 10 }} -Name MyTest #-ArgumentList @($MyVariable)
Start-Job -ScriptBlock { Get-ChildItem "$MyVariable" ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}}
Start-Job -ScriptBlock { "$MyVariable" ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}}
Start-Job -ScriptBlock { '$MyVariable' ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}}
Start-Job -ScriptBlock { using:$MyVariable ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}}
Start-Job -ScriptBlock { "using:$MyVariable" ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}}
Start-Job -ScriptBlock { $args[0] ; while($true) { Get-DataX | Add-toTable "XXX" ; Start-sleep -s 10}} -Argumentlist@($MyVariable")
但它们都不起作用。。。那么,如何在StartJob-ScriptBlock{…}行中传递$MyVariable呢?也许我的问题来自其他地方?想法非常受欢迎。。。
谢谢!
Philippe
应该是这样的:
Start-Job -ScriptBlock {
param($MyVariable)
Get-ChildItem $MyVariable;
while($true)
{ Get-DataX ; Start-sleep -s 120}
} -ArgumentList $MyVariable
这是一篇很棒的文章,它将帮助您了解更多关于Powershell中的脚本块的信息。