image

Disclaimer: use this script at your own risk 😉

In part 1 you could find a script to upgrade your templates to hardware version 7. In this post you’ll find a script that will upgrade your VM’s to hardware version 7.

Note: This script will upgrade the VMware Tools if necessary and will shutdown the VM!!!!!

You can download the script here: upgrade-vhardware_vm and here: http://poshcode.org/1217

The script will perform the following actions:

  • Connect to vCenter
  • Get all the VM’s in the folder you need to specify
  • Create a CSV file with some info about the VM. I will post the content later.
  • Update the VMware Tools if necessary (The VM will restart after the installation)
  • Shutdown the VM
  • Upgrade the vHardware to version 7
  • Start the VM
  • Create an Excel sheet with an overview of the VM’s and their IP settings

During the process of the script, the VM will be unavailable for 5 minutes or less. So be sure that nobody uses the VM.

The beforeHWchange.csv will look like this:

image

The script in action:

image

The last step, the creation of an Excel sheet with an overview of the VM’s. Here you can see if the IP Address is changed or not.

image

To do list:

  • Create a proper VM report function (export to csv) so I can capture multiple network adapters.
  • Create a before Excel sheet with a nice overview of the environment.
Advertisement

10 thoughts on “PowerCLI: Upgrading vHardware to vSphere Part 2: VM’s

  1. Arne, very nice script.

    One question from me:
    After the hardware upgrade, is it still needed to logon to every vm and have Windows detect new hardware devices and restart the vm?

  2. Arne, Have you ever created the “before” report? We have a mash up of network cards, VM hardware 4 & 7, and finally differing versions of vmware tools. Do you have a report that can put all of these in a CSV?

  3. I’ve made some changes to make it upgrade from v7 to v8 but i’m not sure if it will work or not, what do you think

    Function PowerOn-VM($vm){

    Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null

    do {
    $vmview = get-VM $vm | Get-View
    $status = $vmview.Guest.ToolsStatus

    Write-Host $vm is starting! -ForegroundColor Yellow
    sleep 5

    }until(($status -match “toolsOld”) -or ($status -match “toolsOk”))

    if ($status -match “Ok”){
    $Startup = “Ok”}
    elseif($status -match “toolsOld”){
    $Startup = “ToolsOld”}
    else{
    $Startup = “Not Ready”}

    return $Startup

    }

    Function PowerOff-VM($vm){

    sleep 20
    Shutdown-VMGuest -VM $vm -Confirm:$false | Out-Null

    do {
    $vmview = Get-VM $vm | Get-View
    $status = $vmview.Guest.ToolsStatus

    Write-Host $vm is stopping! -ForegroundColor Yellow
    sleep 5

    }until($status -match “toolsNotRunning”)
    if ($status -match “toolsNotRunning”){
    $Shutdown = “Ok”}
    else{
    $Shutdown = “Not Ready”}
    return $Shutdown
    }

    Function Check-ToolsStatus($vm){

    $vmview = get-VM $vm | Get-View
    $status = $vmview.Guest.ToolsStatus

    if ($status -match “toolsOld”){
    $vmTools = “Old”}
    elseif($status -match “toolsNotRunning”){
    $vmTools = “Not Running”}
    else{
    $vmTools = “Ok”}
    return $vmTools
    }

    Function Check-VMHardwareVersion($vm){
    $vmView = get-VM $vm | Get-View
    $vmVersion = $vmView.Config.Version
    $v7 = “vmx-07”
    $v8 = “vmx-08”

    if ($vmVersion -eq $v7){
    $vmHardware = “Old”}
    elseif($vmVersion -eq $v8){
    $vmHardware = “Ok”}
    else{Write-Host “Error!!” -ForegroundColor Red
    $vmHardware = “Error”}

    return $vmHardware
    }

    Function Upgrade-VMHardware($vm){

    $vmview = Get-VM $vm | Get-View
    $vmVersion = $vmView.Config.Version
    $v7 = “vmx-07”
    $v8 = “vmx-08”

    if ($vmVersion -eq $v7){
    Write-Host “Version 7 detected” -ForegroundColor Red

    # Update Hardware
    Write-Host “Upgrading Hardware on” $vm -ForegroundColor Yellow
    Get-View ($vmView.UpgradeVM_Task($v8)) | Out-Null
    }
    }

    Function CreateBeforeList{

    Write-Host “Creating a CSV File with VM info” -ForegroundColor Yellow

    $MyCol = @()
    ForEach ($vm in Get-Folder $Folder | Get-VM )
    {
    $vmview = get-VM $vm | Get-View

    $myObj = “” | Select Host, VMName, PowerState, IPAddress, MacAddress, Nics, VMToolsVersion, VMToolsStatus, VMToolsRunningStatus
    $myObj.Host = $VM.Host.Name
    $myObj.VMName = $VM.Name
    $myObj.PowerState = $VM.PowerState
    $myObj.IPAddress = [String]($vmview.Guest.Net| ForEach {$_.IpAddress})
    $myObj.MacAddress = [String]($vmview.Guest.Net | ForEach {$_.MacAddress})
    $myObj.Nics = $vmview.Guest.Net.Count
    $myObj.VMToolsVersion = $vmview.Config.Tools.ToolsVersion
    $myObj.VMToolsStatus = $vmview.Guest.ToolsStatus
    $myObj.VMToolsRunningStatus = $vmview.Guest.ToolsRunningStatus

    $myCol += $myObj
    }
    $myCol | Export-Csv C:\beforeHWchange.csv -NoTypeInformation
    }

    Function CreateAfterList{

    Write-Host “Creating a CSV and an Excel File with VM info” -ForegroundColor Yellow

    $xlCSV = 6
    $xlXLS = 56
    $csvfile = “C:\afterHWchange.csv”
    $xlsfile = “C:\afterHWchange.xls”

    $Excel = New-Object -ComObject Excel.Application
    $Excel.visible = $True
    $Excel = $Excel.Workbooks.Add()

    $Sheet = $Excel.Worksheets.Item(1)
    $Sheet.Cells.Item(1,1) = “VMName”
    $Sheet.Cells.Item(1,2) = “IPAddress”
    $Sheet.Cells.Item(1,3) = “Settings”

    $intRow = 2

    $WorkBook = $Sheet.UsedRange
    $WorkBook.Interior.ColorIndex = 19
    $WorkBook.Font.ColorIndex = 11
    $WorkBook.Font.Bold = $True

    $import = Import-Csv “C:\beforeHWchange.csv”
    $vms = Get-Folder $Folder | Get-VM

    foreach($vm in $vms){
    $vmview = $vm | Get-View

    foreach($item in $import){
    $oldIp = $item.IPAddress
    $newIP = $vm.Guest.IPAddress

    if($item.VMName -eq $vm){
    if($oldIp -eq $newIP){

    $Sheet.Cells.Item($intRow, 1) = [String]$vmview.Name
    $Sheet.Cells.Item($intRow, 2) = [String]$vm.Guest.IPAddress
    $Sheet.Cells.Item($intRow, 3) = “Good”
    $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
    }
    else{
    $Sheet.Cells.Item($intRow, 1) = [String]$vmview.Name
    $Sheet.Cells.Item($intRow, 2) = [String]$vm.Guest.IPAddress
    $Sheet.Cells.Item($intRow, 3) = “Wrong”
    $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3

    }
    }
    }

    $intRow = $intRow + 1}

    $WorkBook.EntireColumn.AutoFit()

    sleep 5

    $Sheet.SaveAs($xlsfile,$xlXLS)
    $Sheet.SaveAs($csvfile,$xlCSV)

    }

    $vCenter = Read-Host “Enter your vCenter servername”
    $Folder = Read-Host “Enter the name of the folder where the VMs are stored”

    Connect-VIServer $vCenter

    $vms = Get-Folder $Folder | Get-VM | Get-View | Where-Object {-not $_.config.template -and $_.Config.Version -eq “vmx-04” } | Select Name

    CreateBeforeList

    foreach($item in $vms){
    $vm = $item.Name
    $vmHardware = Check-VMHardwareVersion $vm
    $vmToolsStatus = Check-ToolsStatus $vm

    Write-Host “Hardware is old on:” $vm -ForegroundColor Red

    if($vmToolsStatus -eq “Old”){
    Write-Host “The VMware Tools are old on:” $vm -ForegroundColor Red
    Sleep 20

    Get-VMGuest $vm | Update-Tools
    Sleep 120
    Write-Host “VMware Tools are installed on:” $vm -ForegroundColor Cyan

    $vmToolsStatus = Check-ToolsStatus $vm

    if($vmToolsStatus -eq “Ok”){

    $PowerOffVM = PowerOff-VM $vm
    if($PowerOffVM -eq “Ok”){
    Write-Host $vm “is down” -ForegroundColor Yellow

    Upgrade-VMHardware $vm
    PowerOn-VM $vm
    Write-Host $vm “is up to date” -ForegroundColor Green
    }
    }

    }
    else{
    $PowerOffVM = PowerOff-VM $vm
    if($PowerOffVM -eq “Ok”){
    Upgrade-VMHardware $vm
    PowerOn-VM $vm
    Write-Host $vm “is up to date” -ForegroundColor Green

    }
    }
    }

    Sleep 40

    CreateAfterList

    Disconnect-VIServer -Confirm:$false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.