Using PowerCLI to Find Datastore with Missing VM

PowerCLI to Find Datastore with missing VM

During the last maintenance window at work, we starting having issues with vCenter, so to resolve the issues vCenter services were restarted.  After the vCenter services came back up, I noticed one of the servers on a host had lost it connection.  To make things worse, the datastore for the VM changed from the human readable format to location name.

Datastore name changed from the human readable format
Datastore name changed from the human readable format

So as it stands, I’m not sure what datastore this VM is located on.  So I can either look at all of the datastores manually, or use PowerCLI to find it.

$path = "ds:///vmfs/volumes/52debada-e9ea2f68-937f-5cf3fcdbafc0/"
get-vmhost VMHost_Name | Get-Datastore | Where {$_.ExtensionData.info.url -eq $path}

So this with simple script, it returned the name of the datastore that it was on. So now I can just remove it from inventory, and add it back. Problem solved.

PowerCLI to Search Datastore for missing VM

What if we didn’t know anything but the virtual machine’s name?  Would you really what to manually search every datastore that is attached to a VMhost to find the VM folder?  I know I wouldn’t, so below is a script that will search for the VM folder on every datastore attached to a VMhost.

$datastores = Get-VMHost VMHost_Name | Get-Datastore
foreach ($datastore in $datastores){
write-host $datastore
$datastoreshortname = $datastore.name
Get-ChildItem -Recurse -Path "vmstore:\DataCenterName\$datastoreshortname\" -Include *VM_Name* | Select Name,DatastoreFullpath
}

This approach works, but it can take a lot of time to complete based on the number of attached datastores along with the density of VMs that is contains.

Sample results from the script to find the VM Folder
Sample results from the script to find the VM Folder

This are the experiences that I have had just over this past weekend, it caused a few setbacks, but I have learned from these problems.

-Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.