VMware Cloud Community
virtualdive
VMware Employee
VMware Employee
Jump to solution

Selected datastore report PowerCLI

Hi,

Please can someone suggest a script or modify below one, for only generating a report for particular datastores. I do not want a report for every single datastore in vCenter, only the particular ones.

=====================================================================

$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'

$datastores = get-vmhost  | Get-Datastore | Sort-Object Name
$myColCurrent = @()

foreach ($store in $datastores){
$myObj = "" | Select-Object Name, CapacityGB, UsedGB, PercFree, Type, ID, Accessible
$myObj.Name = $store.Name
$myObj.CapacityGB = "{0:n2}" -f ($store.capacityMB/1kb)
$myObj.UsedGB = "{0:N2}" -f (($store.CapacityMB - $store.FreeSpaceMB)/1kb)
$myObj.PercFree = "{0:N}" -f (100 * $store.FreeSpaceMB/$store.CapacityMB)
$myObj.Type = $store.Type
$temp = Get-View -Id $store.Id
$myObj.ID = Get-DSDevice $store
$myObj.Accessible = $store.Accessible
$myColCurrent += $myObj
}

# Export the output
$myColCurrent | ConvertTo-Html -title "Datastore Information" -body "<H2>Datastore Information.</H2>" | Out-File -Append $fileloc

==========================================================================

Thanks,

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
1 Solution

Accepted Solutions
vlife201110141
Enthusiast
Enthusiast
Jump to solution

Pls change the first line

$fileloc = 'c:\output.htm'

View solution in original post

Reply
0 Kudos
32 Replies
vlife201110141
Enthusiast
Enthusiast
Jump to solution

$datastores = get-vmhost  | Get-Datastore | Sort-Object Name
Only this line change get-datastore part like below

get-datastore -Name "datastore1", "datastore2"

Reply
0 Kudos
vlife201110141
Enthusiast
Enthusiast
Jump to solution

$fileloc = 'c:\output.htm'
$body = "This is Datastore Usage PowerCLI Export“
$mailfrom = "xxx"
$to = "xxx, xxx, xxx"
$subject = "This message is sent by PowerCLI script"
$smtpserver = 'xxx'
Add-PSsnapin VMware.VimAutomation.Core
connect-viserver -server 'vcenterserver' -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 -Name "datastore1", "datastore2" | where {$_.Extensiondata.Summary.MultipleHostAccess}
$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}

Reply
0 Kudos
vlife201110141
Enthusiast
Enthusiast
Jump to solution

I'm sorry. You do not need this part

| where {$_.Extensiondata.Summary.MultipleHostAccess}

virtualdive
VMware Employee
VMware Employee
Jump to solution

thanks very much vlife,

This worked well, is there anyway to make the output presentable with some nice headers and colomns in the html. Organised.

Cheers again!

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
vlife201110141
Enthusiast
Enthusiast
Jump to solution

Here you go

$fileloc = 'c:\errors.htm'
$body = "Datastore Info“
$mailfrom = "xxx"
$to = "xxx"
$subject = "This message is sent by PowerCLI script"
$smtpserver = 'xxx'
Add-PSsnapin VMware.VimAutomation.Core
Connect-VIServer -Server 'vcenter' -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 -name "Datastore1", "Datastore2"
$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
}

$a = "<style>"
$a = $a + "BODY{background-color:#CCFFFF;}"
$a = $a + "TABLE{border-width: 20px;border-style: ridge;border-bottom-color:#3399FF;border-right-color:#3399FF;border-top-color:#336633;border-left-color:#336633;border-collapse: collapse;}"
$a = $a + "TH{border-width: 3px;padding: 5px;border-style: ridge;border-color: black;background-color:#99FFFF}"
$a = $a + "TD{border-width: 3px;padding: 3px;border-style: ridge;border-color: black;background-color:#CCFFFF;text-align:center;}"
$a = $a + "</style>"

$myCol | Sort-Object PercFree |
ConvertTo-HTML -head $a -body "<H2>Datastore Usage</H2>" |
Out-File $fileloc

$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}

vlife201110141
Enthusiast
Enthusiast
Jump to solution

Pls change the first line

$fileloc = 'c:\output.htm'

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Is there a way to display actual contents like VM Names that are sitting in the datatsore ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Listing VMs that sit on a specific datastore can be done with

Get-VM -Datastore MyDS | Select Name

But where and how would you want to display this in the report ?


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

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

That helps thanks a lot . Now I can display via email or CSV . Cheers !!!

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

But is there any other way to display exact vm-name or DNS name of VMs residing on the datastore ?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To send the list by email is quite easy

$vms = Get-VM -Datastore MyDS | %{$_.Name}
Send-MailMessage -To "lucd@lucd.info" -Subject "VMs on MyDS" -From "report@lucd.info" `
    -Body ($vms | Out-String) -SmtpServer "mail.lucd.info"

Do you mean that you want to the hostname instead of the VM display name ?

The hostname requires that you have the VMware Tools installed on all your VMs


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

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Yes ! I have VM Tools installed on all VMs.  I want DNS names of the VMs residing on the datastore.

Thanks

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

That script is awesome .. does exactly what I need..

Cheers 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just a small change, the hostname is another property.

$vms = Get-VM -Datastore MyDS | %{$_.Guest.HostName}
Send-MailMessage -To "lucd@lucd.info" -Subject "VMs on MyDS" -From "report@lucd.info" `
    -Body ($vms | Out-String) -SmtpServer "mail.lucd.info"


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

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

This is absolutly spot on !!!

Perfect Well done

you are genius !!!

thanks

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

DO you have any KB , I can learn all the properties from .

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

All the properties that are in the objects returned by a PowerCLI cmdlet can be found in the vSphere PowerCLI Overview.

For example, select All Cmdlets and then select the Get-VM cmdlet (both in the left panel).

In the right panel, when you scroll down a bit, you will see a Section called Return Type. Under the header you see VirtualMachine.

Click the hyperlink and you will get to all the properties that exist on level 1.

For the Guest entry you see that this is a VMGuest object, again click on the hyperlink and you find the level 2 properties under the Guest property.

In here you will find the HostName property.


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

Reply
0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Thanks , That helps!

However when you say  %{$_.Name} displays everything, I have discovered this command doesn't returning files with .iso ,doc etc .

I couldnt find any property like that . have you got any clue ?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let's take that line completely

Get-VM -Datastore MyDS | %{$_.Name}

First we use the Get-VM cmdlet to find all the VMs on datastore MyDS.

All the found VirtualMachine objects are placed in the pipeline (the '|' symbol).

In a Foreach loop (represented by the alias %) we execute the code block (everything between the curly braces) for each object (remember the VirtualMachine objects we placed in the pipeline).

Inside the code block, the object that is passed is represented by the $_ variable.

From that variable we take the Name property, which is apparently not included in the online help page.


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

Reply
0 Kudos