Assigning Values to a Variable using Loop


  • Hi,

    I have created variables using a loop based on a list of computers (looping through them).

    While creating this variables i also create another set of variables in the same loop.

    see below sample code:

    Get-Variable -Name "SRV_*" | Remove-Variable
    Get-Variable -Name "Info_*" | Remove-Variable
    $comps = "server01","server02","server03"
    foreach($comp in $comps)
      New-Variable -Name "SRV_$Comp" -Value $Comp
      New-Variable -Name "Info_$Comp" -Value "GOOD"
      Get-Variable -Name "SRV_$Comp" | % {
    	# How to assign the value of $Info_<>  variables to $SRV_<> variables?

    how to assign the Srv_<> variables the value of Info_<> variables?

    thanks for the help

    Friday, November 02, 2012 5:42 PM

All replies

  • set-variable -name "Info_$comp" -value $_

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 02, 2012 6:00 PM
  • thanks for the reply mjolinor,

    what if i want to invoke a function on $SRV_ and use the value from the $Info_ variables

    for example

    $SRV_Comp has a method called 'Calculate' which takes a value and i want to pass the $info_<> value as the input

    so in general it will look like this


    but am not able to figure how to do this in the loop above.

    Friday, November 02, 2012 6:07 PM
  • Can I ask what it is you're trying to accomplish?

    Your code appears irrational.  Why are you doing this:

    New-Variable -Name "SRV_$Comp" -Value $Comp
    Instead of just using $comp?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 02, 2012 7:28 PM
  • he seems to be wanting to create variable names on the fly, perhas in order to persist the data beyond the loop. It would be much simpler, imho, to use an associative array for this.

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Friday, November 02, 2012 9:04 PM
  • That's what I was thinking, but I'd like to get some idea of what he's trying to accomplish so I can give an example that illustrates doing that.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 02, 2012 9:31 PM
  • thanks Al Dubnar and mjolinor, below is what i am trying to achieve:

    i am creating a chart for each of the computer object and assign a newly created variable within the loop to another variable within the same loop, below is code i have so far, but not able to assign that value:

    foreach($comp in $comps)
    	New-Variable -Name "Chart_$comp"
    	New-Variable -Name "DataSeries_$comp"
    	New-Variable -Name "Title_$comp"
    	Get-Variable -Name "Chart_$Comp" | % {
    		$_.Value = New-Object VisiFire.Charts.Chart 		
    	Get-Variable -Name "Title_$Comp" | % {		
    		$_.Value = New-Object VisiFire.Charts.Title				
    		($($_.Value)).Text = $comp				
    	Get-Variable -Name "DataSeries_$Comp" | % {		
    		$_.Value = New-Object VisiFire.Charts.DataSeries 				
    		($($_.Value)).RenderAs = [Visifire.Charts.RenderAs]::Line					
    # I need to now add $title_<comp> variable to the children of the $Chart_<comp> object like this
    # $Chart_Comp.Children.Add($Title_<Comp>)

    Monday, November 05, 2012 12:55 PM
  • The code you show above only creates these variables, but it does not use them. The only reason you would create a variable in a loop that is not otherwise referenced in that loop would be to use it after the loop had ended. Can you show some of the statements that will appear later on in the script (after the end of the loop) that make references to these variables (whose names cannot be known without knowing the names of the computers processed)?

    Our question is: why are you creating all of those variables with computer names embedded in them when it would be simpler to use an array of some type, probably an associative array?

    If that is where you are having your problem, then please try once more to explain what it is you are trying to accomplish. When you have tried to do this previously, you have actually only tried to explain *how* you are trying to accomplish something. If we knew *what* it was you were trying to accomplish, there is a chance that we could suggest *ways* of accomplishing it that would get you out of the problems that seem to be associated with *how* you are trying to do it.

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, November 05, 2012 7:56 PM
  • Hi Al Dunbar,

    thanks for your reply, i will try to explain what i am trying to do, if there is a better way to achieve this, please let me know

    1.i am coming up a GUI based out of XAML.

    2. It has a text box, where someone can input a bunch of computer names

    3. there is a button which if clicked would have to do the following

          i. Create a chart for each computer listed (hence the loop which i posted above.)

          ii. Chart object needs additional objects assigned to it , like 'DataSeries' and 'ChartTitle' etc.,

          iii. I would have to create 3 objects for each computer, 1) Chart Object 2)Dataseries object 3) Title Object, after this assign Dataseries and title object to the Chart Object using the method $chart.Children.add($dataseries) and $chart.children.add($title)

          iv. After this setup, i have a timer which gets kicked off and going forward, the timer has an update block which will get data for all the computers listed and update their respective charts.

    i hope this explains it better,

    thanks again for your help.

    Wednesday, November 07, 2012 4:12 PM
  • again, when you write "I would have to create 3 objects for each computer" you are explaining HOW you want to do something, NOT what it is you actually want to accomplish by using that method.

    But, be that as it may, we have already suggested that you use an associative array to hold those "objects", instead of manufacturing variable names that contain the name of the computer it represents. Here is my first attempt at modifying your code to work this way:

    Get-Variable -Name "SRV_*" | Remove-Variable
    Get-Variable -Name "Info_*" | Remove-Variable
    $comps = "server01","server02","server03"
    $Info = @{}
    foreach($comp in $comps)
    #  New-Variable -Name "SRV_$Comp" -Value $Comp
    #  New-Variable -Name "Info_$Comp" -Value "GOOD"
        $info[$comp] = "GOOD"
    #  Get-Variable -Name "SRV_$Comp" | % {
        $info | % {I really have no idea what it is you want to do with this information}
    	# How to assign the value of $Info_<>  variables to $SRV_<> variables?

    I think you need to step away from your current application and acquaint yourself with hash tables. start with this command:

        help about_hash_tables

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, November 07, 2012 6:20 PM
  • sorry about not being able to explain properly, hopefully the below code will explain it better:

    		#Create the chart object
    		$chart1 = New-Object VisiFire.Charts.Chart			
    		#Create the dataseries object
    		$ds = New-Object Visifire.Charts.DataSeries	
    		$ds.RenderAs = [Visifire.Charts.RenderAs]::Line
    		# Adding dataseries object to chart		
    		#Creating data points to plot the graph
    		 $dp= New-Object Visifire.Charts.DataPoint  
    		 $dp.YValue = 20		 
    		 $dp1= New-Object Visifire.Charts.DataPoint	 
    		 $dp1.YValue = 40		 
    		 #Adding the data points to Dataseries object
    		# Adding the chart to the GUI

    above code is what is required to generate a chart for 1 computer, i would like to do this for multiple computers.

    i have used hashtables before but not sure how to apply this in this scenario.

    if it is possible, please let me know how to convert that for multiple computers using hashtables

    Wednesday, November 07, 2012 6:47 PM
  • since I do not have visifire installed, it is kind of hard to say. I will assume that it is used just to create presentation graphics. I do not understand where the data to be charted comes from. It seems to me that the above example simply creates a chart to display some lines of some kind. Could you possibly post an image of what this chart looks like, and how it would differ if run on different computers.

    If the data you want to chart comes from other computers, you will need to break your script down into a couple of functional parts, including at least:

    • the code that gets the data from another computer. if this is information that can be determined by cmdlets using the -computername parameter, it should be fairly simple. If not, you will need to use remoting, and remoting will need to be enabled on the computers.
    • the code that creates a chart from the data for each computer. There should be something in the chart indicating the name of the computer.
    • code that does the above in a loop that processes each computer.
    • code that manages the individual charts of the individual computers and arranges them to be displayed.

    once modularized to at least that extent, you can get the individual parts working, then eventually put them together to produce the result you want.

    as far as hashtables go, they are a way to store multiple "values" or "objects" in a single data structure using a key (probably computer name in your scenario) to access the individual objects. I strongly suggest (once more) that you familiarize yourself with how they are used.

    Alternately, you could use an array here - it kind of depends on what you need to do with the data.

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, November 07, 2012 11:28 PM
  • imagine a windows gui, with 10 perfmon console in them, with each console graphing a particular computer, thats the intention of the tool.

    1. i have a seperate function, which given a counter name and computer name will collect the counter information using 'get-counter' cmdlet and will return a PS object as an output wit the counter/computername/value/timestamp/comment etc as the psobject members.

    2. Chart is created on the machine where the tool is run from , it is a simple line graph, see below image:

    i will try this with hash table approach and see how i get along with it, will keep you posted.

    thanks for your response.

    Sample Chart

    Thursday, November 08, 2012 3:42 PM
  • OK, now that is a better explanation of what it is you want to do than how you think it might best be accomplished.

    So, the windows GUI will be an HTA, based on Winforms, or what? would the GUI have input fields for the computername and counter for each of the ten consoles, or would these be specified by the command that start the consolt?

    Adding in the GUI factor makes this even a bit more complex, and also unlikely for you to get much help towards building the whole thing. But if you come back with the parts you are having trouble with...

    Your sample chart doesn't get past our firewall, so I'll have a look later from home.

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Thursday, November 08, 2012 11:58 PM