[SOLVED] I can't use my Textbox Variable in my commands.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
AMIGA1200
Posts: 2
Joined: Mon Aug 19, 2019 12:43 am

[SOLVED] I can't use my Textbox Variable in my commands.

Post by AMIGA1200 » Wed Aug 21, 2019 4:24 am

Hi,

I'm having always the same problem in Powershell Studio and i have tried a lot of things and i can't resolve this problem. I have check a lot of articles on google and nothing works :( You are my last hope :)

My problem is simple and happen in many exemples, i can't use my variables in commands like in Powershell ISE ! And i'm sure it's an easy mistake from me but i do not understand.
  1. #i'm backing up my AD in a variable.
  2.  
  3. $ADcache = Get-ADUser -Filter * -Properties SamAccountName, UserPrincipalName, Name, displayname | Select-Object SamAccountName, UserPrincipalName, Name, displayname
  4.  
  5.  
  6.  
  7. #I write-host it to be sure it's full (And it is)
  8.  
  9. Write-host $ADcache
  10.  
  11.  
  12.  
  13. #Then i get a value from my textbox (Exemple connor entered with keyboard who is in my AD)
  14.  
  15. [string]$script:getMembers = $gui_searchUser01.Text
  16.  
  17.  
  18.  
  19. #I write-host both to be sure the variables are feed (And they are both with connor)
  20.  
  21. Write-host $gui_searchUser01.Text
  22.  
  23. Write-host $script:getMembers
  24.  
  25.  
  26.  
  27. #So i try to do my filtering on the AD cache (It works well in powershell ISE with same code)
  28.  
  29. $ADusers = $ADcache | select-object displayname | Where-Object { $_.displayname -Like "*$getMembers*" } | sort-object displayname
  30.  
  31.  
  32.  
  33. #or
  34.  
  35.  
  36.  
  37. $ADusers = $ADcache | select-object displayname | Where-Object displayname -like "*$getMembers*" | sort-object displayname
  38.  
  39.  
On powershell ISE i got this return :

$ADusers

displayname
-----------
John CONNOR
John CONNOR2

On powershell Studio i got nothing. $ADusers is empty and i think it's because my $getMembers is not transformed with her value "connor" in the command. But i maybe made a mistake about that. Maybe a full part of the command is not interpreted but i don't see why.

Can you help me ?


Thanks
Last edited by AMIGA1200 on Wed Aug 21, 2019 5:41 am, edited 1 time in total.

User avatar
Alexander Riedel
Posts: 7118
Joined: Tue May 29, 2007 4:43 pm

Re: I can't use my Textbox Variable in my commands.

Post by Alexander Riedel » Wed Aug 21, 2019 5:35 am

"Help me Obi Wan Kenobi ..."
You didn't post all your code, so its a bit of a guess, but these cases are *usually* caused by using variables out of scope.
It is interesting to note that you use $script:getMembers everywhere BUT in your query, where you use the default (local scope) $getMembers
If you use a scope modifier for a variable, do it ALWAYS, in each instance. Or do it never so you always use the local scope.

Using the ISE to judge anything is usually ... well, a difficult proposition at least.

Your local $getMembers my have the desired content in the ISE, but you never know if it is from this instance running a script or from 10 minutes ago toying with it on the command line.
The ISE has what is called "Runspace contamination" because it never resets the runspace (unless YOU do that), whereas PowerShell Studio and PrimalScript create a new runspace for each time you run a script.
We do that so that blaring omissions like uninitialized variables or out-of-scope use become immediately apparent and you don't suffer from "But it works on my machine" syndrome.

Hope that helps,

Alex
Alexander Riedel
SAPIEN Technologies, Inc.

AMIGA1200
Posts: 2
Joined: Mon Aug 19, 2019 12:43 am

Re: I can't use my Textbox Variable in my commands.

Post by AMIGA1200 » Wed Aug 21, 2019 5:41 am

Hi,

I'm a bit confused. I let my post because it can help some other persons with the same problems :)

I just forget to set the global scope on my ADcache variable.

Shame on me i know :(

Thanks a lot Alexander for your time and sorry again !
I'm testing PowerShell Studio 2019 because i want my boss to get a licence :) (+ support)

Locked