Hi All,
I have this script that reports the resources of a given list of servers from text file in HTML format. What i am struggling with is creating another table that has the totals i.e. total CPU, Total memory for all the vms in the file.. I also need help rounding up the provisioned space whenever i try and system.math syntax to the code it trows errors
Any ideas greatly appreciated, esxi 6.0
$Result = @()
foreach ($vm in $vmName) {
$view = get-vm $vm
$vms = "" | select-Object Name, VMHost, PowerState, NumCpu, MemoryGB, ProvisionedSpaceGB
$Result += New-Object PSObject -Property @{
ServerName = $vms.Name = $view.Name
VMHost = $vms.VMHost = $view.VMHost
Powerstate = $vms.PowerState = $view.PowerState
CPU = $vms.NumCpu = $view.NumCpu
Mem = $vms.MemoryGB = $view.MemoryGB
UsedGB = $vms.ProvisionedSpaceGB = $view.ProvisionedSpaceGB
}
Try something like this.
I removed those double assignments, I don't see why they are needed.
And I also removed the Select-Object.
foreach ($vm in Get-VM -Name $vmName)
{
$Result += New-Object PSObject -Property @{
ServerName = $vm.Name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 1)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
$Result
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Discussion moved from VMware vSphere™ to VMware PowerCLI
Try something like this.
I removed those double assignments, I don't see why they are needed.
And I also removed the Select-Object.
foreach ($vm in Get-VM -Name $vmName)
{
$Result += New-Object PSObject -Property @{
ServerName = $vm.Name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 1)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
$Result
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is a quick and dirty update.
$Result = @()
$totals = @()
$TotalCPU = $null
$TotalMem = $null
$TotalStr = $null
foreach ($vm in $vmName) {
$view = get-vm $vm
$vms = "" | select-Object Name, VMHost, PowerState, NumCpu, MemoryGB, ProvisionedSpaceGB
$Result += New-Object PSObject -Property @{
ServerName = $vms.Name = $view.Name
VMHost = $vms.VMHost = $view.VMHost
Powerstate = $vms.PowerState = $view.PowerState
CPU = $vms.NumCpu = $view.NumCpu
Mem = $vms.MemoryGB = $view.MemoryGB
UsedGB = $vms.ProvisionedSpaceGB = $view.ProvisionedSpaceGB
}
$TotalCPU += $view.NumCpu
$TotalMem += $view.MemoryGB
$TotalStr += $view.ProvisionedSpaceGB
}
#Round storage to 2 decimal places
$TotalStr = [math]::Round($TotalStr,2)
# Build up a little object for Totals
$totals += New-Object PSObject -Property @{
TotalCPU = $TotalCPU
TotalMem = $TotalMem
TotalStorage = $TotalStr
}
$result
$totals
Thank you for your replies guys i really appreciate your support, below us what i have from your suggestions. The report creates the tables and reports on the server resources fine but there are no data for the totals unfortunately. FYI - the reason i am doing the HTML format in this way is so that i can change the cell red when it value exceeds what we expect to see..
Luc - by the way when i remove the double assignment i get no value back..
$Result = @()
$totals = @()
$TotalCPU = $null
$TotalMem = $null
$TotalStr = $null
foreach ($vm in $vmName) {
$view = get-vm $vm
$vms = "" | select-Object Name, VMHost, PowerState, NumCpu, MemoryGB, ProvisionedSpaceGB
$Result += New-Object PSObject -Property @{
ServerName = $vms.Name = $view.Name
VMHost = $vms.VMHost = $view.VMHost
Powerstate = $vms.PowerState = $view.PowerState
CPU = $vms.NumCpu = $view.NumCpu
Mem = $vms.MemoryGB = $view.MemoryGB
UsedGB = $vms.ProvisionedSpaceGB = [math]::Round($view.ProvisionedSpaceGB, 0)
}
$TotalCPU += $view.NumCpu
$TotalMem += $view.MemoryGB
$TotalStr += $view.ProvisionedSpaceGB
}
#Round storage to 2 decimal places
#$TotalStr = [math]::Round($TotalStr,2)
# Build up a little object for Totals
$totals += New-Object PSObject -Property @{
TotalCPU = $TotalCPU
TotalMem = $TotalMem
TotalStorage = $TotalStr
}
#Start of html formatting
if($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>VMHost</B></TD>
<TH><B>Power State</B></TD>
<TH><B>CPUs</B></TD>
<TH><B>Memory GB</B></TD>
<TH><B>Used Space</B></TH>
</TR>"
Foreach($Entry in $Result){
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.VMHost)</TD>
<TD>$($Entry.PowerState)</TD>
<TD>$($Entry.CPU)</TD>
<TD>$($Entry.Mem)</TD>
<TD>$($Entry.UsedGB)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputReport
}
########################## Next table
if($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>Server Memory</B></TH>
<TH><B>Server CPU</B></TD>
<TH><B>Server Space</B></TH>
</TR>"
Foreach($Entry in $Result){
$HTML += "
<TD>$($Entry.TotalMem)</TD>
<TD>$($Entry.TotalCPU)</TD>
<TD>$($Entry.TotalStorage)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputTotal
}
#Start of email formatting
$body = [System.IO.File]::ReadAllText($OutputReport)
$body += [System.IO.File]::ReadAllText($OutputTotal)
If you look at my snippet, you'll notice that I also dropped the Select.
The New-Object is all that is needed.
And single assignments of course
Did you actually test my snippet?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
I did but i tried to take parts of your coed and add it to mine, i got a number of errors because of some conflicts. I have actually now just tried copying and pasting your code replacing mine and it does work a treat, and i should be able to still change the cell value to red when it exceed the agreed amount in HTML table.
Thank you very much Luc, i am very grateful for all your posts and your prompt response....!
Thanks
Btw, if you have problems implementing that conditional colouring of cells, there are a number of threads in this community on that.
Otherwise feel free to ask.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference