Experts, i need help in creating a script for getting datastore information in my vcenter. Just the basic output, total capacity, free space and provisioned space.
Thanks ahead.
You pipe the results to the Export-Csv cmdlet
Get-Datastore | Select Name, @{N="CapacityGB";E={[Math]::Round($_.CapacityMB/1KB,2)}}, @{N="FreeSpaceGB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}}, @{N="ProvisionedGB";E={[Math]::Round(($_.CapacityMB - $_.FreeSpaceMB + $_.Extensiondata.Summary.Uncommitted/1MB)/1KB,2)}} | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I run this as a scheduled task every day. I hope it is helpfull to you also...
$fileloc = 'c:\output.htm'
$body = "This is Datastore Usage PowerCLI Export“
$mailfrom = "mailaddress"
$to = "mailaddress1, mailaddress2, mailaddress3"
$subject = "This message is sent by PowerCLI script"
$smtpserver = 'Yoursmtpserver'
Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1
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)
$att.Dispose()
$msg.Dispose()
Start-Sleep -s 5
if (Test-Path $fileloc){Remove-Item -Path $fileloc -confirm:$false}
Try this
Get-Datastore | Select Name, CapacityMB, FreeSpaceMB, @{N="ProvisionedMB";E={$_.CapacityMB - $_.FreeSpaceMB + $_.Extensiondata.Summary.Uncommitted/1MB}}
And if you prefer GB
Get-Datastore | Select Name, @{N="CapacityGB";E={[Math]::Round($_.CapacityMB/1KB,2)}}, @{N="FreeSpaceGB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}}, @{N="ProvisionedGB";E={[Math]::Round(($_.CapacityMB - $_.FreeSpaceMB + $_.Extensiondata.Summary.Uncommitted/1MB)/1KB,2)}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, how do I save this to a csv file?
You pipe the results to the Export-Csv cmdlet
Get-Datastore | Select Name, @{N="CapacityGB";E={[Math]::Round($_.CapacityMB/1KB,2)}}, @{N="FreeSpaceGB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}}, @{N="ProvisionedGB";E={[Math]::Round(($_.CapacityMB - $_.FreeSpaceMB + $_.Extensiondata.Summary.Uncommitted/1MB)/1KB,2)}} | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the help.
LucD, the script is very helpful.. really appreciate it.
Anyway, is there a way to generate a report that displays the data in column?
ex. Name Total Space Free Space Provisioned Space
Thanks
Do you mean changing the names of the column headers ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
What I mean is that that output of the script is shown as such:
Datastore: datastore1
TotalSpace: 100GB
FreeSpace: 50GB
ProvisionedSpace: 70GB
Is there a way to make it like the one below:
Datastore TotalSpace FreeSpace ProvisionedSpace
datastore1 100GB 50GB 70GB
What I'm trying to change is the format so that it can be easily sorted in excel.
Thanks ahead.
The script the LucD posted should produce the output in the format that you are referring to:
Datastore TotalSpace FreeSpace ProvisionedSpaced
datastore1 100GB 50GB 70GB
Are you storing the information that you collect in a var. first and then calling it later some other way?