我用AD SamAccountName项填充了一个组合框。选择其中一项时,可以按下按钮以从该帐户中检索信息。但是,当单击按钮时,出现以下错误:
Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
错误所指的命令是:
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
该代码的关键部分是:
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$ComboBox.Width = 300
$ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
ForEach ($User in $Users){
$ComboBox.Items.Add($User.SamAccountName)
}
$ComboBox.SelectedIndex = 1
$MainWindow.Controls.Add($ComboBox)
$Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
$Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
$Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
$Button_Check_TEST.Text = 'Check'
$MainWindow.Controls.Add($Button_Check_TEST)
$Button_Check_TEST.Add_Click({
Try{
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
Catch{
Write-Verbose -Verbose $_.Exception.Message
}
})
问题是,我需要两层。基本上,应该有一个包含四个不同选项的菜单,其中一个是“用户”。单击“用户”时,将出现组合框和“单击”按钮。 在没有“用户”按钮的情况下使用上面的代码可以正常工作。
问题:为什么我使用按钮“创建” ComboBox时ComboBox.SelectedItem不起作用?
完整的代码如下:
$font = New-Object -TypeName System.Drawing.Font("Times New Roman", 18, [System.Drawing.FontStyle]::Bold)
$MainWindow = New-Object -TypeName System.Windows.Forms.Form
$MainWindow.Text = 'PIM v10 Administrator Window'
$MainWindow.Width = 600
$MainWindow.Height = 555
$MainWindow.AutoSize = $true
$Button_User = New-Object -TypeName System.Windows.Forms.Button
$Button_User.Location = New-Object -TypeName System.Drawing.Size(25, 25)
$Button_User.Size = New-Object -TypeName System.Drawing.Size(200, 75)
$Button_User.Text = 'User'
$Button_User.Font = $font
$MainWindow.Controls.Add($Button_User)
$Button_User.Add_Click({
$Label_User = New-Object -TypeName System.Windows.Forms.Label
$Label_User.Text = 'Given Name:'
$Label_User.Location = New-Object -TypeName System.Drawing.Point(250, 50)
$Label_User.AutoSize = $true
$MainWindow.Controls.Add($Label_User)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Label_User_ItemContent.Text = ''
$Label_User_ItemContent.Location = New-Object -TypeName System.Drawing.Point(250, 100)
$MainWindow.Controls.Add($Label_User_ItemContent)
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$ComboBox.Width = 300
$ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
ForEach ($User in $Users){
$ComboBox.Items.Add($User.SamAccountName)
}
$ComboBox.SelectedIndex = 1
$MainWindow.Controls.Add($ComboBox)
$Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
$Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
$Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
$Button_Check_TEST.Text = 'Check'
$MainWindow.Controls.Add($Button_Check_TEST)
$Button_Check_TEST.Add_Click({
Try{
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
Catch{
Write-Verbose -Verbose $_.Exception.Message
}
})
if (-not ($ComboBox.SelectedItem -eq $null)){
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
else {
Write-Host -Object "Object is null"
}
})
$MainWindow.ShowDialog()
答案 0 :(得分:1)
所以发生的事情是您在错误的范围内创建了变量
Function<Object, String>
由于要在Add_click动作中创建组合和标签。这些值仅在执行操作时和在$Button_User.Add_Click({
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$MainWindow.Controls.Add($ComboBox)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_Check_TEST.Add_Click({
$Label_User_ItemContent.Text = (Get-ADUser -Identity
$ComboBox.SelectedItem).SamAccountName
)}
})
中存在。然后从内存中清除项目
当您执行下一个操作$MainWindows.Controls
时,由于变量已清除,因此$Button_Check_TEST.Add_Click()
和$ComboBox
等于零。
一种解决方法是将它们放置在$Label_User_ItemContent
事件之外
$Button_User.Add_Click()
现在整个脚本都处于工作状态
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_User.Add_Click({
$MainWindow.Controls.Add($ComboBox)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_Check_TEST.Add_Click({
$Label_User_ItemContent.Text = (Get-ADUser -Identity
$ComboBox.SelectedItem).SamAccountName
)}
})