In the last weeks I accompanied a customer during the preparation of his storage migration. Among other things, we came up with the usual questions on this subject:
- On which datastores are my virtual machines located?
- Are there virtual machines with snapshots?
- Are CD/DVD drives still connected to certain VMs?
That’s why I played around with PowerCLI and wrote some scripts to get answers to these questions. I leave the setup of the vCenter connection and the parameterization of individual commands to each of you and concentrate here on the central command(s).
List datastores of certain VMs
This small command returns a list of VMs sorted by the name of the VM in a specified cluster, including their datastore and the VM folder.
Get-Cluster -Name <ClusterName> | Get-VM | Select-Object Name,@{N=”Datastore”;E={[string]::Join(‘,’,(Get-Datastore -Id $_.DatastoreIdList | Select-Object -ExpandProperty Name))}},@{N=”Folder”;E={$_.Folder.Name}} | Sort-Object Name
The following example shows the output of the command.
List VMs with Snapshots
The second command creates a list of VMs with snapshots, sorted by name, with information about the name of the snapshot, the date the snapshot was created, and the size of the snapshot.
Get-Cluster -Name <ClusterName> | Get-VM | Get-Snapshot | Sort-Object VM | Format-Table VM,Name,Description,Created,@{ n="SizeGB";e={[math]::round($_.SizeGB,0)}} -AutoSize
Find below a screenshot of the output.
List VMs with CD/DVD drive attached
The last command lists all VMs with a connected CD/DVD drive and the path to the inserted ISO image.
Get-Cluster -Name <ClusterName> | Get-VM | Get-CDDrive | Select-Object @{N="VM";E="Parent"},IsoPath | Where-Object {$_.IsoPath -ne $null} | Sort-Object VM
The following is an example of the output of the command.
Wrap-Up
PowerCLI makes it really easy for an administrator to find out what dependencies exist prior to storage migration. With the commands shown above, you can quickly and easily get an overview of the current state. If you wish, you can also extend or rewrite the commands accordingly and, for example, completely automatically remove all attached ISO images from the VMs.