在PowerShell ForEach循环中可以有多个IN条件吗?

在PowerShell ForEach循环中可以有多个IN条件吗?,powershell,loops,foreach,hashtable,Powershell,Loops,Foreach,Hashtable,我在函数中有以下示例代码: [array]$ARR = $null foreach ($file in $fileTable.identical) { [hashtable]$HT=@{ 'FileName' = $file.Name 'AppName' = $file.App 'GroupName' = $file.Group 'Valid' = $true } $ARR += $HT } foreach ($file

我在函数中有以下示例代码:

[array]$ARR = $null

foreach ($file in $fileTable.identical)
{
   [hashtable]$HT=@{
       'FileName' = $file.Name
       'AppName' = $file.App
       'GroupName' = $file.Group
       'Valid' = $true
   }
   $ARR += $HT
}
foreach ($file in $fileTable.removed)
{
   [hashtable]$HT=@{
       'FileName' = $file.Name
       'AppName' = $file.App
       'GroupName' = $file.Group
       'Valid' = $false
   }
   $ARR += $HT
}
foreach ($file in $fileTable.modified)
{
   [hashtable]$HT=@{
       'FileName' = $file.Name
       'AppName' = $file.App
       'GroupName' = $file.Group
       'Valid' = $false
   }
   $ARR += $HT
}

return $ARR
+对于其他$fileTable。[properties]还有3个foreach循环,其中'Valid'=$false

我不必重复代码块多次,而是想做如下操作:

foreach (($file in $fileTable.removed) -and ($file in $fileTable.modified))
{
   [hashtable]$HT=@{
       'FileName' = $file.Name
       'AppName' = $file.App
       'GroupName' = $file.Group
       'Valid' = $false
   }
}
所以哈希表中唯一不同的变量是$value。 $fileTable是一个pscustomobject,具有一些自定义属性,如相同、修改、添加和删除

我知道我想要的东西在foreach循环中是不可能的,但我正在寻找类似的解决方案来减少代码行数。任何帮助都将不胜感激:)

谢谢

结合您的和s的方法

编辑:合并的提示

$ARR = foreach($Variant in 'identical', 'removed', 'modified'){
  $fileTable.$Variant | ForEach-Object{
    [PSCustomObject]@{
      'FileName'  = $_.Name
      'AppName'   = $_.App
      'GroupName' = $_.Group
    # 'Valid'     = if($Variant -eq 'identical'){$True} else {$False}
      'Valid'     = $Variant -eq 'identical'
    }
  }
}
结合您的和s的方法

编辑:合并的提示

$ARR = foreach($Variant in 'identical', 'removed', 'modified'){
  $fileTable.$Variant | ForEach-Object{
    [PSCustomObject]@{
      'FileName'  = $_.Name
      'AppName'   = $_.App
      'GroupName' = $_.Group
    # 'Valid'     = if($Variant -eq 'identical'){$True} else {$False}
      'Valid'     = $Variant -eq 'identical'
    }
  }
}

$ARR=@('idential'、'removed'、'modified'{$fileTable.${$fileTable.${'FileName'=${.Name;'AppName'=${.App;'GroupName'=${.Group;'Valid'=$false}})
$ARR=@('idential'、'removed'、'modified'{$fileTable.${$fileTable.${$fileTable.${'FileName'=${.AppName;'AppName'=$.App GroupName;'Valid'=$false}}
做得很好,但是
'Valid'=$Variant-eq'idential'
应该可以。做得很好,但是
'Valid'=$Variant-eq'idential'
应该可以。