{"id":273,"date":"2010-03-23T10:01:47","date_gmt":"2010-03-22T23:01:47","guid":{"rendered":"http:\/\/threemillion.net\/blog\/?p=273"},"modified":"2010-03-23T10:01:47","modified_gmt":"2010-03-22T23:01:47","slug":"get-datastorestats","status":"publish","type":"post","link":"https:\/\/threemillion.net\/blog\/?p=273","title":{"rendered":"Get-DatastoreStats"},"content":{"rendered":"<p>Having set up a development lab environment for a project team they began having issues related to storage due to not seemingly understanding that if you put too many Virtual Machines (VMs) on a datastore (LUN) that there might be performance issues. In addition under VMware it is advisable, at least anecdotely, to keep 10 to 15 % free on every LUN. This is too allow startup and restart type VM operations to write temporary files and logs. I have found that having less than 10% free begins to cause problems such as VMs being unable to power up, operations timing out etc.<br \/>\nSo what I needed was not only the capacity and free space of a datastore, I also need the number of VMs.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfunction Get-DatastoreStats\r\n{\r\n  param(\r\n        [Parameter(\r\n            Mandatory=$true,\r\n            ValueFromPipeline=$true,\r\n            HelpMessage=&quot;Datastore&quot;\r\n        )]\r\n        [VMware.VimAutomation.Client20.DatastoreImpl]\r\n        $Datastore\r\n  )\r\n\r\n  Process {\r\n    Write-Debug &quot;Get a list of VMs on the datastore&quot;\r\n    $vmlist = Get-VM -Datastore $Datastore\r\n\r\n    #If no VMs returned then set value to zero\r\n    if ($vmlist -eq $null)\r\n    {\r\n      $NumVMs = 0\r\n    } \r\n      elseif ($vmlist.count -ne $null)\r\n    {\r\n    \t$NumVMs = $vmlist.count\r\n    } else {\r\n    \t$NumVMs = 1\r\n    } #end if\r\n\r\n    $hash =  @{}\r\n    $hash.Name = $Datastore.Name\r\n    $hash.CapacityGB = [Math]::Round($Datastore.CapacityMB\/1KB,0)\r\n    $hash.FreeSpaceGB = [Math]::Round($Datastore.FreeSpaceMB\/1KB,0)\r\n    $hash.FreeSpacePct = [Math]::Round($Datastore.FreeSpaceMB\/$Datastore.CapacityMB*100,0)\r\n    $hash.NumberVMs = $NumVMs\r\n    $object = new-object PSObject -property $hash\r\n    $object | Select Name, CapacityGB, FreeSpaceGB, FreeSpacePct, NumberVMs\r\n\r\n  } # end process\r\n\r\n} #end Function\r\n<\/pre>\n<p>Usage:<br \/>\nPass datastore objects to the function via the pipeline:<br \/>\nGet-Datastore -Name MyDatastore | Get-DatastoreStats<br \/>\nor<br \/>\nGet-Datastore | Sort | Get-DatastoreStats | ft<\/p>\n<p><a href=\"https:\/\/threemillion.net\/blog\/wp-content\/uploads\/2010\/03\/Get-DatastoreStatsResults1.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/threemillion.net\/blog\/wp-content\/uploads\/2010\/03\/Get-DatastoreStatsResults1.png\" alt=\"Example output of Get-DatastoreStats function\" title=\"Get-DatastoreStatsResults\" width=\"512\" height=\"113\" class=\"alignnone size-full wp-image-281\" \/><\/a><\/p>\n<p>Now I and the development team can see the datastore usage and VM numbers in one little report.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having set up a development lab environment for a project team they began having issues related to storage due to not seemingly understanding that if you put too many Virtual <a href=\"https:\/\/threemillion.net\/blog\/?p=273\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":""},"categories":[8,18],"tags":[],"_links":{"self":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/273"}],"collection":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=273"}],"version-history":[{"count":8,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":283,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions\/283"}],"wp:attachment":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}