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.
Unable to pass variables into Get-ADUser Filter
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.
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.
Re: Unable to pass variables into Get-ADUser Filter
You can use the debugger to inspect the filter or just print it.
- $filter = "Name -like '$check_username_text_box*'"
- Write-Host $filter
- $label1.Text = Get-ADUser -Server "ENTERSERVERNAME" -Filter $filter | Out-String
Re: Unable to pass variables into Get-ADUser Filter
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.
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.
Re: Unable to pass variables into Get-ADUser Filter
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
}
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
}