How to run code in the powershell script for selected checkboxes

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Idler_
Posts: 3
Last visit: Thu Mar 05, 2020 6:25 am

How to run code in the powershell script for selected checkboxes

Post by Idler_ »

I have a ps script that extracts applied GPO’s from a specific OU into a CSV file and loads the name of the GPO value into a form with check list next to each GPO name and then i can select any GPO available and either select to enable or disable this GPO for a user by entering the username in the next form.

My problem is when i select multiple GPO’s to apply for a user the code runs against only one GPO(Last selected) and not for all selected, how is it possible to invoke the command on all selected GPO from the form, and is there a way after i finish entering the username the first form would re-open to go through the process again to select other GPO’s for other users?

--------------
EDIT: It has been solved thank you.
Last edited by Idler_ on Fri Feb 28, 2020 12:26 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to run code in the powershell script for selected checkboxes

Post by jvierra »

Your issue is that you are using only the "SelectedItem". You need to enumerate the "CheckedItems' collection to get all of the items that have been checked.

When trying to use forms it is critical that you reference the documentation to understand how to use a control. Much of the code you see on the Internet that is written with PowerShell is either wrong or misleading.

https://docs.microsoft.com/en-us/dotnet ... eckedItems

All controls are well documented on the Microsoft "docs" site and can be found by a simple search.
Idler_
Posts: 3
Last visit: Thu Mar 05, 2020 6:25 am

Re: How to run code in the powershell script for selected checkboxes

Post by Idler_ »

Hi jvierra,
I am not sure i quite understand what is going on in the link it seems like it is a C# language? also i ran some tests but i failed big time.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to run code in the powershell script for selected checkboxes

Post by jvierra »

The documentation for all things Forms is mostly in C#. It can help you to understand how these are intended to be used. Also the articles on the Sapien info site are helpful.
https://info.sapien.com/index.php/guis/gui-controls

Unfortunately the CheckedListBox has not been documented for PowerShell. You can use the ListBox docs to get background and then realize that it has a "CheckedItems" collection.

How to use forms in PowerShell requires that you have sufficient training in PowerShell to understand how to use the documentation.
Idler_
Posts: 3
Last visit: Thu Mar 05, 2020 6:25 am

Re: How to run code in the powershell script for selected checkboxes

Post by Idler_ »

im sorry i cant find what i need, i give up.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to run code in the powershell script for selected checkboxes

Post by jvierra »

The CheckedListBox has a property called "CheckedItems". This contains all checked items as an array. You have to enumerat3e this array and handle each check item separately.

Code: Select all

foreach($item in $checlListBox.CheckedItems){
     #process the item
     Write-Host $item
}
I also wonder why you are using a VB input box when you can easily add a textbox to the form. Also the processing should be don in the form and not after it closes.

I suggest starting over and try to state simply and clearly what you want to accomplish and then create the code that does that.

I also wonder why you are using a CheckedListBox which is designed to select multiple items for code that only wants one item. A ComboBox is the correct control for that use case.
This topic is 4 years and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked