Script: VMWare Lab Manager Automatic Cleanup

Version 8


    In the official documentation they say that you shouldn't use Lab Manager and Virtualcenter to manage the same ESX Hosts. Since I don't have the luxury of investing in dedicated ESX Servers for my lab, I decided to write a script that automatically moves Lab Manager-created VM's into a specific folder and resource pool, and automatically removes undeployed Lab Manager VM's from the inventory.


    This keeps my VirtualCenter organized and puts the Lab Machines in low-end resource pools so that my production VM's still get their reserves and SLA shares.



    • Powershell

    • VI Toolkit for Windows

    • A VirtualCenter user with rights to browse through VirtualCenter and Move VM's

    • Access to the SSL port of your Virtualcenter server from the host you run the script (i.e. not firewalled).


    First, edit the script and change the constants so that they match your environment:

    1. Lab Manager Resource Pool - Which resource pool you want your Lab Manager VMs to reside (ex. "MyLab")

    2. Lab Manager Folder - Same as above but a folder under Virtual Machines and Templates

    3. Virtualcenter Server - Your VC Server. Note that the name here must match the name on your virtualcenter SSL certificate.

    4. VC Username - The user to connect to VirtualCenter as. I recommend a service account but you can use "administrator" if you want.

    5. VC Password - Password for the user. Yes, I know it is in cleartext in the script, but since it's on my VC server using a low-privilege service account and I apply strict permissions to the files there, I'm not worried. If this bothers you, look up the SecureCredentials stuff in powershell.


    I set up this to run as a scheduled task on my VirtualCenter server every 5 minutes, but it can run anywhere that has the VI Toolkit and access to the VirtualCenter SSL port.


    I recommend creating a service account to run the script periodically under, because it looks cool in your Virtualcenter when a bunch of cleanup tasks owned by "LabManagerCleanup" come up every 5 minutes.


    Huge thanks to the VI Toolkit team! The meat of the script is probably only about 10-15 lines and makes some direct VMWare SDK accesses that the base cmdlets don't do, I've just added a ton of comments and colored output fluff. Powershell saved me so much time, I banged this out in a couple hours.



    Hope this helps anyone else out there! I know there's some kludgy logic in this script and I note it, but I'm a consultant/admin, not a programmer, and it works just fine. Feel free to make it better!



    NOTE: The below may not format well in some versions of IE7. View it in Firefox and it should be line-carriaged properly.


    # TITLE:      Lab Manager Cleanup Script
    # AUTHOR:     Justin Grote <jgrote*NO*@!SPAM!enpointe.|PLEASE|com>
    # PURPOSE:     Cleans up undeployed Lab Manager VM's, and puts deployed Lab Manager VM's 
    #                into specific resource pools and folders
    # USAGE:     Edit Constants to match your environment, run once manually to test, then schedule as repeating task.
    #Load the VI Toolkit for Powershell
    add-pssnapin VMWare.VimAutomation.Core
    #Virtualcenter Server Name
    $VCNAME = ""
    #VirtualCenter Service Account
    #NOTE: Highly recommended this be a separate service account such as "VMLabMgrSA" so it is easy
    #      to see in Virtualcenter when the script is moving things about.
    $VCUSER = "username"
    $VCPASSWORD = "yourpassword"
    #Desired Lab Resource Pool Name
    $LABMGRPOOLNAME = "MyLabManagerResourcePool"
    #Desired Virtualcenter Folder
    $LABMGRFOLDERNAME = "MyLabManagerFolder"
    #Creates an indentation for subtasks. Used for script output formatting
    $INDENT = "  "
    ### SCRIPT
    write-host -fore green "== BEGIN LAB MANAGER CLEANUP =="
    #Connect to Virtualcenter
    $vc = get-viserver $VCNAME -user $VCUSER -password $VCPASSWORD
    #Lab Manager Resource Pool
    $LabMgrPool = get-resourcepool $LABMGRPOOLNAME
    #Lab Manager Virtualcenter Folder
    $LabMgrFolder = get-folder $LABMGRFOLDERNAME
    #Discover all Lab Manager VM's. 
    #Note this is done really sloppily by looking for all VM's that have a .vmx file in a 
    #specific datastore folder that Lab Manager manages. 
    #A Better way would be to query some sort of Lab Manager API.
    $LabMgrVMs = get-vm | where {(get-view $_.ID).summary.config.vmpathname -like "*] VM/*"}
    ## TASK 1: Move All Virtual Machines to the designated Lab Manager Folder and Resource Pool
    write-host -fore green "Moving Lab Manager Virtual Machines to their desired locations..."
    foreach ($LabMgrVM in $LabMgrVMs) {
      #Verify that the VM is not already in the desired folder and move it.
      if (-not ($(get-vm -location $LabMgrFolder | foreach{$_.get_ID() }) -contains $LabMgrVM.get_ID() )) {
        write-host -fore Yellow $INDENT "Moving" $LabMgrVM.Name "to folder" $LabMgrFolder.Name
        move-VM $LabMgrVM -Location $LabMgrFolder | out-null
      else {
        write-host $INDENT $LabMgrVM.Name "is already in folder" $LabMgrFolder.Name
      #Verify that the VM is not already in the desired resource pool and move it.
      if (-not ($(get-vm -location $LabMgrPool | foreach{$_.get_ID() }) -contains $LabMgrVM.get_ID() )) {
        write-host -fore Yellow $INDENT "Moving" $LabMgrVM.Name "to resource pool" $LabMgrPool.Name
        move-VM $LabMgrVM -Location $LabMgrPool | out-null
      else {
        write-host $INDENT $LabMgrVM.Name "is already in resource pool" $LabMgrPool.Name
    ## TASK 2: Clean up all undeployed Lab Manager VMs.
    write-host -fore green "Cleaning up all undeployed Lab Manager VMs"
    #Get all Lab Manager VM's listed in the defined Lab Manager folder.
    #I do this instead of getting *ALL* VM's because there may be some orphaned VMs that
    #Aren't from Lab Manager that we may not want to touch
    #Again, this is sloppy, and should use some sort of Lab Manager API
    $LabMgrRegisteredVMs = get-vm -location $LabMgrFolder
    #Check if any Lab Manager VM's are orphaned, and remove them from the inventory.
    foreach ($LabMgrVM in $LabMgrRegisteredVMs) {
      if ((get-view $LabMgrVM.get_ID() ).runtime.ConnectionState -like "orphaned") {
        write-host $INDENT "Removing orphaned VM" $LabMgrVM.Name "from inventory."
        remove-VM $LabMgrVM -confirm:$false | out-null
    write-host -fore green "== END LAB MANAGER CLEANUP =="
    ### END SCRIPT






    Justin Grote

    Senior Systems Engineer

    En Pointe Technologies