5 Replies Latest reply on Jan 4, 2013 7:32 AM by ironman13

    pb script powershell

    ironman13 Novice

      I can not see a reason in my script outputs are duplicates

       

      if someone has a better vision than me

       

      here is my script

       

      $colItems = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
        ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
          ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
            ForEach ($IP in $VM.Guest.IPAddress) {
              ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
               ForEach ($NOTES in $VM.Notes) {
                   ForEach ($MEMORY in $VM.MemoryMB) {
                      ForEach ($ProvisionedSpaceGB in $VM.ProvisionedSpaceGB) {
                           ForEach ($CPU in $VM.NumCpu) {
                              ForEach ($HOSTS in $VM.VMhost) {
                                  "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                                    @{N="MEMORY";E={$MEMORY}},
                                     @{N="CPU";E={$CPU}},
                                   @{N="Notes";E={$NOTES}},
                                     @{N="HOST";E={$HOSTS}},
                                     @{N="Datacenter";E={$Datacenter.name}},
                                     @{N="Cluster";E={$Cluster.Name}},
          #      @{N="IP";E={$IP}},
          #      @{N="Hard Disk";E={$HardDisk.Name}},
                                    @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
                                    @{N="Espace Disques";E={$ProvisionedSpaceGB}}
            #   @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
             #  @{N="VMDKpath";E={$HardDisk.FileName}},
                #@{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $HardDisk.filename.replace(".","-flat.")}).size/1GB}}
      }
          }
              }
                  }
                      }
                          }
                              }
                                  }
                                      }
                                          }

        • 1. Re: pb script powershell
          Grzesiekk Expert

          you got looped yourself i guess , i can rewrite this for you if you want

          • 2. Re: pb script powershell
            Grzesiekk Expert

            im leaving office now , but this should give you an idea i guess what was wrong:

            ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
              ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
                ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
                $IP=$VM.Guest.IPAddress
                $vmnotes=$VM.Notes
                $vmmemory=$VM.MemoryMB
                $vmnumcpu=$VM.NumCpu
                $vmhost=$VM.VMhost.name
                                        "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                                          @{N="MEMORY";E={$vmMEMORY}},
                                           @{N="CPU";E={$vmnumCPU}},
                                         @{N="Notes";E={$vmNOTES}},
                                           @{N="HOST";E={$vmhostHOST}},
                                           @{N="Datacenter";E={$Datacenter.name}},
                                           @{N="Cluster";E={$Cluster.Name}}
                                    }
                                        }
                                            }

            i've skipped some properties as i did not know what exactly you wanted to achieve, but this way you will be not over looping yourself.

            greg

            • 3. Re: pb script powershell
              ironman13 Novice

              I have less than perfect duplicates but I still, I wish the info back completes a VM (ip, his memory, his disks, or datastore is esx host and on what and cluster)

              • 4. Re: pb script powershell
                ironman13 Novice

                in fact it is ok, duplicates are on VM fault tolrance