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: 26
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:
  1. $formUpdateBitlockerPIN_Load={
  2.  
  3.     #TODO: Initialize Form Controls here
  4.  
  5.     Import-Module ActiveDirectory
  6.  
  7.    
  8.  
  9.     # Get Domain Admin Credentials
  10.  
  11.     do
  12.  
  13.     {
  14.  
  15.         $Global:DomainAdminCredentials = Get-Credential -message "Enter Domain Admin credentials" #Read credentials
  16.  
  17.         $username = $DomainAdminCredentials.username
  18.  
  19.         $password = $DomainAdminCredentials.GetNetworkCredential().password
  20.  
  21.        
  22.  
  23.         # Get current domain using logged-on user's credentials
  24.  
  25.         $CurrentDomain = "LDAP://" + ([adsi]"").DistinguishedName
  26.  
  27.         $domain = New-Object System.DirectoryServices.DirectoryEntry ($CurrentDomain, $UserName, $Password)
  28.  
  29.        
  30.  
  31.         if ($domain.Name -eq $null)
  32.  
  33.         {
  34.  
  35.             [void][System.Windows.Forms.MessageBox]::Show("Username or Password Incorrect", "Warning")
  36.  
  37.             $Authenticated = $false
  38.  
  39.         }
  40.  
  41.         else
  42.  
  43.         {
  44.  
  45.             $Authenticated = $true
  46.  
  47.         }
  48.  
  49.     }
  50.  
  51.     while ($Authenticated -eq $false)
  52.  
  53.    
  54.  
  55.     # Collect computers and users
  56.  
  57.     $Global:ADComuters = Get-ADComputer -Filter * -SearchBase "OU=MY,OU=SEARCH,DC=BASE" | Select-Object -ExpandProperty Name | Sort-Object
  58.  
  59.     $Global:AdUsers = Get-ADUser -Filter * -Properties DisplayName,SamAccountName | Select-Object -ExpandProperty DisplayName | Sort-Object
  60.  
  61.     Update-ComboBox $comboboxComputerName $ADComuters
  62.  
  63.     Update-ComboBox $comboboxUserName $AdUsers
  64.  
  65. }
  66.  
  67.  
  68.  
  69. #region Control Helper Functions
  70.  
  71. function Update-ComboBox
  72.  
  73. {
  74.  
  75. <#
  76.  
  77.     .SYNOPSIS
  78.  
  79.         This functions helps you load items into a ComboBox.
  80.  
  81.    
  82.  
  83.     .DESCRIPTION
  84.  
  85.         Use this function to dynamically load items into the ComboBox control.
  86.  
  87.    
  88.  
  89.     .PARAMETER ComboBox
  90.  
  91.         The ComboBox control you want to add items to.
  92.  
  93.    
  94.  
  95.     .PARAMETER Items
  96.  
  97.         The object or objects you wish to load into the ComboBox's Items collection.
  98.  
  99.    
  100.  
  101.     .PARAMETER DisplayMember
  102.  
  103.         Indicates the property to display for the items in this control.
  104.  
  105.        
  106.  
  107.     .PARAMETER ValueMember
  108.  
  109.         Indicates the property to use for the value of the control.
  110.  
  111.    
  112.  
  113.     .PARAMETER Append
  114.  
  115.         Adds the item(s) to the ComboBox without clearing the Items collection.
  116.  
  117.    
  118.  
  119.     .EXAMPLE
  120.  
  121.         Update-ComboBox $combobox1 "Red", "White", "Blue"
  122.  
  123.    
  124.  
  125.     .EXAMPLE
  126.  
  127.         Update-ComboBox $combobox1 "Red" -Append
  128.  
  129.         Update-ComboBox $combobox1 "White" -Append
  130.  
  131.         Update-ComboBox $combobox1 "Blue" -Append
  132.  
  133.    
  134.  
  135.     .EXAMPLE
  136.  
  137.         Update-ComboBox $combobox1 (Get-Process) "ProcessName"
  138.  
  139.    
  140.  
  141.     .NOTES
  142.  
  143.         Additional information about the function.
  144.  
  145. #>
  146.  
  147.    
  148.  
  149.     param
  150.  
  151.     (
  152.  
  153.         [Parameter(Mandatory = $true)]
  154.  
  155.         [ValidateNotNull()]
  156.  
  157.         [System.Windows.Forms.ComboBox]
  158.  
  159.         $ComboBox,
  160.  
  161.         [Parameter(Mandatory = $true)]
  162.  
  163.         [ValidateNotNull()]
  164.  
  165.         $Items,
  166.  
  167.         [Parameter(Mandatory = $false)]
  168.  
  169.         [string]$DisplayMember,
  170.  
  171.         [Parameter(Mandatory = $false)]
  172.  
  173.         [string]$ValueMember,
  174.  
  175.         [switch]
  176.  
  177.         $Append
  178.  
  179.     )
  180.  
  181.    
  182.  
  183.     if (-not $Append)
  184.  
  185.     {
  186.  
  187.         $ComboBox.Items.Clear()
  188.  
  189.     }
  190.  
  191.    
  192.  
  193.     if ($Items -is [Object[]])
  194.  
  195.     {
  196.  
  197.         $ComboBox.Items.AddRange($Items)
  198.  
  199.     }
  200.  
  201.     elseif ($Items -is [System.Collections.IEnumerable])
  202.  
  203.     {
  204.  
  205.         $ComboBox.BeginUpdate()
  206.  
  207.         foreach ($obj in $Items)
  208.  
  209.         {
  210.  
  211.             $ComboBox.Items.Add($obj)
  212.  
  213.         }
  214.  
  215.         $ComboBox.EndUpdate()
  216.  
  217.     }
  218.  
  219.     else
  220.  
  221.     {
  222.  
  223.         $ComboBox.Items.Add($Items)
  224.  
  225.     }
  226.  
  227.    
  228.  
  229.     $ComboBox.DisplayMember = $DisplayMember
  230.  
  231.     $ComboBox.ValueMember = $ValueMember
  232.  
  233. }
  234.  
  235. #endregion
  236.  
  237.  
  238.  
  239. $comboboxComputerName_SelectedIndexChanged={
  240.  
  241.     #TODO: Place custom script here
  242.  
  243.    
  244.  
  245. }
  246.  
  247.  
  248.  
  249. $buttonOK_Click={
  250.  
  251.     #TODO: Place custom script here
  252.  
  253.     # For computer
  254.  
  255.     Set-ADComputer -Identity $comboboxComputerName.Text -Replace @{ Comment = "Bitlocker PIN:" + $textboxBitlocker.text } -Credential $DomainAdminCredentials
  256.  
  257.     # For Users
  258.  
  259.     $SAMAccountName = Get-ADUser -Filter {DisplayName -like $comboboxUserName.Text} -Properties SAMAccountName,Displayname
  260.  
  261.     Set-ADUser -Identity $User -Replace @{ Comment = "Bitlocker PIN" + $textboxBitlocker.Text} -Credential $DomainAdminCredentials
  262.  
  263. }

User avatar
mxtrinidad
Site Admin
Posts: 359
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: 26
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: 359
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: 26
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!

jvierra
Posts: 13901
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