-1

这应该是一个非常明显的问题......我已经研究了提议的“重复”答案......没办法......只有我的“硬编码解决方案有效:-(我只想有一个参数(路径)在 Start-Job - ScriptBlock {} 命令中。这是我的场景:

$MyVariable = "C:\TestPath\Load.ps1"
Start-Job -ScriptBlock { C:\TestPath\Load.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")

但是它们都不起作用...那么,如何在 Start-Job -ScriptBlock {...} 行中传递 $MyVariable ?也许我的问题来自其他地方?想法非常受欢迎......

谢谢 !

菲利普

4

1 回答 1

0

这应该是这样的:

Start-Job -ScriptBlock {
    param($MyVariable) 
        Get-ChildItem $MyVariable; 
        while($true) 
        { Get-DataX ; Start-sleep -s 120}
    } -ArgumentList $MyVariable

这是一篇很棒的文章,它将帮助您了解有关 Powershell 中脚本块的更多信息。

于 2018-11-17T13:05:45.087 回答