PoshCode Logo PowerShell Code Repository

Upgrade VM's to v7 (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1217"></script>download | new post

More info can be found at www.ict-freak.nl

  1. Function PowerOn-VM($vm){
  2.  
  3.         Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null
  4.        
  5.                 do {
  6.                         $vmview = get-VM $vm | Get-View
  7.                         $status = $vmview.Guest.ToolsStatus
  8.        
  9.                                 Write-Host $vm is starting! -ForegroundColor Yellow
  10.                                 sleep 5
  11.        
  12.                 }until(($status -match "toolsOld") -or ($status -match "toolsOk"))
  13.                
  14.                 if ($status -match "Ok"){
  15.                         $Startup = "Ok"}
  16.                 elseif($status -match "toolsOld"){
  17.                         $Startup = "ToolsOld"}
  18.                 else{
  19.                         $Startup = "Not Ready"}
  20.                
  21.                 return $Startup
  22.  
  23. }
  24.  
  25. Function PowerOff-VM($vm){
  26.                
  27.                 sleep 20
  28.                 Shutdown-VMGuest -VM $vm -Confirm:$false | Out-Null
  29.                                        
  30.                         do {
  31.                                 $vmview = Get-VM $vm | Get-View
  32.                                 $status = $vmview.Guest.ToolsStatus
  33.        
  34.                                         Write-Host $vm is stopping! -ForegroundColor Yellow
  35.                                         sleep 5
  36.        
  37.                         }until($status -match "toolsNotRunning")
  38.                         if ($status -match "toolsNotRunning"){
  39.                         $Shutdown = "Ok"}
  40.                         else{
  41.                         $Shutdown = "Not Ready"}
  42.                         return $Shutdown       
  43. }
  44.  
  45. Function Check-ToolsStatus($vm){
  46.  
  47.         $vmview = get-VM $vm | Get-View
  48.         $status = $vmview.Guest.ToolsStatus
  49.  
  50.                 if ($status -match "toolsOld"){
  51.                         $vmTools = "Old"}
  52.                 elseif($status -match "toolsNotRunning"){
  53.                         $vmTools = "Not Running"}
  54.                 else{
  55.                         $vmTools = "Ok"}
  56.                 return $vmTools
  57. }
  58.  
  59. Function Check-VMHardwareVersion($vm){
  60.         $vmView = get-VM $vm | Get-View
  61.         $vmVersion = $vmView.Config.Version
  62.         $v4 = "vmx-04"
  63.         $v7 = "vmx-07"
  64.        
  65.                 if ($vmVersion -eq $v4){
  66.                         $vmHardware = "Old"}
  67.                 elseif($vmVersion -eq $v7){
  68.                         $vmHardware = "Ok"}            
  69.                 else{Write-Host "Error!!" -ForegroundColor Red
  70.                         $vmHardware = "Error"}
  71.                
  72.                 return $vmHardware
  73. }
  74.  
  75. Function Upgrade-VMHardware($vm){
  76.  
  77.         $vmview = Get-VM $vm | Get-View
  78.         $vmVersion = $vmView.Config.Version
  79.         $v4 = "vmx-04"
  80.         $v7 = "vmx-07"
  81.  
  82.                 if ($vmVersion -eq $v4){
  83.                         Write-Host "Version 4 detected" -ForegroundColor Red
  84.                        
  85.                         # Update Hardware
  86.                         Write-Host "Upgrading Hardware on" $vm -ForegroundColor Yellow
  87.                         Get-View ($vmView.UpgradeVM_Task($v7)) | Out-Null
  88.         }      
  89. }
  90.  
  91. Function CreateBeforeList{
  92.  
  93. Write-Host "Creating a CSV File with VM info" -ForegroundColor Yellow
  94.  
  95. $MyCol = @()
  96.    ForEach ($vm in Get-Folder $Folder | Get-VM )
  97.    {
  98.         $vmview = get-VM $vm | Get-View
  99.    
  100.       $myObj = "" | Select Host, VMName, PowerState, IPAddress, MacAddress, Nics, VMToolsVersion, VMToolsStatus, VMToolsRunningStatus
  101.       $myObj.Host = $VM.Host.Name
  102.       $myObj.VMName = $VM.Name
  103.       $myObj.PowerState = $VM.PowerState
  104.           $myObj.IPAddress = [String]($vmview.Guest.Net| ForEach {$_.IpAddress})
  105.       $myObj.MacAddress = [String]($vmview.Guest.Net | ForEach {$_.MacAddress})
  106.           $myObj.Nics = $vmview.Guest.Net.Count
  107.           $myObj.VMToolsVersion = $vmview.Config.Tools.ToolsVersion
  108.           $myObj.VMToolsStatus = $vmview.Guest.ToolsStatus
  109.           $myObj.VMToolsRunningStatus = $vmview.Guest.ToolsRunningStatus
  110.          
  111.       $myCol += $myObj
  112.     }
  113.         $myCol | Export-Csv C:\beforeHWchange.csv -NoTypeInformation
  114. }
  115.  
  116. Function CreateAfterList{
  117.  
  118. Write-Host "Creating a CSV and an Excel File with VM info" -ForegroundColor Yellow
  119.  
  120. $xlCSV = 6
  121. $xlXLS = 56
  122. $csvfile = "C:\afterHWchange.csv"
  123. $xlsfile = "C:\afterHWchange.xls"
  124.  
  125. $Excel = New-Object -ComObject Excel.Application
  126. $Excel.visible = $True
  127. $Excel = $Excel.Workbooks.Add()
  128.  
  129. $Sheet = $Excel.Worksheets.Item(1)
  130. $Sheet.Cells.Item(1,1) = "VMName"
  131. $Sheet.Cells.Item(1,2) = "IPAddress"
  132. $Sheet.Cells.Item(1,3) = "Settings"
  133.  
  134. $intRow = 2
  135.  
  136. $WorkBook = $Sheet.UsedRange
  137. $WorkBook.Interior.ColorIndex = 19
  138. $WorkBook.Font.ColorIndex = 11
  139. $WorkBook.Font.Bold = $True
  140.  
  141.  
  142. $import = Import-Csv "C:\beforeHWchange.csv"
  143. $vms = Get-Folder $Folder | Get-VM
  144.  
  145. foreach($vm in $vms){
  146.         $vmview = $vm | Get-View
  147.  
  148.         foreach($item in $import){
  149.         $oldIp = $item.IPAddress
  150.         $newIP = $vm.Guest.IPAddress
  151.  
  152.  
  153.                 if($item.VMName -eq $vm){
  154.                         if($oldIp -eq $newIP){
  155.                                
  156.                                 $Sheet.Cells.Item($intRow, 1) = [String]$vmview.Name
  157.                                 $Sheet.Cells.Item($intRow, 2) = [String]$vm.Guest.IPAddress
  158.                                 $Sheet.Cells.Item($intRow, 3) = "Good"
  159.                                 $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
  160.                                 }
  161.                                 else{
  162.                                 $Sheet.Cells.Item($intRow, 1) = [String]$vmview.Name
  163.                                 $Sheet.Cells.Item($intRow, 2) = [String]$vm.Guest.IPAddress
  164.                                 $Sheet.Cells.Item($intRow, 3) = "Wrong"
  165.                                 $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
  166.                                
  167.                                 }
  168.                 }
  169.         }
  170.  
  171.  
  172. $intRow = $intRow + 1}
  173.  
  174. $WorkBook.EntireColumn.AutoFit()
  175.  
  176. sleep 5
  177.  
  178. $Sheet.SaveAs($xlsfile,$xlXLS)
  179. $Sheet.SaveAs($csvfile,$xlCSV)
  180.  
  181. }
  182.  
  183.  
  184. $vCenter = Read-Host "Enter your vCenter servername"
  185. $Folder = Read-Host "Enter the name of the folder where the VMs are stored"
  186.  
  187. Connect-VIServer $vCenter
  188.  
  189. $vms = Get-Folder $Folder | Get-VM  | Get-View | Where-Object {-not $_.config.template -and $_.Config.Version -eq "vmx-04" } | Select Name
  190.  
  191. CreateBeforeList
  192.  
  193. foreach($item in $vms){
  194.                 $vm = $item.Name
  195.                 $vmHardware = Check-VMHardwareVersion $vm
  196.                 $vmToolsStatus = Check-ToolsStatus $vm
  197.                
  198.                         Write-Host "Hardware is old on:" $vm -ForegroundColor Red
  199.  
  200.                                         if($vmToolsStatus -eq "Old"){
  201.                                                         Write-Host "The VMware Tools are old on:" $vm -ForegroundColor Red
  202.                                                         Sleep 20
  203.                                                        
  204.                                                         Get-VMGuest $vm | Update-Tools
  205.                                                         Sleep 120
  206.                                                         Write-Host "VMware Tools are installed on:" $vm -ForegroundColor Cyan
  207.                                                                
  208.                                                                 $vmToolsStatus = Check-ToolsStatus $vm
  209.                                                        
  210.                                                                 if($vmToolsStatus -eq "Ok"){
  211.                                                                        
  212.                                                                         $PowerOffVM = PowerOff-VM $vm
  213.                                                                         if($PowerOffVM -eq "Ok"){
  214.                                                                                 Write-Host $vm "is down" -ForegroundColor Yellow
  215.                                                                                
  216.                                                                                 Upgrade-VMHardware $vm
  217.                                                                                 PowerOn-VM $vm
  218.                                                                                 Write-Host $vm "is up to date" -ForegroundColor Green
  219.                                                                         }
  220.                                                                 }
  221.                                        
  222.                                         }
  223.                                         else{
  224.                                                 $PowerOffVM = PowerOff-VM $vm
  225.                                                 if($PowerOffVM -eq "Ok"){
  226.                                                         Upgrade-VMHardware $vm
  227.                                                         PowerOn-VM $vm
  228.                                                         Write-Host $vm "is up to date" -ForegroundColor Green
  229.                                                        
  230.                                                 }      
  231.                                         }
  232. }
  233.  
  234. Sleep 40
  235.  
  236. CreateAfterList
  237.  
  238. Disconnect-VIServer -Confirm:$false

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me