Update VMtools a vHW

Tento script som vytvoril keď som potreboval premigrovať VMs na nové ESX hosty a následne na daných VMs updateovat tools a virtuálny HW. Ak používate vSphere Update Manager, tak tento script Vám pravdepodobne bude slúžiť iba ako inšpirácia. :)

Priebeh scriptu:

  • vypne VM
  • vyrobí snapshot
  • presunie ju na ESX host zadaný v $Destination
  • zapne VM
  • po naštartovaní VMtools začne ich update
  • script VM reštartuje
  • po reštarte ju vypne
  • upgrade vHW
  • zapne VM

Odporúčam tento script spúšťať z PowerCLI konzoly alebo PowerGUI alebo Powershell ISE alebo niečoho podobného. Budete tak môcť sledovať priebeh migrácie.

Connect-VIServer -Server vc.yourcompany.com

$servers = Get-Content C:\Servers.csv | Select-Object -Skip 1
$VMs = Get-VM $servers
$Destination = "esx1.yourcompany.com"
$vHW = "v9"

Function CheckRunningTools
{
param ([string] $VM)

while ((Get-VM $VM).ExtensionData.Guest.ToolsRunningStatus -ne "guestToolsNotRunning")
{
(Get-VM $VM).ExtensionData.UpdateViewData("Guest.ToolsRunningStatus")
Start-Sleep -Seconds 2
}
Write-Host "VMTools uz niesu na $VM spustene"
}

Function CheckGuestOperationsReady
{
param ([string] $VM)

while ((Get-VM $VM).ExtensionData.Guest.GuestOperationsReady -ne $true)
{
Start-Sleep -Seconds 2
(Get-VM $VM).ExtensionData.UpdateViewData("Guest.GuestOperationsReady")
}
Write-Host "$VM ma GuestOperationsReady hodnotu True "
}

$VMs | ForEach-Object {
Write-Host "Vypina sa $($_)."
$a = Get-Date
Write-Host "$($a.ToString("dd.MM.yyyy hh:mm:ss")) zacal proces migracie servera $($_) na nove ESX hosty"
Shutdown-VMGuest -VM $_ -confirm:$false | Out-Null
while ($_.ExtensionData.Runtime.PowerState -eq "poweredOn"){
Start-Sleep -Seconds 2
$_.ExtensionData.UpdateViewData("Runtime.PowerState")
}
Write-Host "$($_) je vypnuty a pripraveny na snapshot a migraciu na novy host."
New-Snapshot -VM $_ -Name "Before vHW and VMTools update" | Out-Null
Write-Host "Snapshot na $($_) bol vytvoreny."
Move-VM -VM $_ -Destination $Destination | Out-Null
Write-Host "$($_) bol presunuty na $((Get-VM $_.Name).Host) a spusta sa."
Start-VM -VM $_ | Wait-Tools | Out-Null
Write-Host "Zacina samotny update VMTools."
Update-Tools -VM $_
CheckRunningTools -VM $_.Name
Write-Host "VMTools na $($_) sa updateovali a server sa restartuje."
CheckGuestOperationsReady -VM $_.Name
Shutdown-VMGuest -VM $_ -confirm:$false | Out-Null
while ((Get-VM $_.Name).ExtensionData.Runtime.PowerState -eq "poweredOn"){
Start-Sleep -Seconds 2
$_.ExtensionData.UpdateViewData("Runtime.PowerState")
}
Write-Host "$($_) je vypnuty."
Set-VM -VM $_.Name -Version $vHW -Confirm:$false | Out-Null
Write-Host "$($_) ma vHW verzie $((Get-VM $_.Name).Version)."
Start-Sleep -Seconds 5
Start-VM -VM $_.Name | Out-Null
Write-Host "$($_) sa startuje a ma VMTools so statusom: $((Get-VM $_.Name).ExtensionData.Guest.ToolsVersionStatus2) a vHW verzie $((Get-VM $_.Name).Version)."
$b = Get-Date
$c = ($b - $a).TotalMinutes
Write-Host "$($b.ToString("dd.MM.yyyy hh:mm:ss")) sa ukoncil proces migracie servera $($_) na nove ESX hosty a trval $($c) minut."
"$($b.ToString("dd.MM.yyyy hh:mm:ss")) sa ukoncil proces migracie servera $($_) na nove ESX hosty a trval $($c) minut." | Add-Content C:\Scripts\Exports\VMsMigration\VMsMigration.txt
}

Author: Martin

Infrastructure engineer | virtualization & cloud enthusiast | vSphere specialist | blogger | VMware vExpert 2017, 2018, 2019, 2020 | VMCE | Slovak VMUG Leader |  user group ambassador for kmug.sk | husband&father

Leave a Reply

Your email address will not be published. Required fields are marked *