ganapa2000
Hot Shot
Hot Shot

Getting multiple output when gathering information from multiple vCenters

Jump to solution

Hi,

I am trying to get resource information from based on folder from multiple vcenters and I am getting output multiple times.

Please help

Script

$vCenters = @(

"vCenter01"

"vCenter02"

)

Connect-viserver -Server $vCenters

$report = @()

$folder = RND

Foreach ($vc in $global:DefaultVIServers)

{

$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$allocated.'Resource_Info' = 'Allocated'

$allocated.CPU = 1000

$allocated.'Memory(GB)' = 1700

$allocated.'Capacity(GB)' = 18076

$report += $allocated

$report += Get-Folder $folder | Get-VM | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},

  @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},

  @{N='Resource_Info';E={'Used'}},

  @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

  @{N="Memory(GB)";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},

  @{N = "Capacity(GB)"; E = {

   $script:vdiskFree = ($_.Group.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Group.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   "{0:f1}" -f $script:vdiskCapacity

   }

   },

  @{N = "Used(GB)"; E = {"{0:f1}" -f ($script:vdiskCapacity - $script:vdiskFree)}},

  @{N = "FreeSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree))}},

  @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100)}},

  @{N="Total VM's";E={$_.Group.Count}},

  @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},

  @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}

  $available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

  $available.'Resource_Info' = 'Available'

  $available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum

  $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum

  $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum

  $report += $available

  $report

}

Output

pastedImage_0.png

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You have to use the Server parameter to get the data per vCenter.

But how do you want to display the Allocated and Available lines?

Per vCenter or for all vCenters?

This does it per vCenter

$vCenters = @(

"vCenter01"

"vCenter02"

)


Connect-viserver -Server $vCenters


$fullReport = @()

$folder = 'vm'

Foreach ($vc in $global:DefaultVIServers)

{

$report = @()

$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$allocated.'Resource_Info' = 'Allocated'

$allocated.CPU = 1000

$allocated.'Memory(GB)' = 1700

$allocated.'Capacity(GB)' = 18076

$report += $allocated

$report += Get-Folder $folder -Server $vc | Get-VM -Server $vc | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},

  @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},

  @{N='Resource_Info';E={'Used'}},

  @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

  @{N="Memory(GB)";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},

  @{N = "Capacity(GB)"; E = {

   $script:vdiskFree = ($_.Group.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Group.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   "{0:f1}" -f $script:vdiskCapacity

   }

   },

  @{N = "Used(GB)"; E = {"{0:f1}" -f ($script:vdiskCapacity - $script:vdiskFree)}},

  @{N = "FreeSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree))}},

  @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100)}},

  @{N="Total VM's";E={$_.Group.Count}},

  @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},

  @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}

  $available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

  $available.'Resource_Info' = 'Available'

  $available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum

  $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum

  $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum

  $report += $available

  $fullReport += $report

}

$fullReport

This for all vCenters

$vCenters = @(

"vCenter01"

"vCenter02"

)

Connect-viserver -Server $vCenters


$report = @()

$folder = 'vm'

$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$allocated.'Resource_Info' = 'Allocated'

$allocated.CPU = 1000

$allocated.'Memory(GB)' = 1700

$allocated.'Capacity(GB)' = 18076

$report += $allocated

Foreach ($vc in $global:DefaultVIServers)

{

$report += Get-Folder $folder -Server $vc | Get-VM -Server $vc | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},

  @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},

  @{N='Resource_Info';E={'Used'}},

  @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

  @{N="Memory(GB)";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},

  @{N = "Capacity(GB)"; E = {

   $script:vdiskFree = ($_.Group.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Group.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   "{0:f1}" -f $script:vdiskCapacity

   }

   },

  @{N = "Used(GB)"; E = {"{0:f1}" -f ($script:vdiskCapacity - $script:vdiskFree)}},

  @{N = "FreeSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree))}},

  @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100)}},

  @{N="Total VM's";E={$_.Group.Count}},

  @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},

  @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}

}

$available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$available.'Resource_Info' = 'Available'

$available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum

$available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum

$available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum

$report += $available


$report


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

View solution in original post

0 Kudos
3 Replies
Ankushsethi
Enthusiast
Enthusiast

Try to print the $report outside of foreach loop

0 Kudos
LucD
Leadership
Leadership

You have to use the Server parameter to get the data per vCenter.

But how do you want to display the Allocated and Available lines?

Per vCenter or for all vCenters?

This does it per vCenter

$vCenters = @(

"vCenter01"

"vCenter02"

)


Connect-viserver -Server $vCenters


$fullReport = @()

$folder = 'vm'

Foreach ($vc in $global:DefaultVIServers)

{

$report = @()

$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$allocated.'Resource_Info' = 'Allocated'

$allocated.CPU = 1000

$allocated.'Memory(GB)' = 1700

$allocated.'Capacity(GB)' = 18076

$report += $allocated

$report += Get-Folder $folder -Server $vc | Get-VM -Server $vc | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},

  @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},

  @{N='Resource_Info';E={'Used'}},

  @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

  @{N="Memory(GB)";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},

  @{N = "Capacity(GB)"; E = {

   $script:vdiskFree = ($_.Group.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Group.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   "{0:f1}" -f $script:vdiskCapacity

   }

   },

  @{N = "Used(GB)"; E = {"{0:f1}" -f ($script:vdiskCapacity - $script:vdiskFree)}},

  @{N = "FreeSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree))}},

  @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100)}},

  @{N="Total VM's";E={$_.Group.Count}},

  @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},

  @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}

  $available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

  $available.'Resource_Info' = 'Available'

  $available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum

  $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum

  $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum

  $report += $available

  $fullReport += $report

}

$fullReport

This for all vCenters

$vCenters = @(

"vCenter01"

"vCenter02"

)

Connect-viserver -Server $vCenters


$report = @()

$folder = 'vm'

$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$allocated.'Resource_Info' = 'Allocated'

$allocated.CPU = 1000

$allocated.'Memory(GB)' = 1700

$allocated.'Capacity(GB)' = 18076

$report += $allocated

Foreach ($vc in $global:DefaultVIServers)

{

$report += Get-Folder $folder -Server $vc | Get-VM -Server $vc | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},

  @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},

  @{N='Resource_Info';E={'Used'}},

  @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

  @{N="Memory(GB)";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},

  @{N = "Capacity(GB)"; E = {

   $script:vdiskFree = ($_.Group.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Group.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   "{0:f1}" -f $script:vdiskCapacity

   }

   },

  @{N = "Used(GB)"; E = {"{0:f1}" -f ($script:vdiskCapacity - $script:vdiskFree)}},

  @{N = "FreeSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree))}},

  @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100)}},

  @{N="Total VM's";E={$_.Group.Count}},

  @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},

  @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}

}

$available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Used(GB)","FreeSpace(GB)","FreeSpace(%)","Total VM's","PoweredOn VM's","PoweredOff VM's"

$available.'Resource_Info' = 'Available'

$available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum

$available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum

$available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum

$report += $available


$report


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

That worked perfectly....I missed that server parameter. Everyday is a new learning LucD.

Thank you very much Smiley Happy

0 Kudos