Hi Experts,
I'm trying to remove a vm from the host inventory as well as disk but it fails to delete from the inventory. So when I try to re-create the vm with the same name it fails with VM name already exists. What am I doing wrong here?
$visrv=Connect-VIServer $esxhost -User $esxuser -Password $esxpwd -port 443
$vmlist = ("vm1", "vm-2", "vm-3")
foreach ($vm in $vmlist)
{
$vmState = GET-VM -Name $vm
if($vmState.PowerState -ne "PoweredOff")
{
Stop-VM -vm $vm
Write-output "$vm shutting down" (get-date)
}
Remove-VM $vm -server $visr -DeleteFromDisk:$true
}
Hello and welcome to the communities.
Note: Discussion successfully moved from VI: VMware ESX® 3.0 to VMware vSphere™ PowerCLI
You seem to be connecting to an ESXi server and not a vCenter. Is that correct ?
If you remove a VM from the ESXi server and that VM was also known in the vCenter, the VM will be still present in the vCenter, but with the indication "orphaned".
Where do you try to recreate the new VM, directly on the ESXi server ?
If yes, you will have to execute the Remove-VM while connected to the vCenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Correct I connect to the ESXi server to remove the vms. Is the proper way to connect to vCenter and do the remove?
If you remove a VM from the ESXi server and that VM was also known in the vCenter, the VM will be still present in the vCenter, but with the indication "orphaned".
That is what is happening.
Where do you try to recreate the new VM, directly on the ESXi server ?
I try to create the new VM on vCenter.
If yes, you will have to execute the Remove-VM while connected to the vCenter
But my vCenter has multiple esxi hosts and each ESXi host contains vms with the same name. So I don't want to delete all the vms but I want to delete a particual vm contained in host X. http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Remove-VM.html doesn't allow to specify a vm for a particual host while connect to a vCenter.
Thanks,
Sagun
Are these VMs with the same name all registered in vCenter ? That sounds impossible.
In any case, you can specify an ESXi server with the Get-VM cmdlet, use the Location parameter.
Like this for example:
Get-VM MyVM -Location (Get-VMHost $esxserver)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here's a snippet of the vcenter:
I may be not explaining the scenario.
I think the screenshot didn't get picked up in your previous reply
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry didn't realize. Here you go.
I see. It looks as if you have these VMs with the same name in different datacenters.
You can also use a Get-Datacenter -Name <dcname> on the Location parameter of the Get-VM.
That way only that specific VM gets removed (and unregistered).
Something like this
$vmlist = ("vm1", "vm-2", "vm-3") $dcName = "MyDC"
foreach ($vmname in $vmlist) { $vm = GET-VM -Name $vmname -Location (Get-Datacenter -Name $dcName) if($vm.PowerState -ne "PoweredOff") { Stop-VM -VM $vm
Write-output "$vmname shutting down" (get-date) } Remove-VM $vm -DeleteFromDisk:$true
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference