Windows Vista Forums

How To: Create Dynamic Variable Names in Scripts

  1. #1


    Brandon Shell Guest

    How To: Create Dynamic Variable Names in Scripts

    Goal:
    -----
    I want to create x number of groups. These groups will have a list of
    servers. I split these servers in groups by name. What I would like is for
    this function to be dynamic. I hope the example will make my goal clear.

    Example:
    ---------
    This is a very simple example... I am looking at creating 40+ groups... it
    gets to be ALOT of duplicate code.

    # This is what works now
    Function Create-Groups {
    param([system.array]$servers)
    # this is where I would like to be more dynamic
    $list1 = @()
    $list2 = @()
    $list3 = @()
    $list4 = @()
    $list5 = @()
    foreach($server in $servers) {
    if ($Server -match ".*ver1.$") {$list1 += $server}
    elseif($Server -match ".*ver2.$") {$list2 += $server}
    elseif($Server -match ".*ver3.$") {$list3 += $server}
    elseif($Server -match ".*ver4.$") {$list4 += $server}
    elseif($Server -match ".*ver5.$") {$list5 += $server}
    }
    # This is where I would like to be more dynamic
    $global:group1 = $list1
    $global:group2 = $list2
    $global:group3 = $list3
    $global:group4 = $list4
    $global:group5 = $list5
    }

    Problem:
    ---------
    Now if I wanted to make 10 groups instead of 5... I have create 5 more
    arrays and global variables. I would like to do something like:

    # this of course fails... is there a way I can do this?
    $i = 1
    while($i -lt 21) {
    $list$i = @()
    ++$i
    }



      My System SpecsSystem Spec

  2.   


  3. #2


    =?Utf-8?B?U3VuZyBNIEtpbQ==?= Guest

    RE: How To: Create Dynamic Variable Names in Scripts

    "Brandon Shell" wrote:
    > # this of course fails... is there a way I can do this?
    > $i = 1
    > while($i -lt 21) {
    > $list$i = @()
    > ++$i
    > }

    You can use "Invoke-Expression" cmdlet to create variables dynamially

    foreach ($i in (1..21)) {
    Invoke-Expression ('$`{list' + $i + '`} = @()')
    }

    Have fun~
    --
    Sung M Kim

    Please don''t bother me with spam...


      My System SpecsSystem Spec

  4. #3


    Brandon Shell Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    Thanks
    "Sung M Kim" <SungMKim@discussions.microsoft.com> wrote in message
    news:0A7D67F1-A612-46C3-A7CC-A5075012EFB0@microsoft.com...
    > "Brandon Shell" wrote:
    >> # this of course fails... is there a way I can do this?
    >> $i = 1
    >> while($i -lt 21) {
    >> $list$i = @()
    >> ++$i
    >> }

    > You can use "Invoke-Expression" cmdlet to create variables dynamially
    >
    > foreach ($i in (1..21)) {
    > Invoke-Expression ('$`{list' + $i + '`} = @()')
    > }
    >
    > Have fun~
    > --
    > Sung M Kim
    >
    > Please don''t bother me with spam...
    >




      My System SpecsSystem Spec

  5. #4


    =?Utf-8?B?Um9tYW4gS3V6bWlu?= Guest

    RE: How To: Create Dynamic Variable Names in Scripts

    Yes, it is possible, but I would double think before using this approach. I
    am just curious, why not to use, say, global hash tables with keys like
    'ver1.$', 'ver2.$', etc. for servers, lists etc.?

    --
    Thanks,
    Roman


      My System SpecsSystem Spec

  6. #5


    Brandon Shell Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    Because I want to pipe the list to functions.

    Example:
    group1 | Check-Something | fix-problem

    "Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message
    news:EDCBA044-6C0B-4361-96CC-37961F4B6589@microsoft.com...
    > Yes, it is possible, but I would double think before using this approach.
    > I
    > am just curious, why not to use, say, global hash tables with keys like
    > 'ver1.$', 'ver2.$', etc. for servers, lists etc.?
    >
    > --
    > Thanks,
    > Roman
    >




      My System SpecsSystem Spec

  7. #6


    =?Utf-8?B?Um9tYW4gS3V6bWlu?= Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    I definitely do not criticize.

    Suppose your lists are in a hash $groups with keys like 1, 2, ‘ver5.$’ and
    etc. For any <key>, i.e. group, you can do this:

    $group[<key>] | Check-Something | fix-problem

    No problem. For example if you want to process 3 groups 1, 2, ‘ver5.$’:

    1, 2, ‘ver5.$’ | % {$group[$_]} | Check-Something | fix-problem

    Suppose now a list of groups for a particular task is defined as an array
    $ToDo; it is as easy as before:

    $ToDo | % {$group[$_]} | Check-Something | fix-problem

    Do you know how to do the same with your approach? If you know the answer
    and like it then this approach is good for you.

    --
    Thanks,
    Roman


      My System SpecsSystem Spec

  8. #7


    Adam Milazzo Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    Brandon Shell wrote:
    > Goal:
    > -----
    > I want to create x number of groups. These groups will have a list of
    > servers. I split these servers in groups by name. What I would like is for
    > this function to be dynamic. I hope the example will make my goal clear.


    I wouldn't use invoke-expression. It's somewhat ugly.

    I'd use the cmdlets designed for the purpose: new-variable,
    set-variable, and get-variable (nv, sv, and gv).

    nv "var.$foo"
    sv "var.$foo" "value $i"
    gv "var.$foo"

    In place of string interpolation, you can use subexpressions:

    nv ("var." + $nextVar)

    You can also specify the value at creation time:

    nv name value

    -- Adam

      My System SpecsSystem Spec

  9. #8


    Jouko Kynsijrvi Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    Sung M Kim wrote:
    > "Brandon Shell" wrote:
    >> # this of course fails... is there a way I can do this?
    >> $i = 1
    >> while($i -lt 21) {
    >> $list$i = @()
    >> ++$i
    >> }

    > You can use "Invoke-Expression" cmdlet to create variables dynamially
    >
    > foreach ($i in (1..21)) {
    > Invoke-Expression ('$`{list' + $i + '`} = @()')
    > }


    Another, simpler, way is to use the new-variable cmdlet:

    foreach ($i in (1..21)) {
    new-variable "list$i" @()
    }



      My System SpecsSystem Spec

  10. #9


    Brandon Shell Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    I will take a look. You way does have some potential. My only concern is
    that most of scripts are wrappers for people... that... hmmm... are less
    than brilliant.

    Im trying to keep the input as little and brainless as possible.

    "Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message
    news:165DEFC0-FACA-4258-A531-A44ADE1FD1E6@microsoft.com...
    >I definitely do not criticize.
    >
    > Suppose your lists are in a hash $groups with keys like 1, 2, 'ver5.$' and
    > etc. For any <key>, i.e. group, you can do this:
    >
    > $group[<key>] | Check-Something | fix-problem
    >
    > No problem. For example if you want to process 3 groups 1, 2, 'ver5.$':
    >
    > 1, 2, 'ver5.$' | % {$group[$_]} | Check-Something | fix-problem
    >
    > Suppose now a list of groups for a particular task is defined as an array
    > $ToDo; it is as easy as before:
    >
    > $ToDo | % {$group[$_]} | Check-Something | fix-problem
    >
    > Do you know how to do the same with your approach? If you know the answer
    > and like it then this approach is good for you.
    >
    > --
    > Thanks,
    > Roman
    >




      My System SpecsSystem Spec

  11. #10


    =?Utf-8?B?U3VuZyBNIEtpbQ==?= Guest

    Re: How To: Create Dynamic Variable Names in Scripts

    "Jouko Kynsijärvi" wrote:
    > Another, simpler, way is to use the new-variable cmdlet:
    >
    > foreach ($i in (1..21)) {
    > new-variable "list$i" @()
    > }


    That's brilliant
    This is the second time that I overlooked "*-Variable" cmdlets... as was the
    case when I needed to create a constant variable ;p
    --
    Sung M Kim


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast

How To: Create Dynamic Variable Names in Scripts
Similar Threads
Thread Forum
Passing dynamic number of parameters to a function as a variable PowerShell
PATHEXT environment variable and PS1 scripts PowerShell
Creating objects with variable names in functions PowerShell
Running Scripts with Spaces in Names PowerShell
Suggestion: Consider shorter standard names for Alias.Function, and Variable PSDrives PowerShell