SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

View All Comments

powercliupgradehwlevel9

Script: PowerCLI: Upgrade to Hardware Level 9

This script will automatically upgrade VMs from a textfile to hardware level 9. It was based on Script: PowerCLI: Upgrade VMs to New Tools and Hardware Level Version

Script

# This script will upgrade the VMWare hardware level from VMs listed in a simple input file
# Input file should be a simple text file with one VM name per line
 
Function Connect-vCenter{
# Connect to vCenter
write-host -foregroundcolor yellow `n  "In which datacenter will you upgrade VMs? You will automatically be connected to the correct vCenter."
write-host -foregroundcolor yellow `n  "1: Site1"
write-host -foregroundcolor yellow `n  "2: Site2"
$vcenter = read-host
     while (($vcenter -gt 2) -or ($vcenter -lt 1)){
          write-host -foregroundcolor red "Not a valid choice. Please just enter a number from the list"
          $vcenter = read-host    
    }
     if ($vcenter -eq 1){
          Connect-VIServer -server site1.getshifting.com -ErrorAction Stop}
    else {
          Connect-VIServer -server site2.getshifting.com -ErrorAction Stop}
}
 
 
Function VM-Selection{
   $sourcename = Read-Host "Give the name of inputfile (full path) with VMs you want to upgrade"
      if((Test-Path -path $sourcename) -eq $True){
         $abort = Read-Host "You wil now upgrade all VMs in: $sourcename, this is your last chance to abort by pressing <CTRL>+C. Press <ENTER> to continue."
         $list = Get-Content $sourcename | Foreach-Object {Get-VM $_ | Get-View | Where-Object {-not $_.config.template} | Select Name }
		 $vms = $list
      }
      else{
         Write-Host "$sourcetype does not exist. Exit the script by pressing <CTRL>+C and try again."
      }
   return $vms
}
 
Function PowerOn-VM($vm){
   Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null
   Write-Host "$vm is starting!" -ForegroundColor Yellow
   sleep 10
 
   $toolsstatus = $null
   $teller = 0
 
   while (($toolsstatus -ne "toolsOk") -and ($teller -ne 10)){
	  sleep 15
	  $vmview = get-VM $vm | Get-View
          $toolsstatus = $vmview.Guest.ToolsStatus
 
	  Write-Host "$vm is starting, toolsstatus is $toolsstatus, teller is $teller!" -ForegroundColor Yellow
	  $teller ++
	  }
 
	if (($toolsstatus -match "toolsOk") -or ($toolsstatus -match "toolsOld")){
       $Startup = "OK"
	   Write-Host "$vm is started and has ToolsStatus $toolsstatus" -ForegroundColor Green
	}
	else{
       $Startup = "ERROR"
	   Write-Host "The ToolsStatus of $vm is $toolsstatus. This is unusual." -ForegroundColor Red
	   }
    return $Startup
}
 
Function PowerOff-VM($vm){
   $vmview = Get-VM $vm | Get-View
   $toolsstatus = $vmview.Guest.ToolsStatus
   if (($toolsstatus -eq "toolsNotInstalled") -or ($toolsstatus -eq "toolsNotRunning")){
	  Stop-VM -VM $vm -Confirm:$false | Out-Null}
   else{
   Shutdown-VMGuest -VM $vm -Confirm:$false | Out-Null
   Write-Host "$vm is stopping!" -ForegroundColor Yellow
   sleep 10
 
   $toolsstatus = $null
   $teller = 0
 
   while (($powerstate -ne "PoweredOff") -and ($teller -ne 11)){
      sleep 15
      $vmview = Get-VM $vm | Get-View
      $getvm = Get-VM $vm
      $powerstate = $getvm.PowerState
      $toolsstatus = $vmview.Guest.ToolsStatus
 
      Write-Host "$vm is stopping with powerstate $powerstate and toolsStatus $toolsstatus! The teller is $teller." -ForegroundColor Yellow
      $teller ++
      }
 
   }
 
   $getvm = Get-VM $vm
   $powerstate = $getvm.PowerState
   if ($powerstate -eq "PoweredOff"){
      $Shutdown = "OK"
	  Write-Host "$vm is powered-off"}
      else{
      $Shutdown = "ERROR"
	  Write-Host "The Powerstate of $vm is $powerstate. This is unusual." -ForegroundColor Red
	  }
   return $Shutdown
}
 
 
Function Check-VMHardwareVersion($vm){
   $vmView = get-VM $vm | Get-View
   $vmVersion = $vmView.Config.Version
   $v4 = "vmx-04"
   $v7 = "vmx-07"
   $v8 = "vmx-08"
   $v9 = "vmx-09"
 
   if ($vmVersion -eq $v4){
      $vmHardware = "Old"}
   elseif($vmVersion -eq $v7){
      $vmHardware = "Old"}
   elseif($vmVersion -eq $v8){
      $vmHardware = "Old"}
  elseif($vmVersion -eq $v9){
      $vmHardware = "OK"}
   else{
      $vmHardware = "ERROR"
	  [console]::ForegroundColor = "Red"
	  Read-Host "The Hardware version is unknown This is unusual. Press <CTRL>+C to quit the script or press <ENTER> to continue"
	  [console]::ResetColor()
   }
   return $vmHardware
}
 
Function Upgrade-VMHardware($vm){
   $vmview = Get-VM $vm | Get-View
   $vmVersion = $vmView.Config.Version
   $v4 = "vmx-04"
   $v7 = "vmx-07"
   $v8 = "vmx-08"
   $v9 = "vmx-09"
 
   if ($vmVersion -ne $v9){
      Write-Host "Old Version of hardware detected." -ForegroundColor Red
 
# Update Hardware
      Write-Host "Upgrading Hardware on" $vm -ForegroundColor Yellow
      Get-View ($vmView.UpgradeVM_Task($v9)) | Out-Null
   }
}
 
Function CheckAndUpgrade($vm){
 
   $vmHardware = Check-VMHardwareVersion $vm
   if($vmHardware -ne "OK"){
      Write-Host "The hardware level is $vmHardware on $vm" -ForegroundColor Red
      $PowerOffVM = PowerOff-VM $vm
	  if($PowerOffVM -eq "OK"){
	     Write-Host "Starting upgrade hardware level on $vm." 
		 Upgrade-VMHardware $vm
	     sleep 5
		 $PowerOnVM = PowerOn-VM $vm
	     if($PowerOnVM -eq "OK"){
		 Write-Host "$vm is up to date" -ForegroundColor Green}
		 else{
		 Write-Host "The VM $vm is not turned on or there is something wrong. Please take a look at $vm" -ForegroundColor Red}
	  }
	  else{Write-Host "The VM $vm is not turned off. Skipping $vm." -ForegroundColor Red}
   }  
   else{Write-Host "The hardware level of $vm is already OK. Skipping $vm." -ForegroundColor Red}
}
 
Connect-vCenter
$vms = VM-Selection
foreach($item in $vms){
   $vm = $item.Name
   Write-Host "Starting $vm"
   CheckAndUpgrade $vm
}
 
Disconnect-VIServer     * -confirm:$false
You could leave a comment if you were logged in.
powercliupgradehwlevel9.txt · Last modified: 2021/09/24 00:25 (external edit)