Skip to content

Instantly share code, notes, and snippets.

@rbocchinfuso
Created March 6, 2014 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbocchinfuso/9398933 to your computer and use it in GitHub Desktop.
Save rbocchinfuso/9398933 to your computer and use it in GitHub Desktop.
Datastore Capacity Report
#PowerShell script to check datastore capacity
add-pssnapin VMware.VimAutomation.Core
Connect-VIServer -Server 'vcenter' -User 'domain\user' -Password 'password'
#HTML formatting
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"
$a = $a + "</style>"
# Main section of check
Write-Host "Checking Datastore Capacity"
$date = get-date
$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
$filename = "x:\dir\datastore_cap_" + $datefile + ".htm"
#Get datastore capacity report
Write-Host "Colleacting Datastore report....." -f yellow
$ss = Get-Datastore | select Name, @{N="Capacity(GB)";E={[Math]::Round($_.CapacityMB/1024,2)}}, @{N="FreeSpace(GB)";E={[Math]::Round($_.FreeSpaceMB/1024,2)}}, @{N="Free Space (%)";E={[math]::Round(((100* ($_.FreeSpaceMB))/ ($_.CapacityMB)),0)}} | sort -Property "Free Space (%)" | ConvertTo-HTML -head $a -body "<H2>Datastore Capacity Report</H2>"| Out-File $filename
Write-Host "Datastore report exported at Script location" -f green
Write-Host " Complete" -ForegroundColor Green
Write-Host "Your datastore report has been saved to:" $filename
# Create mail message
$server = "smtp.domain.com"
$port = 25
$to = "user@domain.com"
$from = "user@domain.com"
$subject = "Datastore Capacity Report"
$body = Get-Content $filename
$message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body
#Create SMTP client
$client = New-Object system.Net.Mail.SmtpClient $server, $port
# Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf.
$client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
# Try to send the message
try {
# Convert body to HTML
$message.IsBodyHTML = $true
#Ub
# $attachment = new-object Net.Mail.Attachment($filename)
# $message.attachments.add($attachment)
# Send message
$client.Send($message)
"Message sent successfully"
}
# Catch an error
catch {
"Exception caught in CreateTestMessage1(): "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment