Hyper-V

PowerShell is King – Find VM Configuration files (.vmcx) that have not been imported/registered

Sometime you need to move a large number of VMs, or you need to recover from a broken hyper-v host, or similar. If you have a few VM’s, that will be easy, but if you have many you will most likely use PowerShell to import the VM’s. That works perfectly fine, but you have a few VM’s that did weren’t imported correctly you will end in a state when you ask your self –Which VM’s are imported and which ones are not?

(You can download the script from GitHub: https://github.com/DeploymentBunny/Files/tree/master/Tools/Get-VIAUnimportedvmcxFiles )

Find the not yet imported VM configurations

That will of course be done using PowerShell, and here is the function that I use

image
Script and output.

Function Get-VIAUnimportedvmcxFiles
{
    <#
    .Synopsis
        Script used find not yet imported Hyper-V Configurations
    .DESCRIPTION
        Created: 2016-11-07
        Version: 1.0
        Author : Mikael Nystrom
        Twitter: @mikael_nystrom
        Blog   : http://deploymentbunny.com
        Disclaimer: This script is provided "AS IS" with no warranties.
    .EXAMPLE
        Get-VIAUnimportedvmcxFiles
    #>    
    [CmdletBinding(SupportsShouldProcess=$true)]
    
    Param(
    [string]$Folder
    )

    if((Test-Path -Path $Folder) -ne $true){
        Write-Warning "I'm sorry, that folder does not exist"
        Break
    }

    $VMsIDs = (Get-VM).VMId
    $VMConfigs = (Get-ChildItem -Path $Folder -Filter *.vmcx -Recurse).BaseName

    $obj = Compare-Object -ReferenceObject $VMsIDs -DifferenceObject $VMConfigs

    $Configs = foreach($Item in ($obj.InputObject)){
        $Items = Get-ChildItem -Path $Folder -Recurse -File -Filter *.vmcx  | Where-Object -Property Basename -Like -Value "*$Item" 
        $Items | Where-Object -Property FullName -NotLike -Value "*Snapshots*"
        }
    Return $Configs.FullName
}

/mike

Categories: Hyper-V, PowerShell

Tagged as: ,

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

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