VMware Cloud Community
tdubb123
Expert
Expert

script getting multiple lines of same vm

for this script, I am trying to get some info on vms. Name, DC, Cluster.

But why am I getting multiple same lines of same VM?

$report = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = get-content "c:\scripts\vms.txt

      foreach ($vm in $vms){

        $info = "" | select Name, Datacenter, Cluster

        $info.Name = $vm.name

        $info.datacenter = $dc.name

        $info.cluster = $cluster.name

        $info.vcenter = $cluster.Uid.Split(":")[0].Split("@")[1]

        $report += $info

      }

    }

}

$report | out-gridview

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Does this also produce duplicates?

Get-VM -Name (Get-Content -Path 'c:\scripts\vms.txt') |

ForEach-Object -Process {

   New-Object -TypeName PSObject -Property ([ordered]@{

   Name = $_.Name

   Datacenter = (Get-Datacenter -VM $_).Name

   Cluster = (Get-Cluster -VM $_).Name

   vCenter = ([uri]($_.ExtensionData.Client.ServiceUrl)).Host

   })

} | Out-GridView


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
tdubb123
Expert
Expert

this works. but I wonder why the 1st script produces duplicates

Reply
0 Kudos
LucD
Leadership
Leadership

To be honest, I would be amazed if that 1st script works at all.

You are reading a number of strings from a .txt file into $vms.

Then in the loop (foreach($vm in $vms)), you are getting the name property ($vm.Name) of each string.

A string doesn't have a Name property.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos