在Start-Jobs命令中将多个参数传递给scriptblock

时间:2016-06-09 04:30:14

标签: powershell powershell-v2.0 powershell-v3.0 start-job

我正在尝试将多个参数传递给我的scriptblock:

$ConfigFile = @("C:\zip1.zip","C:\zip2.zip")
$unzippath = "C:\"
$ScriptBlock = {
  param($ConfigFile,$unzippath) 
  $shell = new-object -com shell.application
  $zip = $shell.NameSpace($ConfigFile)
  foreach($item in $zip.items())
  {
     $shell.Namespace($unzippath).copyhere($item)
  }
}

Start-Job -Scriptblock $ScriptBlock -ArgumentList $ConfigFile,$unzippath

但它失败了。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您做得对,$ConfigFile作为array传递。所以你的问题是而不是将参数传递给scriptblock。它更像是如何使用Shell.application提取多个zip文件。

您可能必须使用Foreach-Object cmdlet迭代每个zip并提取它:

$ConfigFile = @("C:\zip1.zip","C:\zip2.zip")
 $unzippath = "C:\"
 $ScriptBlock = {
    param($ConfigFile,$unzippath) 
    $ConfigFile | ForEach-Object {
        $shell = new-object -com shell.application
        $zip = $shell.NameSpace($_)
        foreach($item in $zip.items())
        {
           $shell.Namespace($unzippath).copyhere($item)
        }
    }
  }

  Start-Job -Scriptblock $ScriptBlock -ArgumentList $ConfigFile,$unzippath