Automation

 View Only
  • 1.  Help with Harddisks and the 'switch' statement

    Posted Mar 23, 2011 08:28 AM

    All

    I'm wondering if you could help please ? this is probably more to do with 'powershell' than powercli but here goes ..... What i'm trying to acheive is the ability to create a function that goes through all of my VMs - counts the number of hard disks per VM - goes through EACH harddisk and gives me the capacity of each one. Then outputs it to a csv file (which iknow how to do). The segment below pretty much does what I want, however I was wondering if there was a way to reduce the amount of code ?!?!? I tried using a foreach statement but got stuck (PS - the maximum number of harddisks our VMs can have is 5)

    I'm just used a switch statement as thats what I 'thought' answered the problem. However if there is a way that i could get the same result without so much 'code' that would be ideal.

    switch ($vm.harddisks.length){

         ($vm.harddisks.length -eq 1){

         ($obj | Add-member Noteproperty "Hard Disk 1" ($vm.harddisks[0].capacitykb))

         }

         ($vm.harddisks.length -eq 2){

         ($obj | Add-member Noteproperty "Hard Disk 1"  ($vm.harddisks[0].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 2"  ($vm.harddisks[1].capacitykb))

         }

          ($vm.harddisks.length -eq 3){

         ($obj | Add-member Noteproperty "Hard Disk 1"  ($vm.harddisks[0].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 2"  ($vm.harddisks[1].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 3"  ($vm.harddisks[2].capacitykb))

         }

         ($vm.harddisks.length -eq 4){

         ($obj | Add-member Noteproperty "Hard Disk 1"  ($vm.harddisks[0].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 2"  ($vm.harddisks[1].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 3"  ($vm.harddisks[2].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 4"  ($vm.harddisks[3].capacitykb))

         }

         ($vm.harddisks.length -eq 5){

         ($obj | Add-member Noteproperty "Hard Disk 1"  ($vm.harddisks[0].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 2"  ($vm.harddisks[1].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 3"  ($vm.harddisks[2].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 4"  ($vm.harddisks[3].capacitykb))

         ($obj | Add-member Noteproperty "Hard Disk 5"  ($vm.harddisks[4].capacitykb))

         }

    write-host $obj

    }

    Again many thanks in advance

    Munster99



  • 2.  RE: Help with Harddisks and the 'switch' statement
    Best Answer

    Posted Mar 23, 2011 08:32 AM

    Try something like this

    $i = 1
    $vm.Harddisks | %{
        $obj | Add-member Noteproperty ("Hard Disk " + $i) ($vm.harddisks[$i -1].capacitykb)
        $i++
    }
    write-host $obj



  • 3.  RE: Help with Harddisks and the 'switch' statement

    Posted Mar 25, 2011 11:43 AM

    LucD

    You are a 'scholar' and a 'gent' that worked perfectly and was just what I wanted. :smileylaugh:

    (Its so handy I can use it for a variety of ways.)

    Many thanks

    Munster