Get-ADUser Filtering not working

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.
Locked
User avatar
Chris.Ediger
Posts: 24
Joined: Tue Oct 24, 2017 9:33 am

Get-ADUser Filtering not working

Post by Chris.Ediger » Fri Oct 26, 2018 9:39 am

I am building a tool in which I have a combobox that contains a users AD Displayname (Lastname, Firstname). I want to use the Displayname later on in the script to filter and find the users SAMAccountName so that I can use Set-ADUser to add text to the comment field of the users attribute. I get the error:

ERROR: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again

$SAMAccountName = Get-ADUser -Filter {DisplayName -like $comboboxUserName.Text} -Properties SAMAccountName,Displayname
Set-ADUser -Identity $SAMAccountName -Replace @{ Comment = "Bitlocker PIN" + $textboxBitlocker.Text} -Credential $DomainAdminCredentials

Here's the whole script:

Code: Select all

$formUpdateBitlockerPIN_Load={
#TODO: Initialize Form Controls here
Import-Module ActiveDirectory

# Get Domain Admin Credentials
do
{
$Global:DomainAdminCredentials = Get-Credential -message "Enter Domain Admin credentials" #Read credentials
$username = $DomainAdminCredentials.username
$password = $DomainAdminCredentials.GetNetworkCredential().password

# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([adsi]"").DistinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry ($CurrentDomain, $UserName, $Password)

if ($domain.Name -eq $null)
{
[void][System.Windows.Forms.MessageBox]::Show("Username or Password Incorrect", "Warning")
$Authenticated = $false
}
else
{
$Authenticated = $true
}
}
while ($Authenticated -eq $false)

# Collect computers and users
$Global:ADComuters = Get-ADComputer -Filter * -SearchBase "OU=MY,OU=SEARCH,DC=BASE" | Select-Object -ExpandProperty Name | Sort-Object
$Global:AdUsers = Get-ADUser -Filter * -Properties DisplayName,SamAccountName | Select-Object -ExpandProperty DisplayName | Sort-Object
Update-ComboBox $comboboxComputerName $ADComuters
Update-ComboBox $comboboxUserName $AdUsers
}

#region Control Helper Functions
function Update-ComboBox
{
<#
.SYNOPSIS
This functions helps you load items into a ComboBox.

.DESCRIPTION
Use this function to dynamically load items into the ComboBox control.

.PARAMETER ComboBox
The ComboBox control you want to add items to.

.PARAMETER Items
The object or objects you wish to load into the ComboBox's Items collection.

.PARAMETER DisplayMember
Indicates the property to display for the items in this control.

.PARAMETER ValueMember
Indicates the property to use for the value of the control.

.PARAMETER Append
Adds the item(s) to the ComboBox without clearing the Items collection.

.EXAMPLE
Update-ComboBox $combobox1 "Red", "White", "Blue"

.EXAMPLE
Update-ComboBox $combobox1 "Red" -Append
Update-ComboBox $combobox1 "White" -Append
Update-ComboBox $combobox1 "Blue" -Append

.EXAMPLE
Update-ComboBox $combobox1 (Get-Process) "ProcessName"

.NOTES
Additional information about the function.
#>

param
(
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[System.Windows.Forms.ComboBox]
$ComboBox,
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
$Items,
[Parameter(Mandatory = $false)]
[string]$DisplayMember,
[Parameter(Mandatory = $false)]
[string]$ValueMember,
[switch]
$Append
)

if (-not $Append)
{
$ComboBox.Items.Clear()
}

if ($Items -is [Object[]])
{
$ComboBox.Items.AddRange($Items)
}
elseif ($Items -is [System.Collections.IEnumerable])
{
$ComboBox.BeginUpdate()
foreach ($obj in $Items)
{
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
}
else
{
$ComboBox.Items.Add($Items)
}

$ComboBox.DisplayMember = $DisplayMember
$ComboBox.ValueMember = $ValueMember
}
#endregion

$comboboxComputerName_SelectedIndexChanged={
#TODO: Place custom script here

}

$buttonOK_Click={
#TODO: Place custom script here
# For computer
Set-ADComputer -Identity $comboboxComputerName.Text -Replace @{ Comment = "Bitlocker PIN:" + $textboxBitlocker.text } -Credential $DomainAdminCredentials
# For Users
$SAMAccountName = Get-ADUser -Filter {DisplayName -like $comboboxUserName.Text} -Properties SAMAccountName,Displayname
Set-ADUser -Identity $User -Replace @{ Comment = "Bitlocker PIN" + $textboxBitlocker.Text} -Credential $DomainAdminCredentials
}

User avatar
mxtrinidad
Site Admin
Posts: 315
Joined: Sun Mar 03, 2013 12:42 pm

Re: Get-ADUser Filtering not working

Post by mxtrinidad » Fri Oct 26, 2018 10:18 am

You missed to display the selected item(s) from the combo box.
For Example:

Code: Select all

$combobox1_SelectedIndexChanged = {
	#TODO: Place custom script here
	$labelDisplayComboSelected.Text = $combobox1.SelectedItem.ToString();
}
:)

User avatar
Chris.Ediger
Posts: 24
Joined: Tue Oct 24, 2017 9:33 am

Re: Get-ADUser Filtering not working

Post by Chris.Ediger » Fri Oct 26, 2018 10:30 am

I'm still a little new to this so I'm not entirely sure what you are suggesting I do. Does that snippet of code go somewhere in my script? If so where...?

User avatar
mxtrinidad
Site Admin
Posts: 315
Joined: Sun Mar 03, 2013 12:42 pm

Re: Get-ADUser Filtering not working

Post by mxtrinidad » Fri Oct 26, 2018 11:10 am

Ah!

First, in the form designer, double-click on the ComboBox field. This will create the event block "$combobox1_SelectedIndexChanged".
In this event block, you'll add the code to save the value selected from the combox box:
$labelDisplayComboSelected.Text = $combox1.SelectedItem.ToString()

In the example I provided showing saving to a Label Control Text property named: $labelDisplayComboSelected.Text
For you, it could look like:
$ComputerName = $comboboxComputerName.SelectedItem.ToString()

So, for each of the ComboBox Control, you need to create the combo box *_SelectedIndexChanged event in order to add the code to grab the selected value.
Then, you can consume the value(s) with the Set-ADUser cmdlet.
:)

User avatar
Chris.Ediger
Posts: 24
Joined: Tue Oct 24, 2017 9:33 am

Re: Get-ADUser Filtering not working

Post by Chris.Ediger » Fri Oct 26, 2018 11:45 am

That worked. I'm not going to say I understand why but it works. Thank you!

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

Re: Get-ADUser Filtering not working

Post by jvierra » Fri Oct 26, 2018 11:51 am

mxtrinidad wrote:
Fri Oct 26, 2018 10:18 am
You missed to display the selected item(s) from the combo box.
For Example:

Code: Select all

$combobox1_SelectedIndexChanged = {
	#TODO: Place custom script here
	$labelDisplayComboSelected.Text = $combobox1.SelectedItem.ToString();
}
:)
Note that the C# examples always add "ToString()". This is completely unnecessary in PowerShell. The shell manages conversions and conversion types correctly. In this case adding it won't hurt. If the item is an object and we want a specific property the "ToString()" will either give us an incorrect property or it will give us the object type name.

Locked