PowerCLI vMotion Enable Easy Switcher Script
Have you found yourself in the position of needing to change which vmkernel ports vMotion is enabled on? Maybe you have to storage vMotion a VM to a different cluster and need to make sure that vMotion is on the management interface. We have all run into issues with dedicated vMotion networks and storage vMotion to another cluster. Even when you enable this on the management interface if you don’t uncheck it from the vMotion vmkernel ports, you will see it fail.
PowerCLI to switch vmkernel ports that vMotion is enabled on
I had a need to make sure this was easy to switch back and forth for a particular use case I ran into. PowerCLI of course is our friend if we want to do things quickly, easily, and consistently. So, take a look at the PowerCLI script below.
Notables:
- It will first ask which cluster or standalone host you want to work with
- It will ask for user input to ask whether you want to enable vMotion on the Management
- It will ask you to type Management or vMotion, which signifies which vmkernel-enabled port groups you want to enable for vMotion
- Once you select, it will begin its operation
# Prompt the user for the cluster or standalone host name
$targetName = Read-Host "Enter the name of the cluster or standalone host"
# Attempt to get the cluster or host based on the input
$cluster = Get-Cluster -Name $targetName -ErrorAction SilentlyContinue
$esxiHost = Get-VMHost -Name $targetName -ErrorAction SilentlyContinue
# Determine the target hosts
if ($cluster) {
$esxiHosts = Get-VMHost -Location $cluster
Write-Host "Applying changes to all hosts in cluster '$($cluster.Name)'."
} elseif ($esxiHost) {
$esxiHosts = @($esxiHost)
Write-Host "Applying changes to standalone host '$($esxiHost.Name)'."
} else {
Write-Host "No cluster or host found with the name '$targetName'. Exiting script."
exit
}
# Prompt the user for their choice of interface for vMotion
$choice = Read-Host "Which interfaces do you want vMotion enabled on? (Management or vMotion)"
# Convert the choice to lowercase for consistent comparison
$choice = $choice.ToLower()
# Valid choices are "management" or "vmotion"
foreach ($esxHost in $esxiHosts) {
# Get management VMkernel adapters based on PortGroupName
$managementAdapters = Get-VMHostNetworkAdapter -VMHost $esxHost -VMKernel |
Where-Object { $_.PortGroupName -like "Management*" }
# Get VMkernel adapters associated with vMotion (either by name or if vMotion is already enabled)
$vmotionAdapters = Get-VMHostNetworkAdapter -VMHost $esxHost -VMKernel |
Where-Object { $_.PortGroupName -like "vmotion*" -or $_.VMotionEnabled -eq $true }
if ($choice -eq "management") {
# Enable vMotion on management adapters
foreach ($adapter in $managementAdapters) {
$adapter | Set-VMHostNetworkAdapter -VMotionEnabled $true -Confirm:$false
Write-Host "Enabled vMotion on management adapter '$($adapter.Name)' on host '$($esxHost.Name)'."
}
# Disable vMotion on vMotion adapters
foreach ($adapter in $vmotionAdapters) {
$adapter | Set-VMHostNetworkAdapter -VMotionEnabled $false -Confirm:$false
Write-Host "Disabled vMotion on adapter '$($adapter.Name)' (PortGroup '$($adapter.PortGroupName)') on host '$($esxHost.Name)'."
}
} elseif ($choice -eq "vmotion") {
# Enable vMotion on vMotion adapters
foreach ($adapter in $vmotionAdapters) {
$adapter | Set-VMHostNetworkAdapter -VMotionEnabled $true -Confirm:$false
Write-Host "Enabled vMotion on adapter '$($adapter.Name)' (PortGroup '$($adapter.PortGroupName)') on host '$($esxHost.Name)'."
}
# Disable vMotion on management adapters
foreach ($adapter in $managementAdapters) {
$adapter | Set-VMHostNetworkAdapter -VMotionEnabled $false -Confirm:$false
Write-Host "Disabled vMotion on management adapter '$($adapter.Name)' on host '$($esxHost.Name)'."
}
} else {
Write-Host "Invalid choice '$choice'. Please enter 'Management' or 'vMotion'."
exit
}
}
Below is a screenshot of switching from the dedicated vMotion network vmkernel ports over to Management for enabling vMotion:
Then we can turn around and switch everything back to the vMotion ports:
Wrapping up
Let me know if this script is helpful to you. I just wanted to share this with the community as I found it helpful to automate this process to switch between management and vMotion port groups. One of the things I hate to have to do is manually update vmkernel ports through the vSphere client, unticking boxes, etc, as this is tedious.