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
0 Kudos
32 Replies
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Thanks,

I understood your point,

I wanted to retrieve all the files & folders in the root directory datastore , regardless or VM file or other.

I had a look at Get-Datastore  Myds |   %{$_.********)

I couldnt find a function which will return all the contents , VMWare and Non-VMWare related files.

I wanted to list all the files . may or may not be VM specfic .

Cheers

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see. In that case have a look at the Datastore Provider.

It is described in the vSphere PowerCLI Admin Guide that came with your PowerCLI installation. You'll find it under <Start><Programs><VMware><VMware vSphere PowerCLI>.

It will allow you to list the files on a specific datastore.


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

0 Kudos
realkeny
Contributor
Contributor
Jump to solution

You may want to take a look a this script below. It does some of the things you were looking for and may be much more. it also gives you a nicely formatted email with the disk utilization of each VM in each datastore highlighting any datastore with utilization above a specific value in RED. i have copied the script below and also attached the powershell script file. hope you or someone else in the community finds this useful.

$ServerName = "vCenter ServerAddress"
$result = @()
$MailServer = "MailServerAddress"
$Sender = "sending user email";
$SenderName = "sending user Full Name";  #optional
$Receiver = "receivinging user email";  #could be the same as sender address
$MailSubject = "VMware Infrastructure Disk usage";
$MailBody = "";
$threshold_percent = 70; #percentage datastore utilzation threshold
$ds_filter_pattern = "esx_*" #pattern for matching datastores to retrieve

function SendMail()
{
$MailClient = new-object system.net.mail.smtpclient($MailServer)
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.From = new-object System.Net.Mail.MailAddress($Sender, $SenderName)
$MailMessage.To.Add($Receiver)
$MailMessage.Subject = $MailSubject
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $MailBody
$mailClient.Send($MailMessage)
}

Connect-VIServer $ServerName #Connect to the VMware Infrastructure server (either ESX or vcenter server).
        #you may also want to specify a username and password if the account running the script does not have access to the vcenter.
        #the line then become "Connect-VIServer -Server $ServerName -Protocol https -User Username -Password password"
if($defaultVIServer -ne $NULL)
{
$msgHTML += "<html xmlns='http://www.w3.org/TR/REC-html40' /><head><meta http-equiv='content-type' content='text/html;charset=UTF-8' />"
$msgHTML += "<meta name=Generator content='Datastores in Virtual Infrastructure' /><meta name=Author content='Kenny Adetutu' />"
$msgHTML += "<style>"
$msgHTML += "BODY{background-color:white;}"
$msgHTML += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$msgHTML += "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:thistle}"
$msgHTML += "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$msgHTML += "</style></head>"
$msgHTML += "<body style='font-family: arial; font-size: 12;' />"
$msgHTML += "<table >"
$msgHTML += "<tr><th>Datastore Name</th><th>Total Capacity(GB)</th><th>Available Capacity(GB)</th><th>Percent FreeSpace</th><th>VM Disk Usage</th></tr>"

$datastores = get-view -viewtype "datastore" | where {$_.name -like $ds_filter_pattern} | sort name  #get-datastore | where {$_.name -like "esx_*"}

foreach ($datastore in $datastores)
{
  $ds_capacity = [Math]::Round((($datastore.Summary.Capacity)/1048576000), 2)
  $ds_freespace = [Math]::Round((($datastore.Summary.Freespace)/1048576000), 2)
  $threshold_value = [Math]::Round((($threshold_percent / 100) * $ds_capacity), 2)
  $percent_freespace = [Math]::Round((($ds_freespace / $ds_capacity) * 100), 2)
  $msgHTML += "<tr><td>" + $datastore.Name + "</td>"
  $msgHTML += "<td>" + $ds_capacity + "</td>"
  $msgHTML += "<td>" + $ds_freespace + "</td>"
  if(([Math]::Round(100-$percent_freespace)) -gt $threshold_percent)
  {
   $msgHTML += "<td style='background-color:red;color:white'>" + $percent_freespace + "<br /><span style='font-size:16px'><b>[Over-utilized]</b></span></td>"
  
  }
  else{
    $msgHTML += "<td>" + $percent_freespace + "</td>"
   }
  $msgHTML += "<td>"
  try
  {
   $vms = get-view $datastore.vm | where {$_.runtime.powerstate -eq "PoweredOn"}
 
   foreach($vm in $vms)
   {
    $Total_usage = 0
    $sinfo = $vm.storage.perDatastoreUsage
    for($i=0; $i -le $sinfo.length-1; $i++)
    {
     if($sinfo[$i].Datastore.Value -eq $datastore.MoRef.value)
     {
      $Total_usage = [Math]::Round(($sinfo[$i].Committed/1048576000), 2)
     }
    }
    $msgHTML += "<table style='border-style:none;'><tr><td style='background-color:thistle'>VM Name</td><td>" + $vm.name + "</td></tr>"
    if($Total_usage -gt $threshold_value)
    {
     $msgHTML += "<tr><td style='background-color:thistle'>Total Disk Usage(GB)</td><td style='background-color:red'>" + $Total_usage + "</td></tr>"
    }
    else{
      $msgHTML += "<tr><td style='background-color:thistle'>Total Disk Usage(GB)</td><td>" + $Total_usage + "</td></tr>"
     }
    $msgHTML += "</table><br />" 
   }

  }
  catch
  {
   write-warning "Error: the datastore $($datastore.Name) doest not have any VM"
  }
}

$msgHTML += "</table>"
$body = $msgHTML
$MailBody = "<b>The Virtual Infrastructure disk usage is displayed in the below.</b><br /><br />"  +$body+ "<br /><b>Thanks.<b>"
SendMail
}
Disconnect-VIServer $ServerName -Confirm:$false

0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Well done mate ,

This report shows aqll the VMs present in VCenter. I have some Lab manager Templates and some deleted vms which also have .vmdks still sitting on the datastore.  is there a way to get them on this list as well?

Thanks

0 Kudos
realkeny
Contributor
Contributor
Jump to solution

This script get information for only VMs that are powered On. as you can see from the line that says

$vms = get-view $datastore.vm | where {$_.runtime.powerstate -eq "PoweredOn"}

In order to include all VMs as well as templates in the report, you just edit the line to

$vms = get-view $datastore.vm

Hope that helps.

0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

Yeah I have noticed that line , but Lab manager VMs are created within subdirectories in the datastore,

In my case some .vmdks are lying about and other files as well.

Also I have some vm folders which are not listed , those are the one's removed from the Vcenter.

Accendently we have removed from the Vcenter rather than deleting .

Cheers

0 Kudos
realkeny
Contributor
Contributor
Jump to solution

In my test environment, i have VM created in subdirectories of datastores and are listed in the report. As long as the VM are still registered in vcenter, it will show up when you issue the command (get-vm). If you delete VMs by just removing it from inventory without deleting it from disk, then it will not show up in the report of this script or using the get-vm command. You may need to write a separate script that checks for orphaned VM. This means the script will browse through all datastore looking for vmx or vmdk file that does not match any of the names returned by the get-vm command.

I may be able to put something together when i have some spare time.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

For orphaned VMs have a look at my VMX Raiders Revisited and Raiders of the Lost VMX posts.


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

0 Kudos
hekulkar1
Contributor
Contributor
Jump to solution

Hi Team,

I am trying to genereate datastore report & my requirment is if % free space is less than 10% it should show red & % free is grater than 10% it should show green color ..

I am using below script to generate html out put but critical & free space colour symbols are not working

$clusters=get-cluster

foreach ($cluster in $clusters) {
get-cluster $cluster |Get-VMHost |Get-Datastore | Select Name,@{N="FreeSpaceGB";E={[System.Math]::Round($_.FreeSpaceMB / 1024, 2)}} ,@{N="CapacityGB";E={$_.CapacityMB / 1024}},PercentFree| ConvertTo-html -head $a -Body "<H2> $cluster Storage Details </H2>" |foreach {if($_.PercentFree -le "10"){$_ -replace "<tr>", "<tr bgcolor=red>"}elseif($_.PercentFree -ge "10"){$_ -replace "<tr>", "<tr bgcolor=green>"}else{$_}}   >> c:\vc.html
#-head $a -Body "<H2> $cluster Storage Details </H2>" >> c:\abc.html

}

can you expert pls guide me on this

0 Kudos
realkeny
Contributor
Contributor
Jump to solution

Hi hekulkar1,

You may want to try out the attached script. It creates an html file containing disk usage by VMs and also percentage utilization of each Datastore with the color scheme similar to your requirement. It also sends the file as email attachment, so you will need to configure the parameters of the script to match your environment.

Hope this works for you.

Best regards,

Kenny.

0 Kudos
tigerdeccan
Enthusiast
Enthusiast
Jump to solution

@ realkeny

Awesome work

0 Kudos
virtualdive
VMware Employee
VMware Employee
Jump to solution

Hello vlife,

Please can I requrest you to include the Total Capacity of Datastore into this script?

Thank you for great peice of work again,

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
0 Kudos
realkeny
Contributor
Contributor
Jump to solution

Hello,

The generated html file has the Total Storage Capacity of each Datastore in the 2nd column as “Total Capacity” if you ran the script correctly. I run the scripts every week to gather Datastore usage in my environment.

Thanks.

0 Kudos