0

So I am having some issues with the below script. The weird part is, when I run the apparently offending part separately by itself, it works fine. It's only when it tries to run in the add_Click function that it throws an error message.

[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

Error message is above. Any ideas? I am at my wits end honestly. I have the exact same GetADUser statement running with the same filter at the beginning of the script with no errors, its only in the add_click function it throws it.

Update -- changing some of the variable names to be more descriptive and not match any of the reserved names now only causes the error to pop up when the field is left blank. However, now when there is text in the field and I push the button, nothing happens.

  • 1
    `$input` is an automatic variable. You should never use that name for self defined variables. Choose a better name for that like `$inputTextBox` for instance. Also, is that `{` just in front of Get-ADUser a typo? – Theo Dec 14 '22 at 21:17
  • That was just a typo yes, I tried wrapping it in {} to see if that would change anything, but looks like I missed taking the first one out when I was changing it back. And appreciate the advise on the variable naming – Forrest Vasilinda Dec 14 '22 at 21:33
  • Try `$SearchTerm = $InputTextBox.Text.Trim()` and make sure that the search term contains no `'` chars. - you'd have to escape them as `''` – mklement0 Dec 14 '22 at 22:26
  • @mklement0 Nope, still the same behavior even with that change – Forrest Vasilinda Dec 14 '22 at 22:33
  • Does `Get-ADUser -Filter 'GivenName -eq $SearchTerm -or SurName -eq $SearchTerm'` (sic) at least produce a different symptom? – mklement0 Dec 14 '22 at 22:35
  • Don't know if you read close enough in the OP, but I do have literally the same filter running at the beginning of the script with no issues, it's only when its in the add_click function it has issues. – Forrest Vasilinda Dec 14 '22 at 22:48
  • @ForrestVasilinda, you missed the `'` around the `-Filter` argument in my previous suggestion. Also, the value of `$SearchText` matters with respect to whether your `"..."`-based `-Filter` breaks or not. – mklement0 Dec 14 '22 at 22:52
  • 2 things 1)You are right, I missed that on your suggested code statement, copy/pasted it directly to ensure it as exact and now it is back to the original behavior described in my update in the OP 2)I am aware of that, and make sure to always enter the same thing for both the test code in the beginning and the textbox when the actual GUI appears, and it works fine when I enter it directly into the prompt in the console, but when entering the exact same thing into the textbox is was causes the issue/errors – Forrest Vasilinda Dec 14 '22 at 23:02
  • So I replaced the $SearchTerm assignment with the same Read-Host prompt as my test code at the beginning just to ensure everything would match exactly when running the filter, and noted that when I type something and then hit enter, it just shows the cursor blinking on the next line by itself until the search button is pushed again which brings up the same prompt again. – Forrest Vasilinda Dec 14 '22 at 23:07
  • _Manually attempting_ to make sure that two inputs are the same isn't necessarily the same as actually ensuring they are. _Hard-code_ a `$SearchTerm` value in both scenarios, and if it only works in one of them, further investigation is needed. – mklement0 Dec 14 '22 at 23:09
  • Okay good point and suggestion, I hard coded a variable as $SearchTermHC = "forrest" and used it in both the code at the beginning, and the add click function. Again it is running fine at the beginning and outputs the expected list of user objects to the console, but when I click the search button in the GUI nothing happens. – Forrest Vasilinda Dec 14 '22 at 23:13
  • `$SearchTermHC` is different from `$SearchTerm` - did you account for that in the `-Filter` argument? Are you saying that you _no longer get an error_? – mklement0 Dec 14 '22 at 23:16
  • Yes I changed it in both places for the -Filter arguments. And not getting an error is not new behavior, if you read the OP, I specify in my update from before you and I started working together here in the comments that the error is only thrown when the textfield is left blank, whenever there is something in the textfield there is no error but nothing happens. – Forrest Vasilinda Dec 14 '22 at 23:31

1 Answers1

0

I was getting a blank screen as well so I changed it a bit and added some error checking. Now you can search an empty string or part of the name. I changed the filter to -Like so you get better results.

I added a variable $properties so you can add/remove the AD attributes you would like to use.

Cheers

[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()
  • Oddly enough changing all my variable names to be more descriptive and not match any of the reserved ones now only causes the error to show when I leave the field blank and push the button. However, when I put any text in the field and hit enter, now nothing happens at all. No error, and nothing is populating the table. I even changed it to write the $Users straight to the console and still nothing. – Forrest Vasilinda Dec 14 '22 at 21:57
  • 1
    My man! Just wish I knew what was wrong in the first place just for the knowledge since I am still learning, but nonetheless thank you very much :) Having a working script is still useful and I will take a look to see what you did different and take into account for the future. Thanks again! – Forrest Vasilinda Dec 14 '22 at 23:40
  • You're welcome. Assuming you tested it and it works for you? – John Joseph Trovato Dec 15 '22 at 00:10
  • Hi sorry missed your comment but yes it is working for what I needed it for, and was able to implement some other features as well. Thanks again for the help :)!! – Forrest Vasilinda Dec 20 '22 at 16:12