Recently we’ve had to revert to migrating a customer through cloning virtual machines to a lun on a ‘migration storage box’ after which we had to do an import in our vCenter environment. Normally I just Veeam for ease in the whole migration process but this time there were circumstances which prevented this. After decoupling the migration box and connecting it to our vCenter environment we had to import the virtual machines which seemed tedious to do for a lot of virtual machines. Espescially because I only knew the manual way to do this. Some googling helped me out by finding the VMX Raiders script by LucD which I partly reused in my own script below.

Since I didn’t feel like entering the variables all the time I converted the script to a function. This function accepts the following parameters:

  • Clustername (Cluster in which the import has to take place)
  • Datastorename (Datastore that holds the unregistered VM’s)
  • VMFolder (Obviously the target for the import)

The script can also use the -WhatIf: and -Confirm parameters so that you can check what happends or receive a confirmation window. Just import the function into your PowerShell session and use it in the following format:

Import-VMX-from-datastore -Cluster "CLUSTERNAME" -Datastores "DATASTORENAME" -VMFolder "VMFOLDERNAME"
function Import-VMX-from-datastore{
  #This provides the function with Debug/Verbose/WhatIf parameters.
  [CmdletBinding(SupportsShouldProcess=$True)]
 
#Defined input parameters.
  Param(
    [parameter(Mandatory=$true)]
	$Cluster,
	[parameter(Mandatory=$true)]
    $Datastores,
	[parameter(Mandatory=$true)]
	$VMFolder
  )
  
#Actual import code. 
	foreach($Datastore in Get-Datastore $Datastores) {
		# Collect .vmx paths of registered VMs on the datastore.
		$registered = @{}
		Get-VM -Datastore $Datastore | foreach{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | foreach {$registered.Add($_.Name,$true)}}
	
		# Set PSDrive for the search.
		New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' -WhatIf:$false | Out-Null
		$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
		where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name)}) 
		foreach ($unregisteredvm in $unregistered){
			Write-Verbose "Found unregistered VMX with name: $unregisteredvm.Name"
		}
		Remove-PSDrive -Name TgtDS
 
		#Register all .vmx Files as VMs on the datastore.
		foreach($VMXFile in $unregistered) {
		Write-Verbose "Registering VMX with name: $VMXFile on the platform."
		New-VM -VMFilePath $VMXFile.DatastoreFullPath -ResourcePool $Cluster -Location $VMFolder -RunAsync
		}
	}
}

I’ve also added some verbose logging code into this function so that you can easily check if all the found unregistered virtual machines have been imported. To see it just enable it with the ‘-verbose’ parameter at the end of the function.

Want to see more automation scripts that we’ve made? Just click on the button below!


Bryan van Eeden

Bryan is an ambitious and seasoned IT professional with almost a decade of experience in designing, building and operating complex (virtual) IT environments. In his current role he tackles customers, complex issues and design questions on a daily basis. Bryan holds several certifications such as VCIX-DCV, VCAP-DCA, VCAP-DCD, V(T)SP and vSAN and vCloud Specialist badges.

0 Comments

Leave a Reply

Avatar placeholder

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