...hopefully some useful VMware related stuff

Script to test Veeam replica Virtual Machines in a protected method

11/03/2010 22:49

This script is if you want to check the validity of your Veeam replica virtual machine by bringing it up either disconnected from the network or connected to another test network, without affecting the integrity of the replica and allow the replication to continue after testing.

 

Requirements:

  • VMware PowerCLI (VI-toolkit)
  • Powershell 2.0 (although I'm pretty sure 1.0 is fine also)

 

The script can be downloaded HERE and is shown below the video.

 

 

 

What it does:

 

  • User supplies vCenter / ESX name containing the Veeam _replica virtual machines
  • Enters Username and Password
  • A list of all virtual machines available to  be tested is presented with a request for user input of the name
  • A snapshot is then created
  • The next choice is whether it is to be brought up with disconnected network cards or connected to a different network
  • If you select ‘D’ or leave default it will bring disconnect all network adapters. If you select ‘C’ then it queries the host for all available networks
  • Presents a list of these and waits for the user to make a choice
  • Then the VM is powered on
  • A button is created and is waiting for the user to finish testing the virtual machine etc
  • Once finished and the button pressed, the vm is powered off, reverted to the snapshot and then the snapshot is deleted
  • The notes field is then updated with the info that a failover test was completed
  • There is a certain amount of error checking within the script, for example if an incorrect port group or VM is selected
  • I would welcome any input / feedback on how to improve this script

 

- - - - - - - - - - - -

 

 

# Dans Veeam Replica safe test - Version 1.9
# https://www.a2-alpha.co.uk
# Version 1.9

 

 

 

 


# Set Errors to Silently Continue

$erroractionpreference = "silentlycontinue"


# Add VI-toolkit if not loaded

$snapins = get-pssnapin | select Name
foreach($snapin in $snapins)
{
    if($snapin.Name -eq "VMware.VimAutomation.Core")
          {
              $result = "Yes"
          }
    else
          {
              $result = "No"
          }
}
if($result -eq "No")
{
    Add-PSsnapin VMware.VimAutomation.Core
    Initialize-VIToolkitEnvironment.ps1
}


#ENTER VC AND CREDENTIALS

clear
"  "
"  "
" ** Replica Test Utility **"
" **  www.a2-alpha.co.uk  **"
" **      Version 1.9     **"
"  "
"  "
$vc = read-host "Enter vCenter or ESX name or IP Address"
"  "
$us = read-host "Enter Username"
$pw = read-host "Enter Password" -assecurestring:$true
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $us,$pw

#CLEAR ERROR LOGS

$Error.clear()

#LOG INTO VCENTER

Connect-VIServer -Server $vc -Credential $cred -erroraction SilentlyContinue

$E = $Error
$E = $E | out-string
if($E.Contains("connect"))
{
 clear
 " "
 " "
 "There was an error Connecting to the VI Server or Host, Please check Name and Resolution."
 " "
 " "
}
Else
{
  CLEAR

 #LIST AVAILABLE VIRTUAL MACHINES

        $vms = get-vm
        $VMsArray = @()
        $VMs | ForEach-Object { $VMsArray += $_.Name }
        Write-Output ""
        Write-Output "Here are the virtual machines to choose from:"
        Write-Output ""
        For ($i = 0; $i -lt $VMsArray.Length; $i++ ) { Write-Output "$i $($VMsArray[$i])"}
        Write-Output ""

 #AQUIRE THE VIRTUAL MACHINE FROM NUMBER INPUT
      " "
      " "
  #CHECK INPUTED VIRTUAL MACHINE IS A VALID MACHINE
     
      While($selectedNum -ge $VMsArray.Length -or $selectedNum -lt "0")
      {
            [int]$selectedNum = read-host "Enter the number of the Virtual Machine to Test Replica of"
      }
      $SelectedVM = $VMsArray[$SelectedNum]
      IF((get-vm $selectedVM).PowerState -eq "PoweredOn")
      {
            " "
            "This Virtual Machine is powered on, please select a VM which is powered off."
            " "
      }
      ELSE
      {
            $Error.clear()
            get-vm $selectedVM -ErrorAction silentlycontinue
            $E = $Error
            $E = $E | out-string
            if($E.Contains("get-vm"))
            {
                    clear
                    " "
                    " "
                    "I don't think this is a valid Virtual Machine, please check name and rerun."
                    " "
                    " "
            }
            Else
            {
 

                #TAKE A SNAPSHOT OF THE VIRTUAL MACHINE
               
                clear
                $Error.clear()
                new-snapshot -Name "Pre Replica Power On Test" -VM $selectedVM -confirm:$false -ErrorAction silentlycontinue
                $E = $Error
                $E = $E | out-string
                if($E.Contains("snapshot"))
                {
                      clear
                      " "
                      " "
                      "There was a problem creating the snapshot, please check the ability to create a snapshot and rerun."
                      " "
                      " "
                }
                Else
                {
                     
                      #MAKE DECISION ABOUT NETWORK CARDS
                      CLEAR
                      " "
                      " "
                      " What would you like to do about the network adapters in the machine,"
                      " By default all network cards will be disconnected prior to powering up."
                      " "
                      $netdecision = read-host "Disconnect or Connect to different Network (D / C) "
                      " "
                      " "
                      if($netdecision -eq "C")
                      {
                            $vpge = get-virtualportgroup -VM $selectedVM
                            clear
                            "You have chosen to connect to a network on boot"
                            " "
                            "This VM is currently connected to $vpge"
                            " "
                            $vmhost = get-vmhost -VM (Get-VM -Name $selectedVM) | select-object Name
                            $vmhostn = $vmhost.name
                            $vpgo = get-virtualportgroup -VMhost $vmhostn | select Name
                            #$vpgo = $vpgo | out-string
                            $vpgoarray = @()
                            $vpgo | foreach-object { $vpgoarray += $_.Name }
                            " "
                            "Here are the new Port groups to connect to"
                            "NOTE: You cannot connect to the Service Console or VMkernel Ports"
                            " "
                            For ($i = 0; $i -lt $vpgoarray.Length; $i++) {write-output "$i $($vpgoarray[$i])"}
                            " "
                            $vpgoarraylength = $vpgoarray.Length               
                            while ($vpgs -lt 0 -or $vpgs -ge $vpgoarraylength)
                            {
                                          [int]$vpgs = read-host "Enter the Number of the Port group to connect to Default[0]"
                            }
                            $netadapters = get-networkadapter -VM $selectedVM
                           
                            #CHECK INPUTED NUMBER IS VALID
                           
                            $selectedPortGroup = $vpgo["$vpgs"]
                            #output test
                            $selectedPortGroupName = $selectedPortGroup.Name
                            "You have selected to Connect to $selectedPortGroupName on PowerOn"

                           
                           
                            $netadapters = get-networkadapter -VM $selectedVM
                         
                            ForEach($netadapter in $netadapters)
                                {
                                        set-networkadapter -NetworkAdapter $netadapter -NetworkName $selectedPortGroupName -StartConnected:$TRUE -confirm:$false
                                 
                                }
                        }
                        Else
                        {
                                "The Virtual Machine $selectedVM will start with Disconnected Network Adapters"
                                #IDENTIFY AND DISCONNECT THE NETWORK ADAPTERS
                                $netadapters = get-networkadapter -VM $selectedVM
                               
                                ForEach($netadapter in $netadapters)
                                {
                                       set-networkadapter -NetworkAdapter $netadapter -StartConnected:$FALSE -confirm:$false
                                }
                        }
                        $E = $Error
                        $E = $E | out-string
                        if($E.Contains("network"))
                        {
                                clear
                                " "
                                " "
                                "An error has occured setting the network, make sure that the name is correct and rerun"
                                " "
                                " "
                        }
                        Else
                        {
                                #POWER ON THE VIRTUAL MACHINE

                                start-vm -VM $selectedVM
                                clear
                                #WAIT FOR BUTTON PRESS WHEN TESTING IS COMPLETE

                                [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
                                $form = new-object Windows.Forms.Form
                                $form.Size = New-Object System.Drawing.Size(200,100)
                                $form.Text = "Dans Replica Test for Veeam Replication"
                                $button = new-object Windows.Forms.Button
                                $button.text="Click Here when testing is Complete"
                                $button.Dock="fill"
                                $button.add_click({$form.close()})
                                $form.controls.add($button)
                                $form.Add_Shown({$form.Activate()})
                                $form.ShowDialog()
                                clear


                                #SHUTDOWN THE VIRTUAL MACHINE

                                Stop-VM -VM $selectedVM -Confirm:$false

                                #REVERT AND REMOVE THE SNAPSHOT

                                $snapshot = Get-Snapshot -VM $selectedVM -Name "Pre Replica Power On Test"
                                set-vm -VM $selectedVM -Snapshot $snapshot -confirm:$false
                                remove-snapshot -snapshot $snapshot -confirm:$false

                                #ASSIGN NOTES TO THE VIRTUAL MACHINE TO SHOW REPLICA TEST IS COMPLETE
                                $date = get-date
                                set-vm -VM $selectedVM -description "Failover test occured on $date" -confirm:$false


                                #ENDING COMMENTS
                                disconnect-viserver -force -confirm:$false
                                clear
                                "  "
                                clear
                                " "
                                "Thankyou for using this Script to test your Veeam Replica"
                                " "
                                "Dan"
                                " "
                                }
                         }
                  }
         }
}
#END

Search site