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.
This topic is 5 years and 5 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
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Get-ADUser Filtering not working

Post by Chris_Ediger »

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:
  1. $formUpdateBitlockerPIN_Load={
  2.     #TODO: Initialize Form Controls here
  3.     Import-Module ActiveDirectory
  4.    
  5.     # Get Domain Admin Credentials
  6.     do
  7.     {
  8.         $Global:DomainAdminCredentials = Get-Credential -message "Enter Domain Admin credentials" #Read credentials
  9.         $username = $DomainAdminCredentials.username
  10.         $password = $DomainAdminCredentials.GetNetworkCredential().password
  11.        
  12.         # Get current domain using logged-on user's credentials
  13.         $CurrentDomain = "LDAP://" + ([adsi]"").DistinguishedName
  14.         $domain = New-Object System.DirectoryServices.DirectoryEntry ($CurrentDomain, $UserName, $Password)
  15.        
  16.         if ($domain.Name -eq $null)
  17.         {
  18.             [void][System.Windows.Forms.MessageBox]::Show("Username or Password Incorrect", "Warning")
  19.             $Authenticated = $false
  20.         }
  21.         else
  22.         {
  23.             $Authenticated = $true
  24.         }
  25.     }
  26.     while ($Authenticated -eq $false)
  27.    
  28.     # Collect computers and users
  29.     $Global:ADComuters = Get-ADComputer -Filter * -SearchBase "OU=MY,OU=SEARCH,DC=BASE" | Select-Object -ExpandProperty Name | Sort-Object
  30.     $Global:AdUsers = Get-ADUser -Filter * -Properties DisplayName,SamAccountName | Select-Object -ExpandProperty DisplayName | Sort-Object
  31.     Update-ComboBox $comboboxComputerName $ADComuters
  32.     Update-ComboBox $comboboxUserName $AdUsers
  33. }
  34.  
  35. #region Control Helper Functions
  36. function Update-ComboBox
  37. {
  38. <#
  39.     .SYNOPSIS
  40.         This functions helps you load items into a ComboBox.
  41.    
  42.     .DESCRIPTION
  43.         Use this function to dynamically load items into the ComboBox control.
  44.    
  45.     .PARAMETER ComboBox
  46.         The ComboBox control you want to add items to.
  47.    
  48.     .PARAMETER Items
  49.         The object or objects you wish to load into the ComboBox's Items collection.
  50.    
  51.     .PARAMETER DisplayMember
  52.         Indicates the property to display for the items in this control.
  53.        
  54.     .PARAMETER ValueMember
  55.         Indicates the property to use for the value of the control.
  56.    
  57.     .PARAMETER Append
  58.         Adds the item(s) to the ComboBox without clearing the Items collection.
  59.    
  60.     .EXAMPLE
  61.         Update-ComboBox $combobox1 "Red", "White", "Blue"
  62.    
  63.     .EXAMPLE
  64.         Update-ComboBox $combobox1 "Red" -Append
  65.         Update-ComboBox $combobox1 "White" -Append
  66.         Update-ComboBox $combobox1 "Blue" -Append
  67.    
  68.     .EXAMPLE
  69.         Update-ComboBox $combobox1 (Get-Process) "ProcessName"
  70.    
  71.     .NOTES
  72.         Additional information about the function.
  73. #>
  74.    
  75.     param
  76.     (
  77.         [Parameter(Mandatory = $true)]
  78.         [ValidateNotNull()]
  79.         [System.Windows.Forms.ComboBox]
  80.         $ComboBox,
  81.         [Parameter(Mandatory = $true)]
  82.         [ValidateNotNull()]
  83.         $Items,
  84.         [Parameter(Mandatory = $false)]
  85.         [string]$DisplayMember,
  86.         [Parameter(Mandatory = $false)]
  87.         [string]$ValueMember,
  88.         [switch]
  89.         $Append
  90.     )
  91.    
  92.     if (-not $Append)
  93.     {
  94.         $ComboBox.Items.Clear()
  95.     }
  96.    
  97.     if ($Items -is [Object[]])
  98.     {
  99.         $ComboBox.Items.AddRange($Items)
  100.     }
  101.     elseif ($Items -is [System.Collections.IEnumerable])
  102.     {
  103.         $ComboBox.BeginUpdate()
  104.         foreach ($obj in $Items)
  105.         {
  106.             $ComboBox.Items.Add($obj)
  107.         }
  108.         $ComboBox.EndUpdate()
  109.     }
  110.     else
  111.     {
  112.         $ComboBox.Items.Add($Items)
  113.     }
  114.    
  115.     $ComboBox.DisplayMember = $DisplayMember
  116.     $ComboBox.ValueMember = $ValueMember
  117. }
  118. #endregion
  119.  
  120. $comboboxComputerName_SelectedIndexChanged={
  121.     #TODO: Place custom script here
  122.    
  123. }
  124.  
  125. $buttonOK_Click={
  126.     #TODO: Place custom script here
  127.     # For computer
  128.     Set-ADComputer -Identity $comboboxComputerName.Text -Replace @{ Comment = "Bitlocker PIN:" + $textboxBitlocker.text } -Credential $DomainAdminCredentials
  129.     # For Users
  130.     $SAMAccountName = Get-ADUser -Filter {DisplayName -like $comboboxUserName.Text} -Properties SAMAccountName,Displayname
  131.     Set-ADUser -Identity $User -Replace @{ Comment = "Bitlocker PIN" + $textboxBitlocker.Text} -Credential $DomainAdminCredentials
  132. }
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Get-ADUser Filtering not working

Post by mxtrinidad »

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: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Get-ADUser Filtering not working

Post by Chris_Ediger »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Get-ADUser Filtering not working

Post by mxtrinidad »

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: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Get-ADUser Filtering not working

Post by Chris_Ediger »

That worked. I'm not going to say I understand why but it works. Thank you!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get-ADUser Filtering not working

Post by jvierra »

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.
This topic is 5 years and 5 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