Posted in
Virtual Machine Manager,
Windows Powershell |
No Comment | 2,667 views | 15/08/2013 10:25
You may export and move a VM to another host or volume, or maybe you may delete a VM but configuration and VHD files may still exist on old location. In that case, you can use this script to find that old VM directories.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| # Get Cluster Nodes
$ClusterNodes = "Cluster01Node01","Cluster02Node02"
foreach ($ClusterNode in $ClusterNodes)
{
$Volumes = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\
foreach ($Volume in $Volumes)
{
$ConfigFolders = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\$Volume
foreach ($ConfigFolder in $ConfigFolders)
{
$Result = "0";
$VMName = "";
$ClusterNodes = Get-Cluster $ClusterNode | Get-ClusterNode
foreach ($ClusterNode in $ClusterNodes)
{
$VMs = Get-VM -ComputerName $ClusterNode
foreach ($VM in $VMs)
{
$FullName = $ConfigFolder.FullName
$FullPath = "C:\" + $FullName.Split("\")[4] + "\" + $FullName.Split("\")[5] + "\" + $FullName.Split("\")[6]
if ($FullPath -like $VM.ConfigurationLocation)
{
$Result = 1;
}
$VirtualDisks = Get-VM -ComputerName $ClusterNode -Name $VM.Name | Get-VMHardDiskDrive
foreach ($VirtualDisk in $VirtualDisks)
{
if ($VirtualDisk.Path -like "$FullPath*")
{
$Result = 1;
}
}
}
}
if ($Result -ne "1")
{
Write-Warning $ConfigFolder.FullName
}
}
}
} |
# Get Cluster Nodes
$ClusterNodes = "Cluster01Node01","Cluster02Node02"
foreach ($ClusterNode in $ClusterNodes)
{
$Volumes = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\
foreach ($Volume in $Volumes)
{
$ConfigFolders = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\$Volume
foreach ($ConfigFolder in $ConfigFolders)
{
$Result = "0";
$VMName = "";
$ClusterNodes = Get-Cluster $ClusterNode | Get-ClusterNode
foreach ($ClusterNode in $ClusterNodes)
{
$VMs = Get-VM -ComputerName $ClusterNode
foreach ($VM in $VMs)
{
$FullName = $ConfigFolder.FullName
$FullPath = "C:\" + $FullName.Split("\")[4] + "\" + $FullName.Split("\")[5] + "\" + $FullName.Split("\")[6]
if ($FullPath -like $VM.ConfigurationLocation)
{
$Result = 1;
}
$VirtualDisks = Get-VM -ComputerName $ClusterNode -Name $VM.Name | Get-VMHardDiskDrive
foreach ($VirtualDisk in $VirtualDisks)
{
if ($VirtualDisk.Path -like "$FullPath*")
{
$Result = 1;
}
}
}
}
if ($Result -ne "1")
{
Write-Warning $ConfigFolder.FullName
}
}
}
}
$ClusterNodes should contain different Cluster’s only one nodes. Using with that node name, we can query other nodes too.