Click button and search AD

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.
User avatar
mqh777
Posts: 236
Joined: Tue Feb 25, 2014 1:01 pm

Click button and search AD

Post by mqh777 » Thu Oct 18, 2018 10:16 am

Product: PowerShell Studio 2018 (64 Bit)
Build: v5.5.155
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.16299.0


We have a PowerShell Studio form that has many tabs and over 50 buttons. We want to keep users out of active directory so I want to add a button that when pressed will search for AD groups but only AD groups that begin with SCCM. Then, based on the group they choose it will add their machine to that group. I know how to do all of the PowerShell scripting to add the system but how do you make your button read AD and then capture their choice into a $variable?

User avatar
davidc
Posts: 5801
Joined: Thu Aug 18, 2011 4:56 am

Re: Click button and search AD

Post by davidc » Fri Oct 19, 2018 8:01 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Click button and search AD

Post by jvierra » Fri Oct 19, 2018 9:38 am

The simple answer is to use Get-AdGroup.

$groups = Get-AdGroup -Filter "Name -like 'SCCM*'"

This will fill the variable with all groups that begin with "SCCM".

User avatar
mqh777
Posts: 236
Joined: Tue Feb 25, 2014 1:01 pm

Re: Click button and search AD

Post by mqh777 » Fri Oct 19, 2018 10:19 am

Yes, that part is easy. I can get the groups and display them in the Richtext window. But then you have to copy/paste the group name into another textbox and then click yet another button. I want to click 1 button that does everything. Or is that not possible?

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Click button and search AD

Post by jvierra » Fri Oct 19, 2018 10:33 am

Why do you have to copy and paste? What is it you are trying to do?

I recommend reading the articles on designing forms for administration.

https://info.sapien.com/index.php/guis/gui-design-best-practice/user-interface-design-for-administrators

Alos review the lists of articles available on the left menu of the web page. You will find more info on how to build forms and make them do what you need with very little coding.

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Click button and search AD

Post by jvierra » Fri Oct 19, 2018 10:43 am

mqh777 wrote:
Fri Oct 19, 2018 10:19 am
Yes, that part is easy. I can get the groups and display them in the Richtext window. But then you have to copy/paste the group name into another textbox and then click yet another button. I want to click 1 button that does everything. Or is that not possible?
It sounds to me like you want to display the groups in a ListBox and use the "SelectedIndexChanged" event to execute the code.

Read the Spotlight article on ListBoxes.

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Click button and search AD

Post by jvierra » Fri Oct 19, 2018 11:25 am

Here are some videos that will help you get started with forms:

https://www.youtube.com/user/SAPIENTech/search?query=forms

User avatar
mqh777
Posts: 236
Joined: Tue Feb 25, 2014 1:01 pm

Re: Click button and search AD

Post by mqh777 » Fri Oct 19, 2018 1:47 pm

ok, we're making progress. I have a listbox and 1 button. the button will query AD and get a list of groups. It then displays the groups in the listbox. The only problem is they are not listed 'one per line'. They are only 1/2 wide and all jumbled. How do you format the listbox so it looks like this?

AD group num1
AD group num2
AD group num3

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Click button and search AD

Post by jvierra » Fri Oct 19, 2018 2:12 pm


$listbox1.DataSource = [collections.arraylist]Get-AdGroup -Filter "Name -like 'SCCM*'"
$listbox1.DisplayMember = 'Name'

User avatar
mqh777
Posts: 236
Joined: Tue Feb 25, 2014 1:01 pm

Re: Click button and search AD

Post by mqh777 » Tue Oct 23, 2018 7:13 am

The first line in your code produces the red exclamation point and does not execute. Here is my code:

Code: Select all

$buttonSCCMAppGroups_Click = {
	$SCCMAppGroups = (Get-ADGroup -Filter { name -like "SCCMApp_*" }).name | Sort-Object
	$ListSCCMGroups.Items.Add("$SCCMAppGroups")
	}
This returns the data but it is a 1/2 wide and all jumbled together. I've tried with/without sort-object and with/without -out-string and I get the same result.

Locked