powershell - 如何处理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
解决方案
您误解了嵌套循环的工作方式。对于外循环中的每个数据对象,在内循环中处理数据对象。
例子:
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")
}
}
}
推荐阅读
- python - 给定一个单词列表,为每个单词找到最短的唯一前缀
- flutter - 在底部导航栏颤动中删除底部阴影效果
- android - 设备进入深度睡眠后套接字关闭 - Android Xamarin
- python - 在 google colab 中使用 MS nlp_recipes 时没有名为“utils_nlp”的模块
- neo4j - Neo4j:为多种情况更新节点
- java - 使用spring starter更改嵌入式tomcat的版本
- amadeus - 搜索响应中的票价规则信息
- android - 最新 Okhttp 中的主机拦截器 HttpUrl.parse IllegalArguementException
- java - 从虚拟主题接收相同消息的多个队列仅为一个队列创建一个死信条目
- python - 根据用户模型Django中的标签查询帖子