Description:
The attached script generates a csv-file with all Virtual Machines’ Disks, in which Datastore they are stored, the LUN IDs of the extents that make up this Datastore (in HEX) and the Vendor of the SAN those LUNs are on (just in case you have multiple). Simpy a great way to determine which LUNs are used by which virtual server(s) in a complex environment.
Code:
# VARIABLES $outputFile = 'D:\scripts\VMDiskOverview.csv' # Output file path $VCServer = "MyVCServer" # Virtual Center Server # SCRIPT $VC = Connect-VIServer $VCServer # Connect to Virtual Center $myCol = @() # Prepare output collection $VMs = Get-VM | Sort Name # Get all VMs (sorted) $counter = 0 # Initialize counter for progress bar ForEach ($VM in $VMs) # Loop through VMs { $counter++ # Increase counter for progress bar Write-Progress -Activity "Gathering disk information" -Status "Processing VM $VM" -PercentComplete (100*($counter/$VMs.count)) # Display progress bar $VMHost = Get-VMHost -VM $VM # Get this VM's host $VMHostView = $VMHost | Get-View # Get advanced properties of host ForEach ($DISK in $VM.HardDisks) # Loop through VM's harddisks { $myObj = "" | Select VM, Description, Disk, Datastore, hexLUN0, hexLUN1, hexLUN2, hexLUN3, SAN # Create output object $myObj.VM = $VM.Name # Virtual Machine Name $myObj.Description = $VM.Description # Virtual Machine Description $myObj.Disk = $DISK.Name # Virtual Disk Name $myObj.Datastore = $DISK.Filename.Split("[]")[1] # Datastore Name $extents = ($VMHostView.Config.FileSystemVolume.MountInfo | Where {$_.Volume.Name -eq $myObj.Datastore}).Volume.Extent # Extents for this datastore If ($extents.GetType().BaseType -match "Array") # In case of multiple extents { For ($i = 0; $i -lt $extents.count; $i++) # Increment a counter { $myObj."hexLUN$i" = "{0:X}" -f [int]$extents[$i].DiskName.Split(":")[2] # LUN ID converted to HEX Write-Progress -Activity "Gathering disk information" -Status ("Processing VM $VM - Found LUN {0}" -f $myObj."hexLUN$i") -PercentComplete (100*($counter/$VMs.count)) # Disply progress bar } } Else # In case of single extent { $myObj.hexLUN0 = "{0:X}" -f [int]$extents.DiskName.Split(":")[2] # LUN ID converted to HEX Write-Progress -Activity "Gathering disk information" -Status ("Processing VM $VM - Found LUN {0}" -f $myObj.hexLUN0) -PercentComplete (100*($counter/$VMs.count)) # Display progress bar } $myObj.SAN = ($VMHostView.Config.StorageDevice.SCSILUN | Where {$_.CanonicalName -eq $extents[0].DiskName}).Vendor # SAN Vendor Clear-Variable Extents -ErrorAction SilentlyContinue # Cleanup variable(s) $myCol += $myObj # Add output to collection } } $myCol | Export-Csv $outputFile -NoTypeInformation # Export output to csv Invoke-Item $outputFile # Launch outout file |
Download:
create-vmdiskoverview (Rename to .ps1)
Disclaimer:
All the scripts on PeetersOnline.nl are published under a Creative Commons license, which means you should refer to me if you want to republish (pieces of) them. Thank you. More information about Creative Commons can be found here: Creative Commons Attribution 3.0 Netherlands License.
3 Responses to Create-VMDiskOverview
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008


Can we add the size of hard drive assigned to each vm in this script? Thanks.
Never mind.. I found my answers in one of your other posts. Thanks!!!
Great! I love readers that are able to combine the information in different posts. Well done!
Hugo