0

我需要一些帮助,或者就我正在尝试做的事情提出更好的建议。

我试图复制一些东西,所以我有

$tests = @("test1", "test3", "test5")
$copy_1 = {
$source = "C:\Source\test1"
$Destination = "C:\Destination\test1"

Copy-Item $Source -Recurse -Destination $Destination -Container -Force
}

$copy_2 = {
$source = "C:\Source\test2"
$Destination = "C:\Destination\test2"

 Copy-Item $Source -Recurse -Destination $Destination -Container -Force
}

$copy_3 = {
$source = "C:\Source\test3"
$Destination = "C:\Destination\test3"

Copy-Item $Source -Recurse -Destination $Destination -Container -Force
}

$copy_4 = {
$source = "C:\Source\test4"
$Destination = "C:\Destination\test4"

Copy-Item $Source -Recurse -Destination $Destination -Container -Force
}

Foreach($i in $Tests)
{
    IF($i -eq "test1)
        {
          Start-Job -Name $i -Scriptblock {$($i)}
        }
}

....

这不会调用我的脚本块。

      PSJobTypeName   State         HasMoreData     Location             Command                  

      BackgroundJob   Running       True            localhost            ($($i))  

如何调用 $test1 块?

提前致谢。

4

1 回答 1

2

我不确定你这样做是为了达到什么目的。这会容易得多。

$tests = @("test1", "test3", "test5")

Foreach($i in $Tests)
{
    IF($i -eq "test1")
        {
          Start-Job -Name $i -Scriptblock { Copy-Item "C:\Source\$($i)" "C:\Destination\$($i)" -Recurse -Container -Force }
        }
}

....

编辑:

就像我在下面的评论中所说,您发布的代码对您的 copy_1、copy_2 等变量没有任何作用。您所做的只是遍历字符串数组。这会起作用,并且更接近您尝试的方式。使用PSObject

$copy_1 = New-Object -TypeName PSObject
$copy_1 | Add-Member -MemberType NoteProperty -name Name -value "copy_1"
$copy_1 | Add-Member -MemberType NoteProperty -name Source -value "C:\Source\test1"
$copy_1 | Add-Member -MemberType NoteProperty -name Destination -value "C:\Destination\test1"

$copy_2 = New-Object -TypeName PSObject
$copy_2 | Add-Member -MemberType NoteProperty -name Name -value "copy_2"
$copy_2 | Add-Member -MemberType NoteProperty -name Source -value "C:\Source\test2"
$copy_2 | Add-Member -MemberType NoteProperty -name Destination -value "C:\Destination\test2"

$copy_3 = New-Object -TypeName PSObject
$copy_3 | Add-Member -MemberType NoteProperty -name Name -value "copy_3"
$copy_3 | Add-Member -MemberType NoteProperty -name Source -value "C:\Source\test3"
$copy_3 | Add-Member -MemberType NoteProperty -name Destination -value "C:\Destination\test3"

$tests = @($copy_1, $copy_2, $copy_3)

Foreach($i in $tests)
{
    if($i.Name -eq "copy_1")
        {
          Start-Job -Name $i.Name -Scriptblock { Copy-Item $i.Source $i.Destination -recurse -Container -Force }          
        }
}
于 2015-10-19T21:29:47.643 回答