[英]PowerShell Multidimensional Array-fill a variable with the value from one array based off the value of another array
這需要一些'解釋所以堅持我。 我有一個 arrays 數組,用於構建如下所示的用戶菜單:
每行是一個數組(外部數組),然后是三個內部元素(內部數組),由數字、括號內的空格(實際上是空格字符)和項目名稱(即項目 1)組成。
通過一些 DO/While 和 Switch 向導,這個菜單看起來是交互式的; 用戶可以通過輸入項目編號來切換他們想要修改的一個或多個項目。 這是通過將作為空格字符的內部數組更改為“X”來完成的......它會立即在屏幕上更新。 因此,如果用戶想要修改項目 1 和 3,菜單將如下所示:
一旦用戶對他們的選擇感到滿意,他們就會提交他們的選擇(退出 Do/While 循環)並且腳本繼續到下一部分(這是我需要幫助的地方)。
現在我要做的是將用戶選擇的每個項目的項目名稱寫入一個新數組(只是一個數組); 但我似乎無法連接外部數組是否包含“X”以將第三個內部數組(該數組內)寫入變量。
下面是我的代碼。 記住 $Menu 是一個單級數組,開關的“X”選項是提交腳本下一部分的選擇。
Do {
Clear-Host
# Menu title
Write-Host "Configuration Menu`n"
# Menu items (dynamic)
$Count=0
ForEach($_ in $Menu){
If($Count -le 8) {Write-Host " $($Menu[$Count][0]). ($($Menu[$Count][1])) $($Menu[$Count][2])"}
Else {Write-Host " $($Menu[$Count][0]). ($($Menu[$Count][1])) $($Menu[$Count][2])"}
$Count+=1
}
Write-Host "`n S - Select All"
Write-Host " D - Deselect All"
Write-Host " # - Toggles the correlating menu item"
Write-Host " X - Commit"
Write-Host "[Q] - Quit"
Write-Host "`n Select the configuration settings you want to change"
$UserIn1=Read-Host " "
Switch($UserIn1) {
#I removed other options that aren't part of this question
X {
# The below lines write just the arrays with the "X" to a new array called $Selection
# This way $Selection only has the user-selected array sets
# But this just writes each internal array as it's own array and not as a set
# Also, the beginning array is populated from an XML document, so the text is not
# displayed, only the text "SystemObject[]"
$Count=0
ForEach($_ in $Menu){
$Selection+=@($Menu[$Count][2] | Where-Object {$_ -match "X"} | Out-String)
}
#At one point in time I think I got $Selection to get the actual arrays, but
#Its not currently working right. The idea below is that once I got an array
#of just the user selected array sets, that I could then write the Item name
#$Config[..][2] into $ConfigureItem for each array set the user selected.
#So $ConfigureItem would be an array of just the item names...which I need
# to get in order for the next step to play well (and dynamicly) with my
# xml file which the user is actually modifying.
$Count=0
ForEach($Item in $Selection) {
$ConfigureItem+=@($Selection[$Count][2])
$Count+=1
}
Clear-Host
Write-Host "$ConfigureItem"
Write-Host "Did the test pass?"
pause
Configure
$Commit=$True
} # End of switch
}
While($Commit -ne $True)
我太復雜了。 休息后,我意識到解決方案要簡單得多。 下面的代碼解決了我的問題......我只需要合並一個 If 語句而不使用 where-object。
$Count=0
ForEach($_ in $Menu){
If($Menu[$Count][1] -eq "X") {
$Selection+=@($Menu[$Count][2])
}
$Count+=1
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.