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:
- $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
- }