从 foreach 调用脚本
call scripts from foreach
我需要一些帮助,或者建议我尝试做的事情有更好的方法。
我正在尝试复制一些东西,所以我有
$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 块?
提前致谢。
我不确定您这样做的目的是什么。这会容易得多。
$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 等变量没有任何作用。您所做的只是遍历字符串数组。这会起作用,并且更接近您尝试执行此操作的方式。利用 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 }
}
}
我需要一些帮助,或者建议我尝试做的事情有更好的方法。
我正在尝试复制一些东西,所以我有
$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 块?
提前致谢。
我不确定您这样做的目的是什么。这会容易得多。
$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 等变量没有任何作用。您所做的只是遍历字符串数组。这会起作用,并且更接近您尝试执行此操作的方式。利用 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 }
}
}