学习powershell,并且在使用-Filter时遇到一些问题

wfveoks0  于 2023-01-05  发布在  Shell
关注(0)|答案(1)|浏览(185)

下面的脚本遇到了一些问题,奇怪的是,当我单独运行这个明显有问题的部分时,它运行得很好,只是当它试图在add_Click函数中运行时,它抛出了一个错误消息。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

$SearchTermTest=Read-Host -Prompt "Enter First or last name"
$UsersTest = Get-ADUser -Filter "GivenName -eq '$SearchTermTest' -or SurName -eq '$SearchTermTest'"
Write-Output $UsersTest

# Create the form
$SearchForm = New-Object System.Windows.Forms.Form
$SearchForm.Text = "Search User"
$SearchForm.Size = New-Object System.Drawing.Size(500,500)

# Create the label and textbox for user input
$FirstLastNameLabel = New-Object System.Windows.Forms.Label
$FirstLastNameLabel.Location = New-Object System.Drawing.Size(20,20)
$FirstLastNameLabel.Text = "Enter First or Last Name"
$SearchForm.Controls.Add($FirstLastNameLabel)

$InputTextBox = New-Object System.Windows.Forms.TextBox
$InputTextBox.Location = New-Object System.Drawing.Size(150,20)
$SearchForm.Controls.Add($InputTextBox)

# Create the DataGridView
$DataGridView1 = New-Object System.Windows.Forms.DataGridView
$DataGridView1.Location = New-Object System.Drawing.Size(20,50)
$DataGridView1.Size = New-Object System.Drawing.Size(450,400)
$SearchForm.Controls.Add($DataGridView1)

# Create the search button
$SearchButton = New-Object System.Windows.Forms.Button
$SearchButton.Location = New-Object System.Drawing.Size(280,20)
$SearchButton.Size = New-Object System.Drawing.Size(100,20)
$SearchButton.Text = "Search"
$SearchButton.Add_Click({
    $SearchTerm = $InputTextBox.Text
    $Users = Get-ADUser -Filter "GivenName -eq '$SearchTerm' -or SurName -eq '$SearchTerm'"
    $DataGridView1.DataSource = $null
    $DataGridView1.DataSource = $Users
    $DataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells)
})
$SearchForm.Controls.Add($SearchButton)

$SearchForm.ShowDialog()
Get-ADUser : The search filter cannot be recognized
At C:\Users\ad_forrest.vasilinda\Desktop\user password search.ps1:36 char:14
+ ...    $Users = Get-ADUser -Filter "GivenName -eq '$SearchTerm' -or SurNa ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

错误信息在上面。有什么想法吗?老实说,我已经无计可施了。我有一个完全相同的GetADUser语句,在脚本的开头用相同的过滤器运行,没有任何错误,只是在add_click函数中抛出了它。
更新--将一些变量名更改为更具描述性的名称,并且不匹配任何保留名称,现在只会在字段为空时弹出错误,但是,现在当字段中有文本时,我按下按钮,什么也不会发生。

yzuktlbb

yzuktlbb1#

我也得到了一个空白屏幕,所以我做了一点改变,并添加了一些错误检查。现在你可以搜索一个空字符串或名称的一部分。我把过滤器改为-喜欢,这样你会得到更好的结果。
我添加了一个变量$properties,以便您可以添加/删除要使用的AD属性。
干杯

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

# Properties to display
$properties = "GivenName","SurName","Mail","UserPrincipalName","Company"

# Create the form
$SearchForm = New-Object System.Windows.Forms.Form
$SearchForm.Text = "Search User"
$SearchForm.Size = New-Object System.Drawing.Size(800,500)

# Create the label and textbox for user input
$FirstLastNameLabel = New-Object System.Windows.Forms.Label
$FirstLastNameLabel.Location = New-Object System.Drawing.Size(20,20)
$FirstLastNameLabel.Text = "Enter First or Last Name"
$SearchForm.Controls.Add($FirstLastNameLabel)

$InputTextBox = New-Object System.Windows.Forms.TextBox
$InputTextBox.Location = New-Object System.Drawing.Size(160,20)
$SearchForm.Controls.Add($InputTextBox)

# Create the DataGridView
$DataGridView1 = New-Object System.Windows.Forms.DataGridView
$DataGridView1.Location = New-Object System.Drawing.Size(20,50)
$DataGridView1.Size = New-Object System.Drawing.Size(750,400)
$dataGridView1.ColumnCount = $properties.count
$dataGridView1.ColumnHeadersVisible = $true
$i=0
$properties | %{
    $dataGridView1.Columns[$i++].Name = $_
}
$SearchForm.Controls.Add($DataGridView1)

# Create the search button
$SearchButton = New-Object System.Windows.Forms.Button
$SearchButton.Location = New-Object System.Drawing.Size(280,20)
$SearchButton.Size = New-Object System.Drawing.Size(100,20)
$SearchButton.Text = "Search"
$SearchButton.add_click({
    $SearchTerm = ($InputTextBox.Text).trim()
    $filter = "*"
    if ($searchTerm) {
        $filter = "GivenName -like '*$SearchTerm*' -or SurName -like '*$SearchTerm*'"
    }
    [array]$users = Get-ADUser -Filter $filter -Properties $properties | select $properties
    $DataGridView1.Rows.Clear()
    if ($users) {
        $users | %{
            $DataGridView1.Rows.Add($_.psobject.Properties.value)
        }
    }
    $DataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells)
})
$SearchForm.Controls.Add($SearchButton)
$SearchForm.ShowDialog()

相关问题