1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #Global Functions
#This function generates a nice HTML output that uses CSS for style formatting.
function Generate-Report {
Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal-left {text-align:left;}.Normal {text-align:center;}</style></head><body><table><tr class=""Title""><td colspan=""3""> VMware Datastore Change Report </td></tr><tr class="Title"><td>Datastore</td><td> Difference(MB) </td><td> % Free </td></tr>"
Foreach ($line in $myColDiff){
Write-Output "<td class=""Normal-left"">$($line.name)</td><td class=""Normal"">$($line.Diff)</td><td class=""Normal"">$($line.PercentFree)</td></tr> "
}
Write-Output "</table></body></html>"
}
# Variables #
#############
$VIServer = "VISERVER"
$username = "changeme"
$password = "supersecret"
$digits = 2
$Folder = 'C:\Reports'
$currentFile = 'Datastores_Current.xml'
$previousFile = 'Datastores_Previous.xml'
# Script #
##########
# First, if a Current file exists, rename this Current file to Previous
If (Test-Path "$Folder\$currentFile")
{
Remove-Item -Path "$Folder\$previousFile" -Force
Rename-Item -Path "$Folder\$currentFile" -NewName $previousFile
}
# Next, let's measure and save current datastore sizes
# Connect to Virtual Center
$VC = Connect-VIServer $VIServer -user $username -password $password
# Get all datastores and put them in alphabetical order
$datastores = Get-Datastore | Sort-Object Name
# Create an array to hold the output
$myColCurrent = @()
# Loop through datastores
ForEach ($store in $datastores)
{
# Create a custom object and define its properties
$myObj = "" | Select-Object Name, CapacityGB, UsedGB, PercFree
# Set the values of each property
$myObj.Name = $store.name
$myObj.CapacityGB = [math]::Round($store.capacityMB/1024,$digits)
$myObj.UsedGB = [math]::Round(($store.CapacityMB - $store.FreeSpaceMB)/1024,$digits)
$myObj.PercFree = [math]::Round(100*$store.FreeSpaceMB/$store.CapacityMB,$digits)
# Add the object to the output array
$myColCurrent += $myObj
}
# Export the output to an xml file; the new Current file
$myColCurrent | Export-Clixml -Path "$Folder\$currentFile"
# Disconnect from Virtual Center
Disconnect-VIServer -Confirm:$False
# Finally, let's compare the Current information to that in the Previous file
# Check if a Previous file exists
If (Test-Path "$Folder\$previousFile")
{
# Import the Previous file
$myColPrevious = Import-Clixml "$Folder\$previousFile"
# Create an array to hold the differences
$myColDiff = @()
# Loop through the current datastores
ForEach ($myObjCurrent in $myColCurrent)
{
# The actual compare command
$diff = Compare-Object ($myColPrevious | Where { $_.Name -eq $myObjCurrent.Name }) $myObjCurrent -Property PercFree
# In case of any differences, try to get specifics
If ($diff)
{
# Again, a custom object and properties for outputting results
$myObjDiff = "" | Select-Object Name, PercentFree, Diff
# Again, setting the values of each property
$myObjDiff.Name = $myObjCurrent.Name
$myObjDiff.PercentFree = $myObjCurrent.PercFree
# The most important property is the calculated difference between the current and previous values of PercFree. You can substitute it for UsedGB if you like.
$myObjDiff.Diff = ($diff | Where { $_.SideIndicator -eq '=>' }).PercFree - ($diff | Where { $_.SideIndicator -eq '<=' }).PercFree
# And agin, adding it to the output array
$myColDiff += $myObjDiff
}
# Clearing the variable used inside the loop to prevent incorrect output in case of problems setting the variable!
Clear-Variable diff -ErrorAction "SilentlyContinue"
}
# If nothing changed, we don't want an empty file.
If ($myColDiff.Length -eq 0)
{
$myColDiff = "No changes since last check."
}
# And we conclude by outputting the results to a text file, which can be emailed or printed.
Generate-Report > "$Folder\Track-Datastore.html"
}
IF ($myColDiff -ne "No changes since last check."){
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "my.smtp.host" #Change to a SMTP server in your environment
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "System.Automation@example.com" #Change to email address you want emails to be coming from
$MailMessage.To.add("yomomma@example.com") #Change to email address you would like to receive emails.
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "VMware Datastore Change Report"
$MailMessage.Body = Generate-Report
$SmtpClient.Send($MailMessage)} |