Page 1 of 1

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

Posted: Wed Aug 21, 2019 4:24 am
by AMIGA1200

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.

Code: Select all

#i'm backing up my AD in a variable.
$ADcache = Get-ADUser -Filter * -Properties SamAccountName, UserPrincipalName, Name, displayname | Select-Object SamAccountName, UserPrincipalName, Name, displayname

#I write-host it to be sure it's full (And it is)
Write-host $ADcache

#Then i get a value from my textbox (Exemple connor entered with keyboard who is in my AD)
[string]$script:getMembers = $gui_searchUser01.Text

#I write-host both to be sure the variables are feed (And they are both with connor)
Write-host $gui_searchUser01.Text
Write-host $script:getMembers

#So i try to do my filtering on the AD cache (It works well in powershell ISE with same code)
$ADusers = $ADcache | select-object displayname | Where-Object { $_.displayname -Like "*$getMembers*" } | sort-object displayname


$ADusers = $ADcache | select-object displayname | Where-Object displayname -like "*$getMembers*" | sort-object displayname
On powershell ISE i got this return :



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 ?


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

Posted: Wed Aug 21, 2019 5:35 am
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,


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

Posted: Wed Aug 21, 2019 5:41 am
by AMIGA1200

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)