VMware Cloud Community
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Unregistering VMs

Hi all,

I currently have a script for finding and registering VM Templates or VMs, but what I need to be able to do now is Unregister a list of VMs and then register a select number of them with my ESX Hosts. Can anyone help me with some sample use of the UnregisterVM_Task.

My current registration script has this in it

  1. Register Template with MGMT1

$templateHost = Get-VMHost $mgmt1 | Get-View

$folder = Get-View (Get-Datacenter -Name $datacenterName | Get-Folder -Name $templateFolder).ID

$vmtx = $templatePath + "/" + $templateName + ".vmtx"

$task = $folder.RegisterVM_Task($vmtx,$templateName,$true,$null,$templateHost.MoRef)

I've tried changing the $task setting to unregister but I get an error that the method does not exist.

Any help would be appreciated.

Best Regards,

Alasdair.......

0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

When you say "Unregister" is it not just a case of removing the VM but not deleting it from the datastore ?

Remove-VM (Get-VM "MyVM") -Confirm:$false

You could then register them with a function as below:

Function Register-VM ($path, $name, $asTemplate, $esxhost){
	# Function to register an existing vmx of vmtx file in VC
	$ESXHostview = (Get-View (get-VMHost $ESXHost1).ID).MoRef
	$Folder = "vm"
	$Register = get-view -viewtype Folder -Filter @{"name" = $Folder}
	if ($asTemplate -eq $null){
		$Pool = (Get-View (Get-Resourcepool "Resources").ID).MoRef
	}
	Else {
		$Pool = $null
	}
	$Register.RegisterVM_Task($path, $name, $asTemplate, $pool, $ESXHostview)
}

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
2 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

When you say "Unregister" is it not just a case of removing the VM but not deleting it from the datastore ?

Remove-VM (Get-VM "MyVM") -Confirm:$false

You could then register them with a function as below:

Function Register-VM ($path, $name, $asTemplate, $esxhost){
	# Function to register an existing vmx of vmtx file in VC
	$ESXHostview = (Get-View (get-VMHost $ESXHost1).ID).MoRef
	$Folder = "vm"
	$Register = get-view -viewtype Folder -Filter @{"name" = $Folder}
	if ($asTemplate -eq $null){
		$Pool = (Get-View (Get-Resourcepool "Resources").ID).MoRef
	}
	Else {
		$Pool = $null
	}
	$Register.RegisterVM_Task($path, $name, $asTemplate, $pool, $ESXHostview)
}

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Hi Alan,

Sorry, I forget about the Remove-VM cmdlet. Thanks for the function though, it's better than my cobbled together effort.

Best Regards,

Alasdair..........

0 Kudos