Get-ADUser:无法验证参数'identity'。参数为空

时间:2019-08-08 20:26:48

标签: powershell

我正在处理一个脚本,它的一部分依赖于从列表框中选择一个AD用户。问题是所选用户返回“ Null”。看看下面的代码!

$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

选择用户之后,我们应该能够使用$Employee变量运行更多与AD相关的命令。下面是错误。

   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

2 个答案:

答案 0 :(得分:0)

您的$Employee变量进入Get-ADUser -Identity $Employee调用时必须为null。 $Employee来自$Employee = Get-ADUser -Filter {SamAccountName -eq $Name}行,因此必须是AD找不到具有SamAccountName = $Name的用户。

$Employee变量写一些输出,看看它是否确实为空。然后找出$Name变量是否正确以及该人是否存在于广告中。

答案 1 :(得分:0)

我建议使用列表框SelectedIndex代替SelectedItem属性。

而且,为什么不首先使用Get-ADObject,而不要使用Get-ADUser

$form.Topmost = $true下方的表单生成部分开始,这应该对您有用:

# 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