VMM Script Series, part 3 – Show VHD Collisions

I’m publishing some of the scripts I have written for VMM.  These are each based on specific customer asks or conversations that lead me to go sleepless in trying to solve the problems myself.  I am very interested in your feedback, especially if you would like to offer a change to improve efficiency or effectiveness.

Special thank you to Rajesh Ravindranath who helped me out a lot with this script.

Show VHD Collisions

This script looks for two VMs each mapped to the same VHD.  This can cause issues when starting VMs and it is not simple to identify which VMs are in conflict.

Just to comment, there are very legitimate reasons for a VHD to be mapped to multiple VMs.  If you had a very large set of data that needed to be accessed from two servers but those servers were not online at the same time, such as using a VHD as a container for transferring a large file copy rather than SMB.  Now that R2 has hot-swap storage this is less of an issue than it was on RTM, so the examples of legitimate reasons are dwindling…

Notes

In each of the scripts for this series, I have noted any manually assigned variables at the top of the script just under the header.  For this script you will want to update your VMM server name.

In each of the scripts for this series, I have added a line to load the VMM snap-in.  This way it can be run from any machine, including a workstation, where the VMM console is installed by right clicking on the file and choosing “run”.  If you run the script from within VMM you will get a few lines of error text that the snap-in is already loaded.  You can either ignore this, or comment out the line if it bothers you.

Screen Shots

image

image

Simple is good.  As always, I ignore the snap-in error when running from console.  The script returns each VM name and the location for the conflicting file.

Script

disclaimer: this script is not supported in any way. I have posted the code rather than the .ps1 file so that you can review it, modify it to make it your own, and test it before trusting it in a production environment. now this is your code, I am not responsible for its use.

 

    1: # ------------------------------------------------------------------------------
    2: # Show Storage Collisions
    3: # ------------------------------------------------------------------------------
    4: # blogs.technet.com/offcampus
    5: # version 1.0
    6: #
    7: # Description
    8: #  Useful for displaying any VHD or Pass-Through Disk mapped to more than 1 VM
    9: #
   10: # ------------------------------------------------------------------------------
   11:  
   12: # All variables in the script with customizable values are mapped here for convenience
   13: $VMMServerName = "v-vmm-01"
   14:  
   15: # Load Snap-Ins
   16: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager 
   17:  
   18: # Begin Script
   19: Get-VMMServer $VMMServerName | out-null
   20: $VM = Get-VM
   21:  
   22: $LocationVMMap = @{}
   23: foreach ($entry in $VM)
   24: {
   25:       $vhds = @(Get-VirtualHardDisk -vm $entry.Name)
   26:       If ($vhds) {$vhds | %{$LocationVMMap[$_.Location] += @($entry.Name)}}
   27: }
   28:  
   29: if ($($LocationVMMap.Keys | ?{($LocationVMMap[$_]).Count –gt 1}) -lt 0) {write-host "No Collisions."}
   30: Else {$LocationVMMap.Keys | ?{($LocationVMMap[$_]).Count –gt 1} | %{Write-Host “$($LocationVMMap[$_] –join ‘, ‘)" -nonewline -foregroundcolor yellow; Write-Host " collide on VHD file " -nonewline; Write-Host "$_.” -foregroundcolor yellow}}