VMware Cloud Community
falcalen
Contributor
Contributor
Jump to solution

Script for getting datastore inventory

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.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
9 Replies
vlife201110141
Enthusiast
Enthusiast
Jump to solution

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}

LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
falcalen
Contributor
Contributor
Jump to solution

LucD, how do I save this to a csv file?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
falcalen
Contributor
Contributor
Jump to solution

Thanks for the help.

0 Kudos
falcalen
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean changing the names of the column headers ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
falcalen
Contributor
Contributor
Jump to solution

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.

0 Kudos
AGladden
Contributor
Contributor
Jump to solution

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?

0 Kudos