Reputation: 331
I need some help or maybe advise a better way with what I'm trying to do.
I'm trying to copy few things so I have
$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)}
}
}
....
This doesn't call my scriptblock.
PSJobTypeName State HasMoreData Location Command
BackgroundJob Running True localhost ($($i))
How can I call $test1 block?
Thanks in advance.
Upvotes: 0
Views: 89
Reputation: 500
I'm not sure what you are trying to accomplish by doing it like that. This would be much easier.
$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 }
}
}
....
Edit:
Like I said in the comment below, the code you posted is doing nothing with your copy_1, copy_2, ect variables. All you are doing is iterating through an array of strings. This would work and is closer to the way you're trying to do it. Utilize PSObjects
$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 }
}
}
Upvotes: 2