Unable to pass variables into Get-ADUser Filter

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
axevans1
Posts: 4
Last visit: Mon Aug 22, 2022 8:12 am

Unable to pass variables into Get-ADUser Filter

Post by axevans1 »

Product: PowerShell Studio 2022 (64 Bit)
Build: v5.8.209
OS: Windows 10 Pro (64 Bit)
Build: v10.0.19044.0

I have created a GUI that has a text box for user input, a button and finally a label to print out the information.

Here is what my code looks like for my button.

$Check_Username_Button_Click = {
#TODO: Place custom script here
$label1.Text = Get-ADUser -Server "ENTERSERVERNAME" -Filter "Name -like '$check_username_text_box*'" | Out-String
}

For some reason the variable does not properly pass through when the user clicks the button. I can get the information to pass through if i remove the variable and enter a name and i can even get the code to work in normal powershell but for some reason it does not work in PS 22 and provides no error in the output.

the idea is to have the user input a name into a text field, they click submit and it returns a list of users that match the user input.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to pass variables into Get-ADUser Filter

Post by jvierra »

You can use the debugger to inspect the filter or just print it.
  1. $filter = "Name -like '$check_username_text_box*'"
  2. Write-Host $filter
  3. $label1.Text = Get-ADUser -Server "ENTERSERVERNAME" -Filter $filter | Out-String
axevans1
Posts: 4
Last visit: Mon Aug 22, 2022 8:12 am

Re: Unable to pass variables into Get-ADUser Filter

Post by axevans1 »

Thank you for the reply!, I didnt think of having it write to the host like that.

I now get this as a return Name -like 'System.Windows.Forms.TextBox, Text: TEST*', i cant tell if the code is trying to read the System.Windows.Forms.text and then the actual text.
axevans1
Posts: 4
Last visit: Mon Aug 22, 2022 8:12 am

Re: Unable to pass variables into Get-ADUser Filter

Post by axevans1 »

I figured it out, i spaced this part out.

I added an extra variable named $stored_username = $check_username_text_box.Text on the button click.

$Check_Username_Button_Click = {
#TODO: Place custom script here
$stored_username = $check_username_text_box.Text;
$filter = "Name -like '$stored_username*'";
#Write-Host $filter;
$textbox1.Text = Get-ADUser -Server "SERVER" -Filter $filter | Sort-Object -Property Name | Format-Table -AutoSize Name | Out-String
}
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked