Selecting multiply items in listbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 6 years and 11 months 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
User avatar
v35678
Posts: 23
Last visit: Fri Jan 05, 2024 8:25 am

Selecting multiply items in listbox

Post by v35678 »

Hi, I'm having trouble get multiple items to run when I select them from a listbox. Selecting one item works fine. I'm adding a AD Group to a computer. That is what is selected in the listbox. My goal is to be able to select as many groups as possible and add them to the computer. Any help would be greatly appreciated.
$exprString = '$list2.SelectedItems | foreach-object {$_.tag} | foreach-object {$_.name}'
$endapp = invoke-expression $exprString
$statusbar1.text = "AD GROUPS ON " + $computername.ToUpper() + " (ADDING $($Endapp))"
import-module activedirectory
$addapp = Add-ADGroup
Function Add-ADGroup
Function Add-ADGroup
{
	$computername = $textboxCN.text
	$dns = get-adcomputer $computername
	$b = $dns.distinguishedname
	Add-ADPrincipalGroupMembership $b (Get-ADGroup -Filter * | where { $_.name -eq $endapp })
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Selecting multiply items in listbox

Post by jvierra »

Sorry but your code does not make any sense. why are you using Invoke expression to run code?

The simple method is to get all groups and add them as objects to a lit box then just return the selected items and add the,

Add-AGroupMember <groupname> -Members $listbox1.SelectedItems

No other code is required.

It is not possible to add groups to a computer object.

To add a computer to a group we would do this:

Add-ADPrincipalGroupMembership $computername +'*'-MemberOf $listbox.SelectedItems

The add will fail if a the computer is already member of any group.

To specifiy a computer we must use SamAccount name or object.

Add-ADPrincipalGroupMembership (Get-AdComputer <computername>) -MemberOf $listbox.SelectedItems
User avatar
v35678
Posts: 23
Last visit: Fri Jan 05, 2024 8:25 am

Re: Selecting multiply items in listbox

Post by v35678 »

Thanks I'll give it another go. Thanks for the advice.
User avatar
v35678
Posts: 23
Last visit: Fri Jan 05, 2024 8:25 am

Re: Selecting multiply items in listbox

Post by v35678 »

So
Add-ADPrincipalGroupMembership (Get-AdComputer <computername>) -MemberOf $listbox.SelectedItems
works well but I have a slight problem. Some of the groups name are different than their SamAccountNames. For example: Group name is Sapp - test. SamAccountName is app - test. How can I make that work?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Selecting multiply items in listbox

Post by jvierra »

That is why we store the complete group object in the Listbox.

$lsitbox1.DataSource = [system.colections.arraylist](Get-AdGroup -filter ....)
$listbox1.DisplayMember = 'Name'
This topic is 6 years and 11 months 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