Changing variable name on the fly

Ask your Windows 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.
Locked
User avatar
kellydyjur
Posts: 3
Joined: Tue Aug 18, 2015 8:44 am

Changing variable name on the fly

Post by kellydyjur » Fri Feb 08, 2019 8:35 am

A little back story to what I am try to accomplish here: I want a dashboard that will display the status and health of various aspects of our domain controllers. For example the status of the ntds service etc. I am running some code and then placing the status on a label.text.

My issue: 98% of the code is all the same. So for 15 DCs, the only thing that changes in the functions and controls are the first parts of the variable names, in the following example the "slaveLake" part will be changed to "edmonton" :

Code: Select all

$slaveLake.serviceStatusNtds = QueryService $slaveLake.name $slaveLake.serviceNtds
Furthermore, the first part of the variable that contains the location is the only part that changes when I go to change the color of the text:

Code: Select all

$slaveLakeLabelServiceNtds.ForeColor = 'Red'
My question is that is there any way that I can somehow change the first part of the variable with another location name, and therefore make it possible to pull a lot of the code into functions instead of re-writing 98% of the code?

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 12:34 pm

I am sorry but without an example it is not possible to understand your question.

You cannot change the name of a variable but you can change its contents. Just assign a new content.

$var = 'xxxxx'
$var = 'new content' # change variable contents.

User avatar
kellydyjur
Posts: 3
Joined: Tue Aug 18, 2015 8:44 am

Re: Changing variable name on the fly

Post by kellydyjur » Fri Feb 08, 2019 4:35 pm

Is it possible to use a variable as a part of another variable's name?

So if I want to change the text of a label, I can write the name out

Code: Select all

cityALabel.text = 'foo'
cityBLabel.text = 'bar'
cityCLabel.text = 'splism'
cityDLabel.text = 'splasm'
Or... What I want to do

Code: Select all

Function changelabel ($city, $text){
($city)Label.text = $text
}
changelabel $cityC, 'snafu'
So with form controls name in a way where the only difference between them is the <city> part at the front, I can stuff a lot of code into functions. I'm new to this and I'm sorry if this makes no sense. I remember doing something like this before in vb.net so I was wondering if PowerShell has similiar functionality.

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 4:45 pm

I think you would do best by learning PowerShell first. You cannot guess at this based on things you have seen.

THe following makes no sense. What is it supposed to do?

Code: Select all

Function changelabel ($city, $text){
($city)Label.text = $text
 }
 changelabel $cityC, 'snafu' 
To change a labels text with a variable just assign the variable to the label. There is no need for a function.

$label.Text = $variable

Can you attach a simple form showing what you are trying to do?

User avatar
kellydyjur
Posts: 3
Joined: Tue Aug 18, 2015 8:44 am

Re: Changing variable name on the fly

Post by kellydyjur » Fri Feb 08, 2019 4:57 pm

I'm thinking the more I respond the more lectures I'm going to get on how I should learn more so I'll just go off and do that instead of continuing here. Thanks.

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 5:13 pm

If you are trying to get an object by name you can do this:

At a PowerShell prompt (not ISE) run the following lines one at a time and inspect the results.

Code: Select all

Add-Type -AssemblyName System.Windows.Forms
$form = [System.Windows.Forms]::New()
$form.Controls['city']
$c = [System.Windows.Forms.Label]::New()
$form.Controls.Add($c)
$c.Name = 'city'
# get the control by name
$form.Controls['city']
To assign a label text do this:

$form.Controls['city'].Text = $text

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 5:18 pm

Warning! Avoid the new coders worst mistake of wrapping everything in small functions. It does not work well as a design strategy and is especially problematic in forms.

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 5:21 pm

kellydyjur wrote:
Fri Feb 08, 2019 4:57 pm
I'm thinking the more I respond the more lectures I'm going to get on how I should learn more so I'll just go off and do that instead of continuing here. Thanks.
The problem is that you are asking a question that cannot be understood because you are misusing terms. A simple form with an example of what you are trying to do would solve this issue. Of course lack of familiarity with PowerShell and forms will also make asking clear questions more difficult. If we cannot understand what you are trying to ask makes it almost impossible to answer your question.

User avatar
jvierra
Posts: 13502
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Changing variable name on the fly

Post by jvierra » Fri Feb 08, 2019 6:18 pm

I suspect that the big hang-up here is what do you mean by "change a variables name" There is no way to rename a variable in any language. Variables are used as pointers to values or as containers for values. They can be created and removed but there is no way to "rename" a variable.

You can use the following commands to explicitly create a variable and set its scope among other things.

New-Variable
Get-Variable
Set-Variable
Clear-Variable
Remove-Variable


There is no "Rename-Variable" CmdLet in PowerShell.

Get-Variable allows us to get a variable by name:

Typing Get-Variable at a prompt will list all variables.
Typing Get-Variable pshome will return a variable object.

Example:

PS D:\scripts> $var = get-variable pshome
PS D:\scripts> $var.Name
PSHOME
PS D:\scripts> $var.Value
C:\Windows\System32\WindowsPowerShell\v1.0
PS D:\scripts>

The variable "$var" now points to the same object as "$PsHome" points to. If we change the value of $var it will also change the value of $PSHome. We cannot actually change the builtin variables. We can change variables we have created.


PS D:\scripts> $myvar = 3
PS D:\scripts> $var = Get-Variable myvar
PS D:\scripts> $var.Value
3
PS D:\scripts> $var.Value = 99
PS D:\scripts> $myvar
99


We cannot change its name.


PS D:\scripts> $var.Name
myvar
PS D:\scripts> $var.Name = 'test'
'Name' is a ReadOnly property.
At line:1 char:1
+ $var.Name = 'test'
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

PS D:\scripts>


Of course there is no need I n programming to ever rename a variable so I have to ask why you need to or want to do this.

Locked