Hello All,
Need help here please, I want a script that generates the complete report of only datastores in the vCenter and email it in html format.
Any help would be appreciated.
Thanks
Nick
To exclude the local datastores, you change this line
$Datastores = Get-Datastore
into this
$Datastores = Get-Datastore | where {$_.Extensiondata.Summary.MultipleHostAccess}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you be a bit more specific about what you want in such a datastore report ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hey Luc,
If I get 'Total datastore size' and 'Free space' left, that will do just fine. It should be in columns in a HTML format sent out in an email.
Cheers!
Try something like this
$page = Get-Datastore | Select Name,CapacityMB,FreeSpaceMB | ConvertTo-Html | Out-String
Send-MailMessage -From "lucd@lucd.info" -To "lucd@lucd.info" `
-SmtpServer "mail.lucd.info" `
-BodyAsHtml -Body $page -Subject "Database Report"
By using stylesheets in the ConvertTo-Html cmdlet you can make the HTML page very fancy.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$fileloc = 'c:\output.htm'
$body = "This is Datastore Usage PowerCLI Export“
$mailfrom = "mail address"
$to = "mailaddress1, mailaddress2"
$subject = "This message is sent by PowerCLI script"
$smtpserver = 'IP Address of your SMTP server'
connect-viserver -server 'yourvcenterserver' -user 'xxx' -password 'xxx'
function UsedSpace
{
param($ds)
[math]::Round(($ds.CapacityMB - $ds.FreeSpaceMB)/1024,2)
}
function FreeSpace
{
param($ds)
[math]::Round($ds.FreeSpaceMB/1024,2)
}
function PercFree
{
param($ds)
[math]::Round((100 * $ds.FreeSpaceMB / $ds.CapacityMB),0)
}
$Datastores = Get-Datastore
$myCol = @()
ForEach ($Datastore in $Datastores)
{
$myObj = "" | Select-Object Datastore, UsedGB, FreeGB, PercFree
$myObj.Datastore = $Datastore.Name
$myObj.UsedGB = UsedSpace $Datastore
$myObj.FreeGB = FreeSpace $Datastore
$myObj.PercFree = PercFree $Datastore
$myCol += $myObj
}
$myCol | Sort-Object PercFree | ConvertTo-Html –title "Datastore space " –body "<H2>Datastore space available.</H2>" -head "<link rel='stylesheet' href='style.css' type='text/css' />" | out-file $fileloc -Append
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($fileloc)
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$msg.From = $mailfrom
foreach($mailTo in $to){
$msg.To.Add($mailto)
}
$msg.Subject = $subject
$msg.Body = $body
$msg.Attachments.Add($att)
$smtp.send($msg)
Thank Luc, vlife,
Is it possible to see only specific datastore details. It is generating huge list including the local disk of ESXhosts. Maybe someway if we can exclude the local disks?
Please suggest,
Thanks,
get-datastore cmdlet have -name parameter which is located on position1. You can limit whatever datastore you want with -name parameter. It is postion 1 so you dont need to type parameter name.just change this line.
$Datastores = Get-Datastore "datastore1, datastore2"
To exclude the local datastores, you change this line
$Datastores = Get-Datastore
into this
$Datastores = Get-Datastore | where {$_.Extensiondata.Summary.MultipleHostAccess}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would have changed the way Luc says. He has better approach. Both script works that way
I have a dum question when I run the script from lucD, I get the error below.
I am not sure if it is talking about my smtp server or vc server. If I pull out the mailing portion, it works just fine. I wanted to get the email portion working though.....
What I want to get to work:
$page = Get-Datastore | Select Name,CapacityMB,FreeSpaceMB | ConvertTo-Html | Out-String
Send-MailMessage -From "MYEMAILADDRESS" -To "MYEMAILADDRESS" `
-SmtpServer "MYSMTPSERVER" `
-BodyAsHtml -Body $page -Subject "Database Report"
ERROR I RECEIVE::::::
PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts> .\datastore-list.ps1
Send-MailMessage : Unable to connect to the remote server
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\datastore-list.ps1:2 char:17
+ Send-MailMessage <<<< -From "MYEAMILADDRESS" -To "MYEMAILADDRESS" `
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
thanks
Yes, this is the SMTP problem, you will have to define these parameters to get this error out. SMTP server you can get from the AD, exchange guys and email to and fro you may already know.