Mapping Virtual Machines to Datastores to Storage, Part 2

In a previous post, I walked through the challenges facing one of my customers during a recent outage, during which they needed to understand the mapping of virtual machines to datastores to LUNs.

Due to time constraints, I didn’t have the time to properly test and wrap everything into a nice little package, so the solution I ended up with actually used 2 different scripts… one to collect information on the virtual machines and their datastores, and another to collect information on the SCSI LUNs underlying the datastores.

I wasn’t satisfied, however, and knew I should be able to do it all in one script.  After a bunch of fiddling and testing, I was able to come up with the following:

  • I start by prompting the end user for the name of a target export file.
  • Then I capture information on all the virtual machines for the vCenter in question.
  • Entering a loop, I assign a few variables (more for ease of reference than anything else)
  • Then I capture information on the datastore – and here’s the key – using the name of the datastore as reported by the virtual machine as the filter parameter.
    • Get-View -ViewType datastore -Filter @{“Name”= “^$dsname$”}
  • Then using a Select-Object statement I capture a bunch of properties on the datastore and the virtual machine and
  • Export to a CSV file.

Once I figured out how to use the name of the datastore as reported by the virtual machine as the filter parameter, the rest sort of solved itself.  Here is the completed script:

# you must connect and authenticate to a vCenter server
# use Connect-VIServer to do so
# establish Export file name
$csvname = Read-Host 'Please provide the file path and name for your CSV export'
# start by gathering the list of virtual machines for this vCenter, enter a ForEach loop
Get-View ViewType virtualmachine | ForEach-Object {
# Capture the name of the virtual machine as a variable
# Capture the details of the VMDK file(s) for the virtual machine, including path(s)
# Capture the name of the datastore for the virtual machine, as reported by the vm
# For each virtual machine, capture datastore details, using the
# name of the datastore AS REPORTED BY THE VIRTUAL MACHINE for the filter,
# capture the NAA ID of the LUN for the datastore,
# then export select properties to the CSV file named previously
Get-View ViewType datastore Filter @{"Name"= "^$dsname$"} |Select-Object @{n="VM Name";e={$vmname}}, @{n="VMDK Name";e={$vmdkname}}, @{n="VM Datastore Name";e={$dsname}}, name,@{n='NAA';e={$}} | Export-Csv $csvname NoTypeInformation –Append

It should be noted that this hasn’t been tested and probably doesn’t account for virtual machines that have VMDK files across multiple datastores, but then that wasn’t relevant for the customer in question.

I openly admit I am not a programmer, and only a novice PowerShell / PowerCLI scripter, so I am open to comments / suggestions!

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s