Home A PowerShell script to find and Fix Orphaned VMs
Post
Cancel

A PowerShell script to find and Fix Orphaned VMs

This script will find and fix “orphaned” VMs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$vCenter = "192.168.1.XX" # Vcenter FQDN or IP
$vCUser = "vsphere.local\administrator"
$vCPW = "password"

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

#Get-VM | Select-Object "*"
#exit

$VMHost = "192.168.1.XX" # What host create the VM on

Get-VM | Where-Object{$_.ExtensionData.Runtime.ConnectionState -eq "orphaned"} |
  ForEach-Object -Process {
    Write-Host "Fixing $_"
	Remove-VM -VM $_ -Confirm:$false | Out-Null
	New-VM -Name $_ -VMHost $VMHost -Confirm:$false | Out-Null
	#Start-VM -VM $_ -Confirm:$false
}

Disconnect-VIServer -Server $vCenter -Force -Confirm:$false
This post is licensed under CC BY 4.0 by the author.