This small post will illustrate how we can easily get all virtual machine information of a specific cluster with this function. I used this function to locate on which node a VM was actually located.
How to get List of Virtual Machines from a hyperv cluster using powershell:
This is can be pretty helpful because it will allow you to list all the VM’s that are currently present on a specific node.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
Function Get-VMInformationFromCluster {
Param(
[Parameter(Mandatory=$false)]
[string[]]$ClusterName=$env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[validateScript({
(get-item $_).psIscontainer
})]
[String]$ExportFolderPath = (split-path -path $MyInvocation.scriptname -Parent)
)
import-module FailoverClusters -Force
foreach ($ClusterN in $ClusterName){
try {
$Cluster = get-cluster -name $ClusterN
}catch{
Write-Error “Could not get cluster information.”
write-error $_
exit
}
if (!($Cluster)){
write-host “$_”
exit
}
$Nodes = Get-ClusterNode -Cluster $Cluster.Name
write-host “Cluster $($Cluster.Name) contains $($Nodes.count) nodes.”
foreach ($Node in $Nodes){
if (!($node)){
write-verbose “Cluster node name: $($node) is not recognized. Skipping.”
continue
}
write-host “Gathering information from $($node.name) cluster node.”
[array]$VMs = get-vm -ComputerName $Node.Name
$ExportPath = (join-path -path $ExportFolderPath -childpath $($Node.Name)) + “.csv”
write-verbose “Node $($Node.name) has $($vms.count) virtual machine(s).”
write-verbose “Exporting Cluster information to $($exportPath).”
$VMS | export-csv -Path $ExportPath -NoTypeInformation -Force
write-verbose “Listing VM names present on cluster node: $($node.Name)”
if ($PSCmdlet.InvokeCommand)
foreach ($vm in $VMS){
if (!($vm)){
write-verbose “The current VM: $($vm.name) is not recognized. Skipping.”
continue
}else{
write-verbose “VM -> $($vm.Name) state -> $($vm.State)”
}
}#End foreach VM
}#EndForeach ClusterNode
}#EndForeachClusterN
}
|
Leave A Comment