Automation

 View Only
Expand all | Collapse all

Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

  • 1.  Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 08:07 AM

    Is there a powershell script that can "fix" orphaned VMs?

    We have 2 big environments, one with 190 hosts, the other with 140 hosts

    both of the environments had "partially" crashed, and caused many hosts to reboot.

    the reboots caused many VMs (1800+) to get in 'Orphaned' states.

    I'm working on a powershell script which will eventually do the following:

    search over each host for the 'Orphaned' VMs, save the names of the VMs + Save the parent host name\IP (in a side variable, which will be used later)

    Remove the 'Orphaned' VMs from inventory

    Go to the DS, search the host name\IP that saved (to a side variable) from before, look for the VM name (of the orphaned that removed from inventory) register back the .vmx file 'Register VM...'

    if any one could help with that it will be real amazing!

    Thanks in advance!



  • 2.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 08:14 AM

    Can't you just
    - find the orphaned VMs
    - get/save the path of the VMX file
    - use Remove-VM (without the DeletePermanently switch) to unregister them
    - register them with New-VM and the VMFilePath parameter using the VMX path you saved in step 2



  • 3.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 08:20 AM

    Hi LucD, Thanks for Replying so fast!

    Yes that is basically what I need, but before I start deleting 

    I'm having a little struggle to get/save the path of the VMX files of the orphaned VMs

    for example, I find the orphaned VMs like this 

    $orphanedVMS = Get-VM -Name * | ?{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} 

    and that gives me a lot of information, and I want to reduce the information of the VMs to only the VM.name and parent Host.name

    its a lot of hosts, and a lot of VMs on each host, and I would like to register back the exact VMs that has been removed from each specific host



  • 4.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 08:27 AM

    You can find teh VMX path under the ExtensionData property, see Solved: Re: Get VMx path and then reregister - VMware Technology Network VMTN
    The VMHost parameter is not needed on the New-VM cmdlet when you use the VMFilePath parameter.
    That info is available in the VMX file and will be used when registering the VM



  • 5.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 09:05 AM

    Could you help me with this script?

    each time I run it, its either doesn't save the names hence cant register back

    $vCenter = "vc"
    $vCUser = "user"
    $vCPW = "pass"

    Connect-VIServer $vCenter -User $vCUser -Password $vCPW -Force

    $ResourcePool = "hostname"

    $VMHost = Get-VMHost -Name $ResourcePool | Get-VM -Name * | ?{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} | Select-Object Name, @{N = 'VMX'; E = { $_.ExtensionData.Summary.Config.VmPathName}}

    Remove-VM -VM $VMHost.Name -Confirm:$false

    New-VM -Name $VMHost.Name -ResourcePool $ResourcePool

    Disconnect-VIServer -Server $vCenter -Force -Confirm:$false



  • 6.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)
    Best Answer

    Posted Jun 28, 2022 09:58 AM

    There are probably more than 1 VM on that ESXi node, hence $VMHost is an array.

    Why not try like this?

     

     

    $vCenter = "vc"
    $vCUser = "user"
    $vCPW = "pass"
    
    Connect-VIServer $vCenter -User $vCUser -Password $vCPW -Force
    
    $ResourcePool = "hostname"
    Get-VMHost -Name $ResourcePool | Get-VM | 
    Where-Object{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} | 
    ForEach-Object -Process {
      Remove-VM -VM $_.Name -Confirm:$false
      New-VM -Name $_.Name -VMHost $_.VMHost -VMFilePath $_.ExtensionData.Summary.Config.VmPathName -Confirm:$false
    }
    
    Disconnect-VIServer -Server $vCenter -Force -Confirm:$false

     

     



  • 7.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 10:25 AM

    Thanks! 

    but still having issue with register back New-VM:

    New-VM Please specify at least one of the following parameters: "ResourcePool" or "VMHost".

     

    edit:

    actually it looks like I fixed with removing the VMFilePath switch, and just replace with the resourcepool like so

    New-VM -Name $_.Name -ResourcePool $ResourcePool -Confirm:$false

    I think because its already in the loop so it does register within the info in the VMX file of the path like you said previously.

    so it looks like its working perfectly!

     

    only one more request if you can please, this scrips is meant for each host...

    could you possibly help me out to make such script but for full vc site? and not only for each host seperatly?

    but with same methods, for it to register back to each specific host it was removed from?

    if it possible then thanks a lot!!

     



  • 8.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 11:03 AM

    For re-registring you need to use the VMFilePath parameter.
    All other parameters in the RegisterVM parameter set are optional.
    Not sure how you used the script or the exact error you are getting.



  • 9.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 11:43 AM

    this is what I use:

     

    $vCenter = "vc"

    $vCenter = "vc"
    $vCUser = "user"
    $vCPW = "pass"
    
    Connect-VIServer $vCenter -User $vCUser -Password $vCPW -Force
    
    $ResourcePool = "hostname"
    
    Get-VMHost -Name $ResourcePool | Get-VM |
    Where-Object{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} |
    
    ForEach-Object -Process {
    Remove-VM -VM $_.Name -Confirm:$false
    New-VM -Name $_.Name -ResourcePool $ResourcePool -Confirm:$false
    #Start-VM -VM $_.Name -Confirm:$false
    }
    
    Disconnect-VIServer -Server $vCenter -Force -Confirm:$false

     

    and its working good I've tested it few times now

     



  • 10.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 11:45 AM

    That is not what I posted



  • 11.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:05 PM

    I Know its not, like I said before, with what you posted, I've got error on New-VM 



  • 12.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:08 PM

    Did you include the VMFilePath and VMHost parameter (see updated code above)?



  • 13.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:09 PM

    Your code is creating a new VM, not re-registering an existing VM



  • 14.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:15 PM

    Yes, I've updated to your now again:

    Get-VMHost -Name $ResourcePool | Get-VM | 
    Where-Object{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} |
    
    ForEach-Object -Process {
        Remove-VM -VM $_.Name -Confirm:$false
        New-VM -Name $_.Name -VMHost $vm.VMHost -VMFilePath $_.ExtensionData.Summary.Config.VmPathName -Confirm:$false
       
    }

     

    And its still showing the error: 

    New-VM : 6/28/2022 3:12:49 PM	New-VM		Please specify at least one of the following parameters: "ResourcePool" or "VMHost".


  • 15.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:18 PM

    My bad, that should have said -VMHost $_.VMhost not -VMHost $vm.VMHost.
    I corrected the code above.



  • 16.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 28, 2022 12:23 PM

    Yes I see it now and I actually changed it too my self a moment ago

    since the other statements was also $_. 

    so cool thanks a lot look like it works perfectly!



  • 17.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 06:55 AM

    Hi Again LucD,

    got another small question.

    for the machines I've mistaken instead of 'Register' I've 'Created' new machines.

    to fix that, I can just change the 

    Where-Object{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"}

    to something like that:

    Where-Object{$_.PowerState -eq 'PoweredOff'}

     

    since it created the VMs and left off, I can look for the powered off on each Host, remove from inventory and register back with the right VMX file?

    or since it's already created new VM it will be a problem?



  • 18.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 07:15 AM

    Are you sure these VMs are registered?
    If not, they are in fact orphaned VMs that do not appear with a Get-VM.
    You could use my Orphaned files and folders – Spring cleaning post to find those.



  • 19.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 07:23 AM

    I'm not sure they are registered, this was before you said "Your code is creating a new VM, not re-registering an existing VM

    so I think it did not re-register an existing VM it just created new VMs with the same names

    so my question is, is it enough just to change the 'Where-Object' for the PoweredOff state?

    I'm asking that, because I did try it with the PoweredOff state, it only removed the new created VMs 

    but did not register back... so perhaps I'm doing something wrong?



  • 20.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 07:27 AM

    This should be a 2-step process.
    1. Remove the incorrect new VMs. If these are registered you can just use Remove-VM with the DeletePermanently switch
    2. Register the VMX files from the original list of VMs



  • 21.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 07:40 AM

    Removing with DeletePermanently  switch means it will also remove the incorrect VMX file from the DS?

    $ResourcePool = "hostname"
    
    Get-VMHost -Name $ResourcePool | Get-VM | 
    Where-Object{$_.PowerState -eq 'PoweredOff'} |
    
    ForEach-Object -Process {
        Remove-VM -VM $_.Name -Confirm:$false
        New-VM -Name $_.Name -VMHost $_.VMHost -VMFilePath $_.ExtensionData.Summary.Config.VmPathName -Confirm:$false
    }

     

    This is the script I'm trying to use to fix the new created VMs instead of Registered...
    for example:
    on host 1, there was vm1(orphaned), vm2(orphaned), vm3(orphaned)

    I accidentally used the wrong script, which removed the VMs, but instead of re-registering, I've created new VMs now I have on host 1 VMs - vm1, vm2 vm3, not orphaned.

    I tried to remove them (without DeletePermanently ) and re-register with the script above.

    it did removed them, but did not re-registered them back from the location of the VMX.

    Is removing them permanently with DeletePermanently  will do the trick, and re-register them from the correct path of VMX?



  • 22.  RE: Remove 'Orphaned' VMs, and Register Back (Fix Orphaned VMs)

    Posted Jun 30, 2022 07:46 AM

    Yes, the DeletePermanently switch will make sure all files of that specific VM are removed.

    Then your original VMs, which you unregistered, are still there, just not registered, and thus not visible in vCenter.
    With the script I pointed to earlier, you can find these "orphaned" VMs.
    Once you have the VMX files for these VMs, you can register them with the New-VM cmdlet and the VMFilePath parameter.
    This will have to be a new script, the previous one assumed the VMs were still registered.

    The VMs will then be registered, and visisble in the vCenter.