我有一个脚本块:
$RoboArgs = @{
Source = '01'
Target = '02'
ExtraArgs = '/e', '/purge'
}
Write-Host @RoboArgs
Start-ThreadJob -InputObject $RoboArgs -ScriptBlock {
. robocopy_invoke.ps1
Write-Host @input
} | Receive-Job -Wait -AutoRemoveJob
我想使用输入参数(Invoke-Robocopy @RoboArgs
(调用robocopy_invoke.ps1模块中定义的函数,但一旦输入参数进入脚本块,其内容就会发生变化。这是输出:
-Target: 02 -ExtraArgs: /e /purge -Source: 01
System.Management.Automation.Runspaces.PipelineReader`1+<GetReadEnumerator>d__20[System.Object]
为什么两个Write-Host
调用的输出不同?我怎样才能把第二个和第一个一样?
使用"-InputObject"将对象导入作业(使用$input检索(。所以Start-ThreadJob -InputObject $RoboArgs -ScriptBlock {}
等价于$RoboArgs | Start-ThreadJob -ScriptBlock {}
您需要的是"-ArgumentList"(使用$args检索(:
$RoboArgs = @{
Source = '01'
Target = '02'
ExtraArgs = '/e', '/purge'
}
$RoboArgs
Start-ThreadJob -ArgumentList $RoboArgs -ScriptBlock {
$args
} | Receive-Job -Wait -AutoRemoveJob
示例2(解压缩$args数组(
$RoboArgs = @{
Source = '01'
Target = '02'
ExtraArgs = '/e', '/purge'
}
Write-Host @RoboArgs
Start-ThreadJob -ArgumentList $RoboArgs -ScriptBlock {
$robo = $args[0]
Write-Host @robo
} | Receive-Job -Wait -AutoRemoveJob
示例3(明确定义参数而不是使用$args(
$RoboArgs = @{
Source = '01'
Target = '02'
ExtraArgs = '/e', '/purge'
}
Write-Host @RoboArgs
Start-ThreadJob -ArgumentList $RoboArgs -ScriptBlock {
param ( $myRoboArgs )
Write-Host @myRoboArgs
} | Receive-Job -Wait -AutoRemoveJob