Automation

 View Only
  • 1.  Create portgroups with foreach hash array loop

    Posted Jun 26, 2011 11:06 PM

    I am an absolute beginner in PowerShell and PowerCLI.

    I would like to create a virtual switch with several portgroups and vlanids and then assign a nic

    This code works for me:

    $esxihost="esx01"

    Connect-VIServer -Server $esxihost

    $vSwitch =  New-VirtualSwitch -VMHost $esxihost -Name vSwitch3

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name adm -VLanId 1012

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name man -VLanId 1020

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name test1 -VLanId 1021

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name test2 -VLanId 1022

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name test3 -VLanId 2000

    Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic3 -Confirm:$false

    But I think something like this would be much more elegant:

    $esxihost="esx01"

    Connect-VIServer -Server $esxihost

    $vSwitch =  New-VirtualSwitch -VMHost $esxihost -Name vSwitch3

    $vlans = @{`
        "adm" = 1012; `
        "man" = 1020; `
        "test1" = 1021; `

        "test2" = 1022; `

        "test3" = 2000
    }

    foreach (items in $vlans) {

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $vlans.Key -VLanId $vlans.Value

    }

    Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic3 -Confirm:$false

    But I can't get the foreach loop to work. I am unsure about the three things marked with bold text in the foreach loop. Can someone give me a hint..?

    Thanks.

    Anders



  • 2.  RE: Create portgroups with foreach hash array loop

    Posted Jun 27, 2011 12:54 AM

    foreach (items in $vlans) {

    Should be

    ForEach ($items in $vlans) {

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $vlans.Key -VLanId $vlans.Value

    Should be

    $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $items.Key -VLanId $items.Value



  • 3.  RE: Create portgroups with foreach hash array loop
    Best Answer

    Posted Jun 27, 2011 05:10 AM

    You store the input data in a hash table.

    You can't use the ForEach like you would for a regular array, but you can use the GetEnumerator method.

    Something like this

    $esxihost="esx01"
    
    Connect-VIServer
    -Server $esxihost
    $vSwitch
    =  New-VirtualSwitch -VMHost $esxihost -Name vSwitch3
    $vlans
    = @{     "adm" = 1012;     "man" = 1020;     "test1" = 1021;     "test2" = 1022;     "test3" = 2000} $vlans.GetEnumerator() | %{     $vPortGroup =  New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $_.Key -VLanId $_.Value } Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic3 -Confirm:$false


  • 4.  RE: Create portgroups with foreach hash array loop

    Posted Jun 27, 2011 08:37 AM

    The GetEnumerator does the job - thanks - it works. I think I had misunderstood the hash array.

    What would be your approch for a task like this?

    Hash array
    regular array
    or something esle

    Everyone is welcome to comment on this. I would love to see how optimized the code can be.



  • 5.  RE: Create portgroups with foreach hash array loop

    Posted Jun 27, 2011 08:59 AM

    For this task you could have used a regular 2-dimensional array as well.

    But then you would have to use the numeric indexing on each row.

    The hash array looks and feels more elegant, but that is my personal opinion of course.

    Most of the time PowerShell offers several methods to solve a problem, use what you're most comfortable with :smileyhappy: