This takes some 'splaining so stick with me. I have an array of arrays that is used to build out a user menu that looks like this:
Each line is an array (the outside array) and then there are three inside elements (the inside arrays) that consist of the number, the blank (actually a space character) within the bracket, and the item name (ie Item 1).
Via some DO/While and Switch wizardry this menu appears to be interactive; the user can toggle one or more items that they want to modify by entering in the item number. This is done by changing the inside array that is a space character into an "X"...which is updated immediately on the screen. So if the user wants to modify items 1 and 3 the menu will look like this:
Once the user is satisfied with their selection(s) they commit their selection (exits the Do/While Loop) and the script continues to the next part (which is where I need help).
Now what I want to do is write the item name of every item that has been selected by the user into a new array (just a single array); but I can't seem to make the connection of if the outside array contains an "X" to write the 3rd inside array (within that array) to a variable.
Below is my code. Remember $Menu is a single level array, and the "X" option for the switch is to commit the selection for the next part of the script.
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)
I was waaaaaaay over-complicating things. After a break I realized the solution was a lot simpler. The below code solved my issues...I just needed to incorporate an If statement and not use where-object.
$Count=0
ForEach($_ in $Menu){
If($Menu[$Count][1] -eq "X") {
$Selection+=@($Menu[$Count][2])
}
$Count+=1
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.