I'm working on a script and part of it relies on selecting a AD user from a list box. Problem is that the selected user is coming back 'Null'. Have a look at the code below!
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Account Selection'
$form.Size = New-Object System.Drawing.Size (400,250)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point (110,165)
$OKButton.Size = New-Object System.Drawing.Size (75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point (190,165)
$CancelButton.Size = New-Object System.Drawing.Size (75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point (10,0)
$label.Size = New-Object System.Drawing.Size (280,20)
$label.Text = 'Select the user account'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point (10,40)
$listBox.Size = New-Object System.Drawing.Size (363,150)
$listBox.Height = 120
$form.Controls.Add($listBox)
$form.Topmost = $true
$ADUserGroup = Get-ADObject -Filter 'ObjectClass -eq "User"' -SearchBase 'OU=Users,DC=Company,DC=com' | sort name
foreach ($User in $ADUserGroup)
{
$listBox.Items.Add($User.Name) | Out-Null
}
$result = $form.ShowDialog()
#Store results
if ($result -eq 'Cancel') {exit}
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$Name = $listBox.SelectedItem
$Employee = Get-ADUser -Filter {SamAccountName -eq $Name}
}
Get-ADUser -Identity $Employee
After the user is selected we should be able to run more AD related commands using the $Employee
variable. Below is the error.
Get-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.
At line:69 char:22
+ Get-ADUser -Identity $Employee
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
It must be that your $Employee
variable is null when it gets to the Get-ADUser -Identity $Employee
call. $Employee
comes from the line $Employee = Get-ADUser -Filter {SamAccountName -eq $Name}
, so it must be that AD can't find a user with SamAccountName = $Name
.
Write some output for the $Employee
variable and see if it is indeed null. Then figure out whether the $Name
variable is correct and if that person exists in AD.
I would suggest using the listboxes SelectedIndex instead of the SelectedItem
property.
Also, instead of using Get-ADObject
, why not use Get-ADUser
in the first place.
Taken from the part where the form is built, just below $form.Topmost = $true
, this should work for you:
# Get an array of all user objects in the given OU and sort by property Name
# By default, these objects will have the following properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass,
# ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# If you need more or other properties, then you need to add the -Properties
# parameter to the Get-ADUser cmdlet below.
$ADUserGroup = Get-ADUser -SearchBase 'OU=Users,DC=Company,DC=com' | Sort-Object Name
foreach ($User in $ADUserGroup) {
$listBox.Items.Add($User.Name) | Out-Null
}
$result = $form.ShowDialog()
$selectedIndex = $listBox.SelectedIndex
# close and remove the form
$form.Dispose()
# handle the results
if ($result -eq [System.Windows.Forms.DialogResult]::OK -and $selectedIndex -ge 0) {
# store the selected AD user object in variable $Employee and do the rest of your code with it
$Employee = $ADUserGroup[$selectedIndex]
}
# $Employee now holds the ADUser object with all properties you asked for or is $null if no selection was made
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.