首页 > 解决方案 > 如何处理foreach循环Powershell两次?

问题描述

我在 foreach 循环中有 foreach 循环。我做了一些字符串处理并用特定的字符串映射一个文件。我使用消息框让用户知道哪个文件匹配或不匹配。我试过这个,它有效,但消息框显示 6 次,但它应该是 3 次。任何人都可以帮助我。谢谢你。

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Process = "D:\Pro"
$AllFiles = Get-ChildItem -Path $Process\*.pro -File | ForEach-Object {$_.BaseName.Substring(45)}
$Selected = @("XY11WWOLAT601A1ABA", "XY11WWOLAT601APABA", "XY11WWOLAT601ZHABA") | ForEach-Object {$_.Substring(2,13)}

foreach ($job in $AllFiles)
{
    
     $JobSplit = $job -split "_"
     $Name =  [string]::Concat($JobSplit)

     foreach ($SW in $Selected)
     {
          if ($SW -like "*$Name")
          {
               [System.Windows.Forms.MessageBox]::Show("This SW: $SW`nNOT available.","[Error]" , "OK", "Error")
          
          }
          else {

               [System.Windows.Forms.MessageBox]::Show("This SW: $SW`nAvailable.","[Info]" , "OK", "Info")     
          }
     }         
}

这是AllFiles示例: 123456789A_A2E9D28DA533_20200702045123_XX_ABC11WWOLAT601_A1.pro 123456789A_A2E9D28DA533_20200702011155_XX_ABC11WWOLAT601_ZH.pro

标签: powershellloopsforeach

解决方案


您误解了嵌套循环的工作方式。对于外循环中的每个数据对象,在内循环中处理数据对象。

例子:

1..3 | 
ForEach {
"`n *** Processing outloop $PSItem *** `n"
    'a','b','c' | 
    ForEach {
        "Processing innerloop $PSItem"
    }
}
# Results
<#
*** Processing outloop 1 *** 

Processing innerloop a
Processing innerloop b
Processing innerloop c

*** Processing outloop 2 *** 

Processing innerloop a
Processing innerloop b
Processing innerloop c

*** Processing outloop 3 *** 

Processing innerloop a
Processing innerloop b
Processing innerloop c
#>

所以......至于你的代码......

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Process  = "D:\Pro"

# ForEach #1
$AllFiles = Get-ChildItem -Path $Process\*.pro -File | 
ForEach-Object {$_.BaseName.Substring(45)}

# ForEach #2
$Selected = @("XY11WWOLAT601A1ABA", "XY11WWOLAT601APABA", "XY11WWOLAT601ZHABA") | 
ForEach-Object {$_.Substring(2,13)}


<#
ForEach #3 that use the results of ForEach #1
This is the outer ForLoop
This will process for each data object
So, whatever the count of $Allfiles is each must be process X times
#>
foreach ($job in $AllFiles)
{
     "Processing job $job in the outer loop"
     $JobSplit = $job -split "_"
     $Name =  [string]::Concat($JobSplit)

    <#
    ForEach #4 that use the results of ForEach #2
    This is the inner ForLoop
    This will process 3 times for each data object in the outerloop
    Each much be processed for each data object in $AllFiles.
    #>
     foreach ($SW in $Selected)
     {
        "Processing SW $SW in the inner loop"
          if ($SW -like "*$Name")
          {
               [System.Windows.Forms.MessageBox]::Show("This SW: $SW`nNOT available.","[Error]" , "OK", "Error")
          
          }
          else {

               [System.Windows.Forms.MessageBox]::Show("This SW: $SW`nAvailable.","[Info]" , "OK", "Info")     
          }
     }         
}

推荐阅读