{"id":67,"date":"2019-06-23T23:28:10","date_gmt":"2019-06-23T22:28:10","guid":{"rendered":"http:\/\/192.168.8.14\/?p=67"},"modified":"2019-07-06T21:18:23","modified_gmt":"2019-07-06T20:18:23","slug":"vm-disk-audit-script","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=67","title":{"rendered":"VM disk audit script"},"content":{"rendered":"\n<p>This script scans all VMs in a vCenter and reports the location of each hard drive. The information collected for each drive is as follows:<\/p>\n\n\n\n<p>VM, Thick\/Thin, file path, datastore, capacity, size on disk, Number of snapshots, total size of snapshots.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; first-line: 1; title: ; notranslate\" title=\"\">\n$Report = @()\n$VMs = Get-VM\nforeach ($VM in $VMs)\n{\n  # loop through each VM\n  $VMname = $VM.name\n\n  # get diskobs (low level HD file info) and disk (HD info) \n  $DiskObjs = $vm.ExtensionData.layoutex.disk\n  $Disks = get-harddisk -vm $VM \n   \n  foreach ($Disk in $Disks)\n  {\n    $tmpObj = &#039;&#039; | select VM,StartageFormat,FilePath,DS,CapacityGB,UsedGB,DiskName,NumSnaps,SnapSizeGB  \n\n    # populate VM and basic disk info\n    $tmpObj.VM = $VMName\n    $tmpObj.StartageFormat = $disk.StorageFormat\n    $tmpObj.FilePath = $Disk.FileName\n    $tmpObj.DS = (($disk.Filename).split(&quot;]&quot;))&#x5B;0].replace(&quot;&#x5B;&quot;,&quot;&quot;)\n    $tmpObj.CapacityGB = $Disk.CapacityGB\n    $RealFileName = $Disk.FileName.Replace(&quot;.vmdk&quot;,&quot;-flat.vmdk&quot;)\n    $tmpObj.UsedGB = ($VM.ExtensionData.layoutex.file | where{$_.name -eq $RealFileName}).Size \/1gb\n\n    # get low level file info for this disk (using disk key value)\n    $ThisDiskObj = ($DiskObjs | where{$_.key -eq $Disk.ExtensionData.key}).chain.filekey | sort\n\n    # loop through all files (snapshot) that make up this disk\n    $tmpObj.SnapSizeGB = 0\n    $tmpObj.NumSnaps = 0\n    foreach ($DiskFileKey in $ThisDiskObj)\n    {\n      $ThisDiskFile = $vm.ExtensionData.layoutex.file | where{$_.Key -eq $DiskFileKey}\n      if ($ThisDiskFile.name -match &quot;-flat&quot;)\n      {\n        # base disk (-flat) VMDK\n        $tmpObj.UsedGB = $ThisDiskFile.Size \/ 1gb\n      }\n      $ThisDiskFile = $vm.ExtensionData.layoutex.file | where{$_.Key -eq $DiskFileKey}\n      if ($ThisDiskFile.name -match &quot;-delta&quot;)\n      {\n        # snapshot (-delta) VMDK\n        $tmpObj.SnapSizeGB = $tmpObj.SnapSizeGB + $ThisDiskFile.Size \/ 1gb\n        $tmpObj.NumSnaps ++\n      }\n    }\n\n    $tmpObj.DiskName = $Disk.Name\n    $Report += $tmpObj\n  } \n}\n\n$Report | export-csv &quot;c:\\temp\\VMs.csv&quot; -NoTypeInformation\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"81\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/06\/VMoutput001.jpg\" alt=\"\" class=\"wp-image-68\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/06\/VMoutput001.jpg 950w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/06\/VMoutput001-300x26.jpg 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/06\/VMoutput001-768x65.jpg 768w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\" \/><figcaption>Output saved as CSV and opened in Excel<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script scans all VMs in a vCenter and reports the location of each hard drive. The information collected for each drive is as follows: VM, Thick\/Thin, file path, datastore, capacity, size on disk, Number of snapshots, total size of snapshots.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,4],"tags":[9,11,10,8,6,7],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-powercli","category-powershell","tag-audit","tag-drive","tag-report","tag-vm","tag-vmware","tag-vsphere"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=67"}],"version-history":[{"count":0,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}