Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Created January 14, 2018 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techthoughts2/5d886f614f88fed6cac732a88a47c7ad to your computer and use it in GitHub Desktop.
Save techthoughts2/5d886f614f88fed6cac732a88a47c7ad to your computer and use it in GitHub Desktop.
tbd
function Move-VMsToPreferredNodes {
try {
#lets make sure the c:\rs-pkgs folder exists
#if it does not we will create it
$TARGETDIR = 'C:\rs-pkgs'
if (!(Test-Path -Path $TARGETDIR)) {
New-Item -ItemType directory -Path $TARGETDIR
}
#now we will identify all HA VMs
$haVMs = get-vm | ? { $_.IsClustered -eq $true }
foreach ($vm in $haVMs) {
#lets loop through all HA VMs and find their current location and their preferred owner node
$preferredObject = Get-ClusterGroup $vm.name | Get-ClusterOwnerNode | Select-Object -ExpandProperty OwnerNodes
$currentLocation = Get-ClusterGroup $vm.name | Select-Object -ExpandProperty OwnerNode
if ($preferredObject -ne $null) {
#if there is a preferred owner set we will see if we are currently on it
if ($currentLocation -eq $preferredObject.Name) {
#we are already on the preferred owner, no action necessary
$log = Write-Host $vm.name " is on its preffered node of " $preferredObject.Name
$log | Out-File C:\rs-pkgs\Move-VMsToPreferredNodes.txt -Append
}
else {
#we are not on the preferred owner, initia LM to the preferred owner node ONLY if it is up
if ($preferredObject.State -eq "Up") {
$log = $vm.Name + " currently on " + $currentLocation + " which is not its preferred owner"
$log += "Moving " + $vm.name + " to it's preffered node " + $preferredObject.Name
Get-VM $vm.name | Move-ClusterVirtualMachineRole -MigrationType Live -Node $preferredObject.Name -Wait 0
$log | Out-File C:\rs-pkgs\Move-VMsToPreferredNodes.txt -Append
}
else {
#the preferred owner isn't up - take no action
$log = $vm.Name + " currently on " + $currentLocation + " which is not its preferred owner"
$log += " No action was taken though because the preferred owner is not up right now."
$log | Out-File C:\rs-pkgs\Move-VMsToPreferredNodes.txt -Append
}
}
}
else {
#no action taken here because the VM has no preferred owner set
$log = $vm.Name + " does not have a preferred owner set"
$log | Out-File C:\rs-pkgs\Move-VMsToPreferredNodes.txt -Append
}
}
}
catch {
$_ | Out-File C:\rs-pkgs\Move-VMsToPreferredNodes.txt -Append
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment