I have a daily report that I wrote and I really want to rewrite it, I would like it to be much neater looking, and I just want an overall better look to it. Of course I'm aware of the fabulous vCheck script, but for our environment, I want something with less information that people (besides techs) will look at, I also want to write it myself.
The main help I want is really just advice, I don't want actual code, because I want to write this all myself. I want to incorporate html directly into the email output, right now I'm using out-string when I send out the email and I want that to change. I know there is a -BodyAsHTML parameter when sending the email, so that will be good to bold and color sections of the email. I just need to use it correctly. I have all these ugly looking functions with code here, then text there, and I just know it looks bad underneath the hood. In email it was looking good, that is until I saw it within outlook 2010, in outook 2007 everything lines up, but in 2010, everthing is just off center, and that's one of the main reasons why I want to rewrite it, that and I want it done right this time.
I have omitted the private data from this post, but this is the script.
The actual email that goes out is at the bottom of this post, I just changed some of the numbers, because I'm overly paranoid.
Thanks for taking the time to look at this post, I know I have a lot on here.
The Script
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null)
{
Add-PsSnapin VMware.VimAutomation.Core
}
Connect-VIServer 10.10.10.10 -User .\Server -Password Password
Function Get-Average {
$TopTenVMMemory = Get-VM | where { $_.Powerstate -eq "PoweredOn" } | sort MemoryMB -Descending | select -First 10
$AvgVMMem = [Math]::Round((Get-VM | measure -Property MemoryMB -Average).average)
Write "Top ten VMs with the most memory assigned to them:"
Write $TopTenVMMemory; "`r"
Write "Average memory assigned to VMs in vCenter is: $("$AvgVMMem" + "MB")"
}
Function Get-VMOScount {
$VMCount = @()
$XPCount = 0
$VistaCount = 0
$7Count = 0
$2003Count = 0
$2008Count = 0
$LinuxCount = 0
$OtherCount = 0
$VMcount = Get-VM
switch ($VMcount) {
{ @($_.guest.OSFullname -match "XP") } { $XPCount++ }
{ @($_.guest.OSFullname -match "Vista") } { $VistaCount++ }
{ @($_.guest.OSFullname -match "7") } { $7Count++ }
{ @($_.guest.OSFullname -match "2003") } { $2003Count++ }
{ @($_.guest.OSFullname -match "2008") } { $2008Count++ }
{ @($_.guest.OSFullname -match "Linux") } { $LinuxCount++ }
{ @($_.guest.OSFullname -notmatch "XP" -and $_.guest.OSFullName -notmatch "Vista" -and $_.guest.OSFullName -notmatch "7"`
-and $_.guest.OSFullName -notmatch "2003" -and $_.guest.OSFullName -notmatch "2008" -and $_.guest.OSFullname -notmatch "Linux") } { $OtherCount++ }
}
"Total Windows XP VMs are: $XPCount."
"Total Windows Vista VMs are: $VistaCount."
"Total Windows 7 VMs are: $7Count."
"Total Windows Server 2003 VMs are: $2003count."
"Total Windows Server 2008 VMs are: $2008count."
"Total Linux (any type) VMs are: $LinuxCount."
"Total Other VMs are: $Othercount, these VMs may be powered off, or they may not have VMware tools running to collect the OS type."
}
Function PercentFree { ## Start Function
param($ds)
[Math]::Round(($ds.FreeSpaceMB / $ds.CapacityMB * 100),0)
} ## End Function
Function Write-Datastore { ## Start Function
$vms = ""
$ESXihosts = ""
$Templates = ""
$vms = Get-VM
$ESXihosts = Get-VMHost
$Templates = Get-Template
$totalESXiHost = ($ESXihosts).count
$totalTemplates = ($Templates).count
$totalVMs = ($vms).count
$totalVMsOff = ($vms | where { $_.powerstate -eq "PoweredOff" }).count
$snapsTotal = ($vms | Get-Snapshot).count
$Report = "" | Select ESXiHosts, VMs, Templates, "Total Snapshots"
$Report.esxiHosts = $totalESXiHost
$Report.VMs = "$totalVMs (Of those $totalVMs, $totalVMsoff are powered off)"
$Report.Templates = $totalTemplates
$Report."Total Snapshots" = $snapsTotal
"Report Generated on " + (Get-Date)
Write $Report | fl
Write "------------------------------------------------------------------------------------------------------"; "`r"
"VM Operating System count:"; "`r"
Get-VMOSCount; "`r"
Write "------------------------------------------------------------------------------------------------------"; "`r"
Get-Average
Write "------------------------------------------------------------------------------------------------------"; "`r"
"VMs with over 3 snapshots:"; "`r"
Get-VM | Foreach { $vm = $_.Name; $_ | Get-Snapshot | Measure-Object | Foreach { if ($_.Count -gt 3) { write $("$vm " + "= " + $_.Count) } } }
Write "------------------------------------------------------------------------------------------------------"
$runOnce = 0
$datastores = Get-Datacenter 'Datacenter' | Get-Datastore
$vDataStore = @()
Foreach ($datastore in $datastores) {
if ($runOnce -eq 0) { Write ("`r" + "Please do not place any VMs on the following datastores today " + "(" + (Get-date -Format "MM-dd-yyyy") + ")" + "," + " they are low on space: (Never place VMs on the `"Files`" datastore)"); "`r"; $runOnce++ }
$vDataStore = "" | Select Datastore, UsedSpace, Capacity, PercentageFree
$vDataStore.Datastore = $datastore.Name
$vDatastore.UsedSpace = [Math]::Round($datastore.FreeSpaceGB)
$vDataStore.Capacity = [Math]::Round($datastore.CapacityGB)
$vDataStore.PercentageFree = PercentFree $datastore
if ($vDataStore.PercentageFree -lt 15 -and $vDataStore.Datastore -notmatch 'ISOs') { write $vDataStore.Datastore ("{0:N0}% free" -f $vDataStore.PercentageFree),`
("Total amount free = " + $vDataStore.UsedSpace + "GB"),("Total capacity = " + $vDataStore.Capacity + "GB")"`r" }
}
} ## End Function
Send-MailMessage -SmtpServer '10.10.10.10' -From "<email@email.com>" -To "<email@email.com>" -Subject ("Daily summary for " + (Get-Date -Format "MM-dd-yyyy")) -Body (Write-Datastore | Out-String)
#Disconnect-VIServer * -Confirm:$false
End of the sript
**************************************
Email that goes out
**************************************
Report Generated on 08/07/2012 06:00:52
ESXiHosts : 33
VMs : 784 (Of those 784, 43 are powered off)
Templates : 40
Total Snapshots : 117
------------------------------------------------------------------------------------------------------
VM Operating System count:
Total Windows XP VMs are: 187.
Total Windows Vista VMs are: 0.
Total Windows 7 VMs are: 105.
Total Windows Server 2003 VMs are: 37.
Total Windows Server 2008 VMs are: 87.
Total Linux (any type) VMs are: 36.
Total Other VMs are: 23, these VMs may be powered off, or they may not have VMware tools running to collect the OS type.
------------------------------------------------------------------------------------------------------
Top ten VMs with the most memory assigned to them:
Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
VM1 PoweredOn 2 12288
VM2 PoweredOn 2 12288
Average memory assigned to VMs in vCenter is: 3029MB
------------------------------------------------------------------------------------------------------
VMs with over 3 snapshots:
vm-ls = 16
------------------------------------------------------------------------------------------------------
Please do not place any VMs on the following datastores today (08-07-2012), they are low on space: (Never place VMs on the "Files" datastore)