List AD Users in List Box in Powershell - powershell

I'm currently making my own administration tool and one function should end up being "Disable Account" (Active Directory User).
The Code I currently have is the following:
#Assemblies
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Frame
$frmDisableUser = New-Object system.Windows.Forms.Form
$frmDisableUser.ClientSize = New-Object System.Drawing.Point(378,99)
$frmDisableUser.text = "Disable User"
$frmDisableUser.TopMost = $false
$frmDisableUser.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$frmDisableUser.TopMost = $false
$frmDisableUser.FormBorderStyle = "FixedSingle"
$frmDisableUser.startposition = "CenterScreen"
$frmDisableUser.MaximizeBox = $false
#AD Users Listbox
$lstADUsers = New-Object system.Windows.Forms.ListBox
$lstADUsers.width = 356
$lstADUsers.height = 20
$lstADUsers.location = New-Object System.Drawing.Point(9,18)
$lstADUsers.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#Disable Account Button
$btnDisableAccount = New-Object system.Windows.Forms.Button
$btnDisableAccount.text = "Disable"
$btnDisableAccount.width = 100
$btnDisableAccount.height = 30
$btnDisableAccount.location = New-Object System.Drawing.Point(265,53)
$btnDisableAccount.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btnDisableAccount.Add_Click({
})
#Adds all elements into th eframe
$frmDisableUser.controls.AddRange(#($lstADUsers,$btnDisableAccount))
#Shows the frame
$frmDisableUser.ShowDialog()
The Command I use to get all AD Users as an output is the following:
Get-ADUser -Filter {(Enabled -eq "true")} | Select-Object Name
I think the easiest way is to make it using arrays but I'm not really familiar with arrays to be honest... I'd be very happy if you could help me!
Thanks for your time!

You can add items to the listbox with
$lstADUsers.Items.Add()
I've updated your code to demonstrate this.
#Assemblies
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Frame
$frmDisableUser = New-Object system.Windows.Forms.Form
$frmDisableUser.ClientSize = New-Object System.Drawing.Point(388,299)
$frmDisableUser.text = "Disable User"
$frmDisableUser.TopMost = $false
$frmDisableUser.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$frmDisableUser.TopMost = $false
$frmDisableUser.FormBorderStyle = "FixedSingle"
$frmDisableUser.startposition = "CenterScreen"
$frmDisableUser.MaximizeBox = $false
#AD Users Listbox
$lstADUsers = New-Object system.Windows.Forms.ListBox
$lstADUsers.width = 356
$lstADUsers.height = 220
$lstADUsers.location = New-Object System.Drawing.Point(9,18)
$lstADUsers.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$lstADUsers.AutoSize = $false
Get-ADUser -Filter {(Enabled -eq "true")} | foreach{[void]$lstADUsers.Items.Add($_.name)}
#Disable Account Button
$btnDisableAccount = New-Object system.Windows.Forms.Button
$btnDisableAccount.text = "Disable"
$btnDisableAccount.width = 100
$btnDisableAccount.height = 30
$btnDisableAccount.location = New-Object System.Drawing.Point(265,249)
$btnDisableAccount.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btnDisableAccount.Add_Click({
})
#Adds all elements into th eframe
$frmDisableUser.controls.AddRange(#($lstADUsers,$btnDisableAccount))
#Shows the frame
$frmDisableUser.ShowDialog()
Note the cast to [void] - this is to suppress the output from the .Add() method. It emits the the index number for that item in the array.
I also suggest you check out https://poshgui.com/ if you haven't already. It can help you not only design forms but also learn how to interact with the GUI components.

The Items property of a ListBox (which is an ObjectCollection object) has a method called AddRange with which you can enter an array.
Just get a string array of usernames and enter it in one go.
To make the list better readable, sort it alphabetically. You could also set the 'Sorted' property of the Listbox to $true, but sorting before adding to the listbox is more efficient.
$users = (Get-ADUser -Filter "Enabled -eq 'True'").Name | Sort-Object
Next add the array to the listbox
$lstADUsers.Items.AddRange($users)
P.S. If you need to refresh the listbox data with the result of a new call to Get-ADUser, clear the listbox items first with $lstADUsers.Items.Clear()

Related

Copy listview1 to listview2

I'm trying to copy the checked item in ListADGroup and copy to ListADGroup2. ListADGroup updates with all the groups but when I try adding them to ListADGroup2 it copies the right amount checkboxes but they are empty. Im not sure what is missing from the button command to copy the data properly.
Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.Drawing"
Add-Type -AssemblyName PresentationFramework
Clear-Host
#== Create a New Form ==#
$TSTAPP=New-Object System.Windows.Forms.Form
$TSTAPP.topmost=$true
$TSTAPP.Text="Test App"
$TSTAPP.Location.x=750
$TSTAPP.Location.Y=330
$TSTAPP.Size=New-Object System.Drawing.Size(750,330)
#== Now Lock the form so it cannot be re-sized ==#
$TSTAPP.MaximumSize=New-Object System.Drawing.Size(750,330)
$TSTAPP.MinimumSize=New-Object System.Drawing.Size(750,330)
#== Group List Label ==#
$GroupL = New-Object System.Windows.Forms.Label
$GroupL.Text = "Select Group:"
$GroupL.Location = New-Object System.Drawing.Point(4,5)
$GroupL.AutoSize = $true
$TSTAPP.Controls.Add($GroupL)
#== AD Users Listbox ==#
$ListADGroup = New-Object system.Windows.Forms.Listview
$ListADGroup.Width = 356
$ListADGroup.height = 220
$ListADGroup.location = New-Object System.Drawing.Point(4,22)
#$ListADGroup.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ListADGroup.CheckBoxes = $true
$ListADGroup.Name = "Main"
$ListADGroup.AutoArrange = $true
$ListADGroup.GridLines = $true
$ListADGroup.MultiSelect = $false
$ListADGroup.View = "Details"
$ListADGroup.AutoSize = $true
#$ListADGroup.Columns[0].
$ListADGroup.Columns.Add("Groups")
$AllADGroups = Get-ADGroup -filter * -SearchBase 'OU=Accounts,DC=domain,DC=internal' -SearchScope subtree -Properties Name | Sort-Object Name | foreach{[void]$ListADGroup.Items.Add($_.name)}
$TSTAPP.controls.add($ListADGroup)
#== AD Users Listbox ==#
$ListADGroup2 = New-Object system.Windows.Forms.Listview
$ListADGroup2.Width = 356
$ListADGroup2.height = 220
$ListADGroup2.location = New-Object System.Drawing.Point(370,22)
#$ListADGroup2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ListADGroup2.CheckBoxes = $true
$ListADGroup2.Name = "Main"
$ListADGroup2.AutoArrange = $true
$ListADGroup2.GridLines = $true
$ListADGroup2.MultiSelect = $false
$ListADGroup2.View = "Details"
$ListADGroup2.AutoSize = $true
#$ListADGroup2.Columns.Width = 100
$ListADGroup2.Columns.Add("Groups2")
$TSTAPP.controls.add($ListADGroup2)
#== Add Button ==#
$AddGroup = New-Object system.Windows.Forms.Button
$AddGroup.text = "Add"
$AddGroup.width = 100
$AddGroup.height = 30
$AddGroup.location = New-Object System.Drawing.Point(265,249)
$AddGroup.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TSTAPP.controls.add($AddGroup)
$AddGroup.Add_Click({
$itm = $ListADGroup.CheckedItems
foreach ($items in $itm){
$ListADGroup2.Items.Add($items.name)
}
})
$TSTAPP.ShowDialog()
If you try to debug using ISE or Visual Code you will see that the property containing the name is called Text
So replace :
$ListADGroup2.Items.Add($items.name)
by
$ListADGroup2.Items.Add($items.Text)

How would I associate an IP address with switch name in drop down menu

I'm not entirely sure how to word this so bare with me. The orginization i work for is taking away a license for a software we use to connect to our network switches using a basic SSH connection. Just using PowerShell i know this command works for us. ssh username#IP_of_Switch now comes the fun part.
I want to make a GUI for our techs so they just have to select the switch name and not have to know every IP of all 100+ switches. I know for a drop down box it would look like this
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20)
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)
$swList=#("1D","3D","3F","6E","Laundry","Chapel")
foreach ($sws in $swList) {$DropDownBox.Items.Add($sw)} #end foreach
What i want to do is have those Names be associated with their respective IP some how. AND HAVE A BUTTON TO CONNECT THEM TO SELECTED SWITCHI have a slight idea that i may need to have a txt file with the IP's. Any help would be greatly appreciated.
ComboBox is inherited from ListControl
You should provide Objects to ListItems. Each object should contain at least 2 properties: displayName and value ( you can change names of properties how you want ).
You need to set ComboBox's properties DisplayMember to name of property should be displayed.
Example:
In $FormEvent_Load scriptblock, we add items to ComboBox. Those items are objects with (at least) 2 properties - Name and Address. There we also set DisplayMember property of ComboBox to let it know, which property should be displayed in UI.
In $CBEvent_SelectedIndexChanged scriptblock, we get SelectedItem property from ComboiBox. SelectedItem contains original object we passed to ComboBox.Items.Add. There is Address field as you see, which can be used.
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$combobox1 = New-Object System.Windows.Forms.ComboBox
$label1 = New-Object System.Windows.Forms.Label
$FormEvent_Load = {
$combobox1.DisplayMember = 'Name'
$combobox1.Items.Add(
[PSCustomObject]#{
'Name' = 'Switch1'
'Address' = [ipaddress]::Parse('1.1.1.1')
})
$combobox1.Items.Add(
[PSCustomObject]#{
'Name' = 'Switch2'
'Address' = [ipaddress]::Parse('2.12.21.22')
})
}
$CBEvent_SelectedIndexChanged = {
$label1.Text = $combobox1.SelectedItem.Address.ToString()
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 292
$System_Drawing_Size.Height = 266
$form1.ClientSize = $System_Drawing_Size
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 83
$System_Drawing_Point.Y = 199
$label1.Location = $System_Drawing_Point
$label1.Text = 'Select IP'
$form1.Controls.Add($label1)
$form1.Controls.Add($combobox1)
$form1.add_Load($FormEvent_Load)
$ComboBox1.add_SelectedIndexChanged($CBEvent_SelectedIndexChanged)
$form1.ShowDialog()| Out-Null
This is just an example but it does work. I used a simple csv file to hold the switch IP Addresses. I edited an answer for the majority of this script so I will give credit for that Powershell default dropdown value
CSV File
1D,127.0.0.1
3D,127.0.0.2
3F,127.0.0.3
6E,127.0.0.4
Laundry,127.0.0.5
Chapel,127.0.0.6
Script
########################
# Edit This item to change the DropDown Values
$switchesFilePath = "C:\temp\switches.csv"
$switches = ConvertFrom-Csv (Get-Content -Path $switchesFilePath)
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice = $DropDown.SelectedItem
Write-Host "The selected switch is $($script:Choice.Name) and it's IP is $($script:Choice.IPAddress)"
Write-Host "The ssh command would be ---- ssh username#$($script:Choice.IPAddress)"
$Form.Close()
}
function selectSwitch{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
$DropDown.SelectedItem = $DropDown.Items[0]
$DropDown.DisplayMember = 'Name'
[void] $DropDown.Items.AddRange($switches)
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select a network switch"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Connect to a switch"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$switch = selectSwitch

How to create CheckBox automatically using PowerShell?

I want to create checkbox automatically consider to the total a file inside a folder I selected.
In my script, the checkbox always create one checkbox, but my file more than one. And the checkbox always appear before I select the folder from listbox. Please give me advice if anyone know about this, Thank you so much.
This is the result look like
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(500,700)
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Test"
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,70)
$objListBox.Size = New-Object System.Drawing.Size(260,30)
$objListBox.Height = 250
$objListBox.add_SelectedIndexChanged($SelectedFile)
$objForm.Controls.Add($objListBox)
$objChktBox = New-Object System.Windows.Forms.CheckBox
$objChktBox.Location = New-Object System.Drawing.Size(10,400)
$objChktBox.Size = New-Object System.Drawing.Size(400,3000)
$objChktBox.Height = 200
$objForm.Controls.Add($objChktBox)
# Populate list.
#(Get-ChildItem -Directory ".\").Name | ForEach-Object {[void] $objListBox.Items.Add($_)}
$SelectedFile=
{
$objChktBox.Text = (Get-ChildItem $objListbox.SelectedItem)
}
$objForm.ShowDialog()

Removing dynamic text from a temp file in a script

Ok, I know this is going to end up being basic but I cant seem to figure it out. I am trying to build a tool that will allow us to get the full AD group name for a user. The current problem we have is that the names of our AD groups are long, so a net user MrSmith /domain ends up truncating the full AD group name. I tried using a gpresult -user MrSmith /r but this doesnt return all of the AD groups for some reason.
So I decided that I would create something new that will also have other applications later on. I want to do this by first: doing a net group /domain to pull all of the AD groups in the DC. Second run a net group $ADGroup /domain for each of the ADGroups in the domain and use that output to search for the specified user ID and create a new output that displays the full name of each AD group that user ID is a part of. I am putting this in a drop down GUI for some of the future features I want to add.
Now I have got a good chunk of the first step done, but obviously I am running into an issue. When I do the net group /domain i get output looking like
The request will be processed at a domain controller for domain MyComapny.com.
Group Accounts for \\Server.MyComapny.com
-------------------------------------------------------------------------------
*AD_Group_1
*AD_Group_2
*AD_Group_3
*AD_Group_4
.....
And I add this to a temp .txt file for use.
The Issue I am having is that its not performing the net group $Choice /domain on the AD group selected from the drop down and I dont know why.
Any advice is welcome, I know I am not the best at this so my format and style may not be standard.
Clear-Variable DropDownList
$RawADGroups = Net Group /domain >> 'C:\script_out\RawADList1.txt'
(get-content -path 'C:\script_out\RawADList1.txt') | ForEach-Object {$_ -replace '\*',''} >> 'C:\script_out\RawADList2.txt'
[string[]]$DropDownList = Get-Content -Path 'C:\script_out\RawADList2.txt'
Remove-Item 'C:\script_out\RawADList1.txt'
Remove-Item 'C:\script_out\RawADList2.txt'
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{net group $Choice /domain
}
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 340
$Form.height = 150
$Form.Text = "ADGroup"
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,10)
$DropDown.Size = new-object System.Drawing.Size(300,30)
ForEach ($Line in $DropDownList) {
$DropDown.Items.Add($Line)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(200,20)
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Pick One"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
When I click the Pick One button nothing happens, it doesnt reflect anything.
I Figured this out, it was as I thought VERY simple, I was just forgetting to have the command do anything with the output. Hope this might help someone else.
Clear-Variable DropDownList
$RawADGroups = Net Group /domain >> 'C:\script_out\RawADList1.txt'
(get-content -path 'C:\script_out\RawADList1.txt') | ForEach-Object {$_ -replace '\*',''} >> 'C:\script_out\RawADList2.txt'
[string[]]$DropDownList = Get-Content -Path 'C:\script_out\RawADList2.txt'
Remove-Item 'C:\script_out\RawADList1.txt'
Remove-Item 'C:\script_out\RawADList2.txt'
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{
net group $Choice /domain >> C:\script_out\output.txt
}
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 340
$Form.height = 150
$Form.Text = "ADGroup"
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,10)
$DropDown.Size = new-object System.Drawing.Size(300,30)
ForEach ($Line in $DropDownList) {
$DropDown.Items.Add($Line)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(200,20)
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Pick One"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Get number of dates as selected in Windows Forms MonthCalendar for Powershell

I am using Powershell to create a GUI calendar and I need to be able assign a variable to the number of dates selected as the user selects them. I imagine I would have to use an event but am not sure how to procede. Basically, if a user selects a range of more than 10 days I need a checkbox on the same form to be checked. I am doing something similar with a listbox but I can't seem to figure out how to do the same with the calendar. Thanks.
Listbox code:
$Listbox2 = New-Object System.Windows.Forms.ListBox
$Listbox2.Location = New-Object System.Drawing.Size(240,80)
$Listbox2.Size = New-Object System.Drawing.Size(140,210)
$Listbox2.Height = 210
$Listbox2.SelectionMode = "MultiExtended"
$Listbox2Content | ForEach-Object {[void] $Listbox2.Items.Add($_)}
$Listbox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$Listbox2.Add_SelectedValueChanged({
If (($Listbox2.SelectedItems).Count -ge 10) {$Checkbox2.Checked = $True}
If (($Listbox2.SelectedItems).Count -lt 10) {$Checkbox2.Checked = $False}
})
Calendar Code:
$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.Location = New-Object System.Drawing.Size(12,80)
$Calendar.ShowTodayCircle = $False
$Calendar.ShowToday = $True
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.MinDate = $OldestLog
$Calendar.MaxSelectionCount = "$CalendarDateRange"
$MenuBox.Controls.Add($Calendar)
The $data_selected scriptblock prints the number of selected dates. I attached it to the DataSelected event so it fires when you click on a date or make a range selection.
Add-Type -AssemblyName System.Windows.Forms
$date_selected = {
write-host (($_.End - $_.Start).Days + 1)
}
$form = New-Object Windows.Forms.Form
$form.text = "Calendar"
$form.FormBorderStyle = 'FixedDialog'
$form.Size = New-Object Drawing.Size(190,190)
$cal = New-Object System.Windows.Forms.MonthCalendar
$cal.add_DateSelected($date_selected)
$form.Controls.Add($cal)
$form.Add_Shown($form.Activate())
$form.showdialog() | Out-Null