VMware Cloud Community
vishaln
Enthusiast
Enthusiast

How to unregister and register Virtual machine vcenter 6.7 via PowerCLI

I have list of VMs that have rename VM name from console by user, that VM name is different in datastore

I want to unregister and register with same name state as user kept (they don't come to know the change done by backend)

Please help me with the one line PS command.

The regions of doing this some VMs are not getting power on due to we have clean the metadata corruption with the help of VMware support team, below are the issue.

Issue:               Cluster2 Virtual machines power on failed with error "Transport (VMDB) error -45: Failed to connect to peer process".

Issue idetified:  Running consistency checks on the devices let out about known issue with VMFS 6, metadata corruption

Resolutions:     Run by Vmware Support team VOMA advanced fix in order to fix the metadata corruption

8 Replies
LucD
Leadership
Leadership

You could do something like this

$vm = Get-VM -Name MyVM

$rp = Get-ResourcePool -VM $vm

Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false

New-VM -VMFilePath $vm.ExtensionData.Config.Files.VmPathName -ResourcePool $rp -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vishaln
Enthusiast
Enthusiast

Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false

'DeletePermanently' means it will delete VM or it will remove from inventory?

Also, I want VM name which renamed by user should be same

0 Kudos
LucD
Leadership
Leadership

With the $false value it will only unregister the VM.

Not sure what you mean by "Also, I want VM name which renamed by user should be same"

The unregister/register steps do not change the DisplayName of the VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Zsoldier
Expert
Expert

If it's just a matter of having the datastore vm foldername match the display name of the VM object in vCenter, you can do a simple storage vMotion to make that happen while the VM is online.  Assuming this advanced setting is still required if not default anymore:  VMware Knowledge Base

LucD​'s solution will simply reregister VM to vCenter to the existing path, but will not match display name to datastore path of VM.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
vishaln
Enthusiast
Enthusiast

Yes, I am looking for datastore VM folder name should match with re-register vmx, which not matching with vmx file name.

0 Kudos
LucD
Leadership
Leadership

Then you will need a svMotion, which requires a datastore to which you can move the VM.

Are there spare datastores?

Do you use a Datastore Cluster?
Are there affinity rules in play?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ARJNIG123
Contributor
Contributor

Hi LucD:

Sorry, picking up on an old thread and also I am not savy with PowerCLI so please pardon if my question is fundamentally flawed.

"$vm = Get-VM -Name MyVM" This works when VM is registered. Correct?

Therefore, for unregistering a VM "Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false" will work.

However, when VM is already unregistered, "$vm = Get-VM -Name MyVM" will probably not work. Then while registering that VM, how the extension data will come in "New-VM -VMFilePath $vm.ExtensionData.Config.Files.VmPathName -ResourcePool $rp -Confirm:$false"?

I am interested in a scripted method of registering multiple VMs from different datastores either on one host or multiple hosts of same cluster (if we can use cluster instead of VMHost). My scenario is I have multiple Active-Active pairs of Datastores. VMs are registered on one site. We failover by unregistering VMs > Go to other site > Browse Datastore and register VM. I came across a function that gives me all VMX File paths for all VMs I am interested in. However, so far I have been able to use vmx file path '[DatastoreName] VMName/VMName.vmx' in a one liner (per VM) format -- New-VM -VMHost xxxxxxx.domain.com -VMFilePath '[DatastoreName] VMName/VMName.vmx'

The function provides path for multiple VMs as follows:

'[Datastore1Name] VM1Name/VM1Name.vmx'

'[Datastore1Name] VM2Name/VM2Name.vmx'

'[Datastore2Name] VM3Name/VM3Name.vmx'

I intend to simply use this information in a way which is not one-line command at a time (per-VM).

 

 

I tried something like creating a text file with vmx file paths as:

'[Datastore1Name] VM1Name/VM1Name.vmx'

'[Datastore1Name] VM2Name/VM2Name.vmx'

'[Datastore2Name] VM3Name/VM3Name.vmx'

 

And then do Get-Content to get all paths and then run Foreach loop for each path providing single ESXi host like below:

$VMXpaths = Get-Content H:\Temp\VMXPaths.txt

Foreach ($VMXpath in $VMXpaths){
   New-VM -VMHost xxxxxx.domain.com -VMFilePath $VMXpath
}

But no luck.

Please advise what you think can be done here.

 

 

Thanks.

 

0 Kudos
LucD
Leadership
Leadership

That should work.

You are connected to a vCenter I assume?
Check what this returns

$global:defaultVIServers


Start by looking at the .TXT file.
Does this return the paths, one per line, no blank lines?

Get-Content H:\Temp\VMXPaths.txt

Do you get any error messages when running the script?
How did you run the script?
Any entries in the vCenter Tasks/Events? Check via the Web Client.




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos