Skip to content

Instantly share code, notes, and snippets.

@lrivallain
Last active January 16, 2017 16:31
Show Gist options
  • Save lrivallain/d47d768177c7986e978b89e92065ac66 to your computer and use it in GitHub Desktop.
Save lrivallain/d47d768177c7986e978b89e92065ac66 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Windows 8 and Windows 2012 Server or later virtual machines fail upon reboot (2092807)
https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2092807
Resolution: To _work around_ this issue, update the VM advanced configuration to add monitor_control.enable_softResetClearTSC = TRUE
.NOTES
Author : Ludovic Rivallain
Version : 1.0
#>
Param(
[Parameter(Mandatory=$True)]$VCServer, # The vCenter hostname
[Parameter(Mandatory=$True)]$VCUser, # The vCenter username to connect
[Parameter(Mandatory=$True)]$VCPassword, # The vCenter password to connect
[Parameter(Mandatory=$True)]$SleepTime, # A sleep duration to wait between two new migration tasks
[Parameter(Mandatory=$False)]$VMName # A VM name to filter on (for test runs)
)
# Import PowerCLI commands
If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))
{ Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop }
Catch { Write-host "Unable to load PowerCLI, is it installed?" -ForegroundColor Red; Exit }
}
# Connect to the vCenter
Try{
Connect-VIServer $VCServer -User $VCUser -password $VCPassword -ErrorAction Stop | Out-Null
} Catch {
Write-Host "Unable to connect to the server $VCServer" -ForegroundColor Red ;
Exit
}
## new property for VMHost
New-VIProperty -ObjectType VMHost -Name MemoryFreeGB -Value {[math]::Round(($Args[0].MemoryTotalGB - $Args[0].MemoryUsageGB),2)} -Force | Out-Null
# If specified, filter VM on a specific VMName
[array]$vms = @()
if ($VMName) {
[array]$vms = @(Get-VM | Where-Object { $_.Name -like "*$VMName*" })
} else {
[array]$vms = @(Get-VM)
}
$count = $vms.Count
Write-Host "NB VM found with/without filter : $($vms.Count)"
Write-Host "Press enter to continue and CTRL-C to exit ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# Loop on each VM of the result
ForEach ($vm in $vms) {
$vmv = Get-VM $vm | Get-View
$name = $vmv.Name
Write-Host "Testing VM $name"
$guestid = $vmv.Summary.Config.GuestId
$state = $vmv.Summary.Runtime.PowerState
$vmx = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmx.extraConfig += New-Object VMware.Vim.OptionValue
$vmx.extraConfig[0].key = "monitor_control.enable_softResetClearTSC"
$vmx.extraConfig[0].value = "TRUE"
if ($guestid -like "windows8*Guest") {
Write-Host "--> Applying change on VM $name"
($vmv).ReconfigVM_Task($vmx) | Out-Null
if ($state -eq "poweredOn") {
Write-Host " --> As VM $name was poweredOn, migrate it to apply changes"
# find out the cluster of the VM
$cluster = $vm.VMHost.Parent
# find all other hosts of the cluster
$otherhosts = Get-VMHost | Where-Object {($_.Name -ne $vm.VMHost.Name)}
# get a new host based on free memory
$targethost = $($otherhosts | Sort-Object -property MemoryFreeGB -Descending)[0]
Write-Host " --> VM will be migrated to host $($targethost.name) according to the free memory"
# migrate
$vmv.MigrateVM_Task($null, $targethost.ExtensionData.MoRef, 'highPriority', $null) | Out-Null
Write-Host " --> Sleep for $SleepTime before migrating a new VM"
Start-Sleep -s $SleepTime
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment