I work in a small air-gapped network of windows machines and have been building a powershell script that queries my wsus for machine information, which I use for certain admin tasks later in the script. I’ve been building a gui for it using winforms in powershell, and am stuck on one logic puzzle to get this to dynamically change as my environment changes. The script starts by creating groupboxes based on the groups in my wsus, and then fills in the machines as they match their perspective groups. The issue I’m having is that I want to add a select all button to each groupbox that selects all items in the groupbox for that select-all checkbox. I can do this if I manually index through the list, but this breaks the ability for my script to be dynamic and grow or shrink based on groups that come and go from my wsus. I think the issue lies in the click event always referencing the last index it loops through.
Something like this….
$groupbox = @(1,2,3,4,5)
$selectall = @(6,7,8,9,10)
$index = 0
do {
$selectall[$index] = New-object System.windows.forms.checkbox
$selectall[$index].text = "select all"
$selectall[$index].tag = $groupbox[$index].text
$selectall[$index].add_click({
$checked = $selectall[$index].checked
foreach ($control in $groupbox[$index].controls) {
if ($control -is [System.windows.forms.checkbox] -and $control -ne $selectall[$index]) {
$control.checked = $checked
}
}
})
}
$index ++
while ($index -lt ($groupbox.count-1))
If you try this without the do/while and manually create the selectall boxes with specific index #s – it works perfect.
When I try it with the do/while loop, it only assigns the click event to the final value of $index – I think. I’ve been able to dynamically create the other objects in the form (groupboxes, checkboxes) with no issues, but the .add_click() method has me stumped.
I’m wondering if I need to use a hashtable instead of an array to do this, but some guidance/advice would help. I’ve never used hashtables before.
lrkeil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.