[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.
This topic is 4 years 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
AMIGA1200
Posts: 2
Last visit: Tue Sep 17, 2019 3:06 am

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

Post by AMIGA1200 »

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. $ADcache = Get-ADUser -Filter * -Properties SamAccountName, UserPrincipalName, Name, displayname | Select-Object SamAccountName, UserPrincipalName, Name, displayname
  3.  
  4. #I write-host it to be sure it's full (And it is)
  5. Write-host $ADcache
  6.  
  7. #Then i get a value from my textbox (Exemple connor entered with keyboard who is in my AD)
  8. [string]$script:getMembers = $gui_searchUser01.Text
  9.  
  10. #I write-host both to be sure the variables are feed (And they are both with connor)
  11. Write-host $gui_searchUser01.Text
  12. Write-host $script:getMembers
  13.  
  14. #So i try to do my filtering on the AD cache (It works well in powershell ISE with same code)
  15. $ADusers = $ADcache | select-object displayname | Where-Object { $_.displayname -Like "*$getMembers*" } | sort-object displayname
  16.  
  17. #or
  18.  
  19. $ADusers = $ADcache | select-object displayname | Where-Object displayname -like "*$getMembers*" | sort-object displayname
  20.  
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: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

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

Post by Alexander Riedel »

"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
Last visit: Tue Sep 17, 2019 3:06 am

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

Post by AMIGA1200 »

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)
This topic is 4 years 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