Changing variable name on the fly

Ask your 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.
This topic is 5 years and 1 month 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
User avatar
kellydyjur
Posts: 3
Last visit: Mon Nov 08, 2021 4:42 pm

Changing variable name on the fly

Post by kellydyjur »

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" :
  1. $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:
  1. $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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

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
Last visit: Mon Nov 08, 2021 4:42 pm

Re: Changing variable name on the fly

Post by kellydyjur »

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
  1. cityALabel.text = 'foo'
  2. cityBLabel.text = 'bar'
  3. cityCLabel.text = 'splism'
  4. cityDLabel.text = 'splasm'
Or... What I want to do
  1. Function changelabel ($city, $text){
  2.    ($city)Label.text = $text
  3. }
  4. 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

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
Last visit: Mon Nov 08, 2021 4:42 pm

Re: Changing variable name on the fly

Post by kellydyjur »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

kellydyjur wrote: Fri Feb 08, 2019 3: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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Changing variable name on the fly

Post by jvierra »

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.
This topic is 5 years and 1 month 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