将 Powershell 数组变量作为工作流任务的参数传递给工作流?

问题描述 投票:0回答:1

我创建此代码是为了使用基于安装的 USB 驱动器数量的工作流程来运行 Powershell 脚本。如果安装了多个驱动器,我希望 Powershell 在每个驱动器上并行运行脚本。如果我在工作流程中对 foo.ps1 所需的驱动器名称参数进行硬编码,则该代码可以工作,但如果可能的话,我更愿意将数组结果传递给工作流程。此代码调用工作流程,但立即返回“无法索引到空数组”,我假设这意味着工作流程不了解该数组。有没有办法可以将数组元素作为参数传递来调用各个工作流程元素?

workflow Verify1 {
powershell -file Foo.ps1 $MountedDrives[0] 
}
workflow Verify2 {
 parallel
{
powershell -file Foo.ps1 $MountedDrives[0] 
powershell -file Foo.ps1 $MountedDrives[1]
}
}
$MountedDrive=@(Get-WMIObject cim_logicaldisk | ? drivetype -eq 2 | %{$_.deviceid})
$Reps = $MountedDrives.Count
switch ($Reps)
{
     1 {Verify1}
     2 {Verify2}
}
arrays powershell workflow
1个回答
0
投票

答案由https://stackoverflow.com/users/712649/mathias-r-jessen提供,我需要一个参数声明。我忘记了,调用工作流时还需要发送参数,否则工作流无法获取参数。

workflow Verify1 {
param($MountedDrives)
powershell -file C:\users\lando\desktop\VerifyBoschNGI_v10.ps1 $MountedDrives[0]
}
workflow Verify2 {
param($MountedDrives)
 parallel
{
powershell -file C:\users\lando\desktop\VerifyBoschNGI_v10.ps1 $MountedDrives[0] 
powershell -file C:\users\lando\desktop\VerifyBoschNGI_v10.ps1 $MountedDrives[1]
}
}
$MountedDrives=@(Get-WMIObject cim_logicaldisk | ? drivetype -eq 2 | %{$_.deviceid})
$Reps = $MountedDrives.Count
$Drive=$MountedDrives
write-host $Drive $MountedDrives
switch ($Reps)
{
     1 {Verify1 $MountedDrives}
     2 {Verify2 $MountedDrives}
}
© www.soinside.com 2019 - 2024. All rights reserved.