/
EnableOrDisableCPUCompatibilityOnHyperVCluster.ps1
68 lines (65 loc) · 2.15 KB
/
EnableOrDisableCPUCompatibilityOnHyperVCluster.ps1
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
68
[CmdletBinding()]
param (
# Put your Clusters FQDN or NetBIOS name here
[Parameter(
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[String]
$HyperVClusterName = "CLUSTER.DOMAIN.LOCAL",
# Use "Enable" or "Disable" to set the CompatibilityForMigration feature
[ValidateSet("Enable","Disable")]
[String]
$CpuCompatibilityMode = "Disable"
)
"Getting all VMs in Cluster..."
$AllVms = Get-VM –ComputerName (Get-ClusterNode –Cluster $HyperVClusterName)
If($AllVms.Count -lt 1)
{
Throw "No VMs found in Cluster $HyperVClusterName!"
}
else {
If($CpuCompatibilityMode -eq "Enable")
{
"Enabling CompatibilityForMigrationEnabled..."
ForEach($VM in ($AllVMs | Where-Object {($_ | Get-VMProcessor).CompatibilityForMigrationEnabled -eq $false}))
{
"$($VM.VMName)..."
$flagToStartVM = $false
If($VM.State -ne "Off")
{
$flagToStartVM = $true
$VM | Stop-VM
}
$VM | Set-VMProcessor -CompatibilityForMigrationEnabled $true
If($flagToStartVM)
{
$VM | Start-VM
}
}
}
elseif($CpuCompatibilityMode -eq "Disable")
{
"Disabling CompatibilityForMigrationEnabled..."
ForEach($VM in ($AllVMs | Where-Object {($_ | Get-VMProcessor).CompatibilityForMigrationEnabled -eq $true}))
{
"$($VM.VMName)..."
$flagToStartVM = $false
If($VM.State -ne "Off")
{
$flagToStartVM = $true
$VM | Stop-VM
}
$VM | Set-VMProcessor -CompatibilityForMigrationEnabled $false
If($flagToStartVM)
{
$VM | Start-VM
}
}
}
else
{
Throw "No or wrong value for CpuCompatibilityMode provided - use ""Enable"" or ""Disable"""
}
$AllVms = Get-VM –ComputerName (Get-ClusterNode –Cluster $HyperVClusterName)
$AllVms | Select-Object ComputerName,VMName,@{label="CompatibilityForMigrationEnabled"; expression={($_ | Get-VMProcessor).CompatibilityForMigrationEnabled}}
}