rpmello
Enthusiast
Enthusiast

Script to inventory storage

I'm pretty new to Powershell and the VI Toolkit, this is the first functional script I've made. It goes through a datacenter and tells you the amount of storage used by each folder. This is useful for me to be able to see which business units are using the most storage

What I really want to be able to do with this script is get a nicer output format and possibly have it recognize folder hierarchy so that it can be output and emailed automatically. For instance, under a Desktops folder I have separate folders for Vista and XP, but I can't figure out a way to have the script output them as being related.

$datacenterName = "Systems Datacenter"

$storageSum = 0

Get-Datacenter $datacenterName | Get-Folder | %{

$storageSum=0

Get-Folder $_.name | Get-VM | Get-HardDisk | %{$storageSum= $storageSum + $_.CapacityKB}

$storageSum = $storageSum/1048576

Write-Host $_.name $storageSum GB

}

0 Kudos
1 Reply
LucD
Leadership
Leadership

You can use the logic from the script in to run through all the "blue" folders and calculate the disk space reserved for each guest and template.

And report the total disk space reserved for each folder.

Something like this will do that.

function get-children($entity,$path){
  if($entity.Name -ne "vm"){
    $path += ("\" + $entity.Name)
  }
  foreach($child in $entity.ChildEntity){
    $childfld = Get-View -Id $child
	switch($childfld.gettype().name){
	  "Folder" {
	    if($global:foldersize -ne 0){
  	      Write-Host "Total size for folder : "  ($global:foldersize/1kb) "MB`n"
		  $global:foldersize = 0
		}
	    Write-Host ($path + "\" + $childfld.Name)
		get-children $childfld $path
	  }
      "VirtualMachine"{
	    $vm = $path + "\" + $childfld.Name
		$VMstorage = 0
		if($childfld.Config.Template) {
		  Get-Template $childfld.Name | Get-HardDisk | %{$VMstorage += $_.CapacityKB}
		}
		else {
		  Get-VM $childfld.Name | Get-HardDisk | %{$VMstorage += $_.CapacityKB}
		}

        Write-Host $vm ($VMstorage/1Kb) "Mb"
		$global:foldersize += $VMstorage
	  }
	  "Datacenter"{
	    Write-Host ($path + "\" + $childfld.Name)
        get-children $childfld $path
	  }
	  "ClusterComputeResource" {
	    Write-Host ($path + "\" + $childfld.Name)
        foreach($esxMoRef in $childfld.Host){
          $esx = Get-View -Id $esxMorEF
	      $h = $path + "\" + $childfld.Name + "\" + $esx.Name
		  Write-Host $h
		}
	  }
     }
  }
}

$global:foldersize = 0

# Virtual machines & Templates

Write-Host "Virtual Machines & Templates`n"
Get-Datacenter | %{
  $dc = Get-View -Id ($_).id
  $folder = Get-View -Id $dc.VmFolder
  get-children $folder $dc.Name
}

For emailing a report have a look at .


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