VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot

Seeing output twice in the output file.

Hi,

I am seeing the output twice in the output file. Please help.

$vmInfo = Foreach ($vc in $global:DefaultVIServers)
{
$report = @()
$folder = "Sales"
$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
$allocated.'Resource_Info' = 'Allocated'
$allocated.CPU = 308
$allocated.'Memory(GB)' = 1150
$allocated.'Capacity(GB)' = 8849
$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={[math]::Round(($_.Group | Measure-Object -Property MemoryGB -Sum).Sum,0)}},
@{N="Capacity(GB)";E={"{0:f1}" -f ($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum)}},
@{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)","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
}
$vmInfo | Export-Excel -Path myoutput.xlsx

 

Example : seeing same output twice

ganapa2000_0-1634032492773.png

 

0 Kudos
10 Replies
LucD
Leadership
Leadership

I suspect you might have 2 connections to the same vCenter open.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

No, I think I have one connection.

I am trying to connect to my vCenters in the report like below

$vCenters = @(
"vcenter1"
"vcenter2"
)
Connect-viserver -Server $vCenters

0 Kudos
LucD
Leadership
Leadership

I just ran this, and I don't get the same output twice.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

Not sure LucD, just now I tried making the change in the output file from outside to inside as below, now I got only once 🙂

not sure, if it is correct or any other way ?

$vmInfo = Foreach ($vc in $global:DefaultVIServers)
{
$report = @()
$folder = "PMO(Sales-Support)"
$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
$allocated.'Resource_Info' = 'Allocated'
$allocated.CPU = 308
$allocated.'Memory(GB)' = 1150
$allocated.'Capacity(GB)' = 8849
$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={[math]::Round(($_.Group | Measure-Object -Property MemoryGB -Sum).Sum,0)}},
@{N="Capacity(GB)";E={"{0:f1}" -f ($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum)}},
@{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)","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 | Export-Excel -Path $reportlocation1 -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName PMO_Sales_Support
}
$vmInfo

0 Kudos
LucD
Leadership
Leadership

I don't think so, you moved the Export-Excel inside the loop.
That means you will be overwriting the XLSX for each vCenter.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

But If the place the export-excel outside, I am getting the same output twice.

I check the settings as below, not sure, if anything else i missed

ganapa2000_0-1634035841275.png

 

0 Kudos
LucD
Leadership
Leadership

Those settings are correct.
Did you check what you have in $global:defaultVIServers?


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

Here is the output from $global:defaultVIServers?

ganapa2000_0-1634036921966.png

 

 

0 Kudos
LucD
Leadership
Leadership

Are your vCenters by any chance in Linked Mode?

If you are permanently connected t all these vCenter, then you should in any case restrict all PowerCLI cmdlets to the specific vCenter with the Server parameter.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

No, they are not in linked mode.

I will connect to these vCenters and disconnect once I am done

disconnect-viserver -server * -force -confirm:$false

0 Kudos