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.

Creative Commons License
 

3 Responses to Create-VMDiskOverview

  1. Sandhya says:

    Can we add the size of hard drive assigned to each vm in this script? Thanks.

  2. Sandhya says:

    Never mind.. I found my answers in one of your other posts. Thanks!!!