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 2 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.
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" :
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?
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.
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.
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']
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.
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.
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.
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.
This topic is 5 years and 2 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.