Browsed by
Tag: powercli

vRA 7.x Snapshotting using PowerCLI Script

vRA 7.x Snapshotting using PowerCLI Script

Recently, I was looking for a scripted method for cold snapshotting of vRA in enterprise deployment. First, I wanted to confirm the shutdown order. VMware’s product documentation is quite limited regarding this topic, but it describes the right shutdown order. Looking further I found a better explanation here, where the documentation talks about vRA backup order. Of course, I wanted to check the proper starting procedure as well. I found it quickly here.

Well equipped with all information needed, I decided to write a script. Usually I use PowerCLI for tasks related to VMware software. I was sure that someone had the same idea before me so in order not to reinvent the wheel I started to check what Google will find.
I found a few elegant approaches, but one was the most interesting and inspiring.
Distributed vRealize Automation 7.x Orchestrated Shutdown, Snapshot and Startup using PowerCLI
Razz made it the way that I found I could use in my environment. I adjusted the script to my needs and it worked quite well. Of course, there is a lot to polish, but it works for me.

I decided to share Razz’s script adjusted by me. Maybe it will help someone with their administration tasks related to vRA.


$vCSA = ""
$snapName = ""
$snapDescription = ""
$log = ""

# vRA components
$proxy = @() 
$worker = @() 
$activeMgr = ""
$passiveMgr = @()
$primaryWeb = ""
$secondaryWeb = @()
$masterVRA = ""
$replicaVRA = @()
$dbServers = @()
$vRB = ""
$vRO = @()
$allVMs = @()

# Log file
$log = "coldvRASnapshots.log"

function shutdownVMandWait($vms,$log) {
    foreach ($vmName in $vms) {
        try {
            $vm = Get-VM -Name $vmName -ErrorAction Stop
            foreach ($o in $vm) {
                    if (($o.PowerState) -eq "PoweredOn") {
                        $v = Shutdown-VMGuest -VM $o -Confirm:$false
                        Write-Host "Shutdown VM: '$($v.VM)' was issued"
                        Add-Content -Path $log -Value "$($v)"
                    } else {
                        Write-Host "VM '$($vmName)' is not powered on!"
                    }
            }   
        } catch {
            Write-Host "VM '$($vmName)' not found!"
        }
    }
    foreach ($vmName in $vms) {
        try {
            $vm = Get-VM -Name $vmName -ErrorAction Stop
            while($vm.PowerState -eq 'PoweredOn') { 
                sleep 5
                Write-Host "VM '$($vmName)' is still on..."
                $vm = Get-VM -Name $vmName
            }
            Write-Host "VM '$($vmName)' is off!"
        } catch {
            Write-Host "VM '$($vmName)' not found!"
        }
    }
}

function snapshotVM($vms,$snapName,$snapDescription,$log) {
    foreach ($vmName in $vms) {
        try {
            $vm = Get-VM -Name $vmName -ErrorAction Stop
        } catch {
            Write-Host "VM '$($vmName)' not found!"
            Add-Content -Path $log -Value "VM '$($vmName)' not found!"
        }
        try {
            foreach ($o in $vm) {
                    New-Snapshot -VM $o -Name $snapName -Description $snapDescription -ErrorAction Stop   
            }
        } catch {
            Write-Host "Could not snapshot '$($vmName)' !"
            Add-Content -Path $log -Value "Could not snapshot '$($vmName)' !"
    
        }
    }
}

function startupVM($vms,$log) {
    foreach ($vmName in $vms) {
        try {
            $vm = Get-VM -Name $vmName -ErrorAction Stop
            foreach ($o in $vm) {
                if (($o.PowerState) -eq "PoweredOff") {
                        Start-VM -VM $o -Confirm:$false -RunAsync
                    } else {
                        Write-Host "VM '$($vmName)' is not powered off!"
                    }
                }   
        } catch {
            Write-Host "VM '$($vmName)' not found!"
        }
    } 
}

# vCenter Server FQDN
$vCSA = Read-Host -Prompt "Enter vCenter's FQDN"

# Connect vCenter Server
$creds = Get-Credential

Connect-VIServer $vCSA -Credential $creds -ErrorAction Stop

# Get VM names
$proxy = @(((Read-host -Prompt "Enter comma separated names of Proxy Agent VMs").Split(",")).Trim()) 
$worker = @(((Read-host -Prompt "Enter comma separated names of DEM worker VMs").Split(",")).Trim()) 
$activeMgr = Read-host -Prompt "First, check which VM is a Primary Manager and then enter its name"
$passiveMgr = @(((Read-host -Prompt "Enter comma separated names of Secondary Manager VMs").Split(",")).Trim())
$primaryWeb = Read-host -Prompt "First, check which VM is a Primary Web Server and then enter its name"
$secondaryWeb = @(((Read-host -Prompt "Enter comma separated names of Secondary Web VMs").Split(",")).Trim())
$masterVRA = Read-host -Prompt "Enter a name of Master vRA Node VM"
$replicaVRA = @(((Read-host -Prompt "Enter comma separated names of Replica vRA Node VMs").Split(",")).Trim())
$dbServers = @(((Read-host -Prompt "Shutdown MSSQL AlwaysOn Cluster first, than enter comma separated names of DB Cluster Node VMs").Split(",")).Trim())

<# ### Uncomment all commented block of code if you have vRB or the external vRO instances in your environment
$vRB = Read-host -Prompt "Enter a name of vRB VM"
$vRO = @(((Read-host -Prompt "Enter comma separated names of external vRO VMs").Split(",")).Trim())
#>

$allVMs = @($proxy, $worker, $passiveMgr, $activeMgr, $secondaryWeb, $primaryWeb, $replicaVRA, $masterVRA, $dbServers)

# Snapshot definition
$snapName = Read-Host -Prompt "Enter Snapshot Name"
$snapDescription = Read-Host -Prompt "Enter Snapshot Description"

# Shutting down vRA VMs
foreach ($vmName in $allVMs) {
    foreach ($vm in $vmName) {
        if ($vm) {
            Write-Host "### Shutting down " + $vm
            shutdownVMandWait -vms $vm -log $log
        } else {
            Write-Host "VM '$($vm)' doesn't exist!"
        }
    }   
}

# Snapshotting vRA VMs
foreach ($vmName in $allVMs) {
    foreach ($vm in $vmName) {
        if ($vm) {
            Write-Host "### Taking snapshot of " + $vm
            snapshotVM -vms $vm -snapName $snapName -snapDescription $snapDescription -log $log
        } else {
            Write-Host "VM '$($vm)' doesn't exist!"
        }
    }   
}

# Starting vRA VMs
<#
Write-Host "### Starting vROs"
startupVM -vms $vRO -log $log

Write-Host "### Starting vRB"
startupVM -vms $vRB -log $log
#>

Write-Host "### Starting DB Servers"
startupVM -vms $dbServers -log $log
Write-Host  " Sleeping 5 minutes until db is up"
Start-Sleep -s 300

Write-Host "### Starting primary VRA"
startupVM -vms $masterVRA -log $log
Write-Host  " Sleeping 5 minutes until Licensing service is registered"
Start-Sleep -s 300

Write-Host "### Starting secondary VRA"
startupVM -vms $replicaVRA -log $log
Write-Host  " Sleeping 15 minutes until ALL services are registered"
Start-Sleep -s 900

Write-Host "### Starting Web"
startupVM -vms $primaryWeb -log $log
startupVM -vms $secondaryWeb -log $log
Write-Host  " Sleeping 5 minutes until services are up"
Start-Sleep -s 300

Write-Host "### Starting Primary manager"
startupVM -vms $activeMgr -log $log
Write-Host  " Sleeping 3 minutes until manager is up"
Start-Sleep -s 180

Write-Host "### Starting Secondary manager"
startupVM -vms $passiveMgr -log $log
Write-Host  " Sleeping 3 minutes until manager is up"
Start-Sleep -s 180

Write-Host "### Starting DEM workers"
startupVM -vms $worker -log $log

Write-Host "### Starting Proxy Agents"
startupVM -vms $proxy -log $log

Write-Host "### All components have been started"

# Disconnect vCenter 
Disconnect-VIServer -Server $vCSA -Confirm:$false 
PowerCLI – useful tools

PowerCLI – useful tools

VMware PowerCLI is a powerful tool for daily task for every Admin. The pure console is most commonly used. However, there are a few alternatives to the simple console which could make the use of PowerCLI even more handy. I’ll describe them shortly in the next a few lines.

  1. Powershell ISE script editor, which provides a better user experience. It’s divided into two panes. The upper pane  is for viewing/editing script files, and the lower pane is for running individual commands and displaying their output (an analog of the standard PowerShell console).ISEYou can execute PowerCLI commands in the lower pane of PowerShell ISE or in the PowerShell console. It will be usefull also during the first steps with PowerCLI through analysing of some ready downloaded from Internet scripts.The most convenient way to do is to open a script in the upper pane of Powershell ISE. In this way you can select each individual command and executee by pressing F8 or “Run Selection” button. When the command execution is finished you will see “Completed” message at the bottom of the console.
  2. PowerGUI – it’s another script editor made by Quest Software which was acquired by Dell. powerguiIMHO it’s better organised than PowerShell ISE, the functions are rather simillar. However, instead of tabs with your scripts here you will be able to see the whole folder-tree with different kinds of scripts. It’s really helpful when you work with more than a few scripts.
VMware PowerCLI – Introduction

VMware PowerCLI – Introduction

To begin the jurney with PowerCLI we need to start from the installation of PowerCLI itself.

The installation can be done on a Windows based system, that could be some kind of an administration server. The installation files can be found on this VMware site.

There are a few versions available, they are released asynchronously with vSphere and the version numbers do not exactly correspond to vSphere versions. The most recent version is 6.5 whilst there are other like 6.3, 6.0 or 5.8 available.

Before you install the PowerCLI I recommend to change the Execution Policy of Powershell. It is required to run scripts. To do it, run Windows PowerShell as administrator and execute fallowing command:

Set-ExecutionPolicy RemoteSigned

The installation process is really straightforward, that’s why I will not spam the screanshoots of installations here.

After you finish the installation you can run it and see the first Welcome screen like this:

powercli1

 

The first command I suggest to use is:

Get-VICommand

it lists all the available commands. However to display any information about virtual infrastructure you need to connect to a vCenter server or ESXi host. We will do it in the next part after introduction of useful tools which can be used in conjunction with PowerCLI.

PowerCLI course

PowerCLI course

I was always keen on getting deeper knowledge about PowerCLI or in other words – start to use it in daily administrative tasks. I decided to do something with it and I think it would be the best way to write my own guide in a form of structured notes and share it here with you. Perhaps someone would find it useful.

Therefore in PowerCLI & VMA tab you could find an agenda of this course which will be systematically updated with next parts.

There are planned fallowing parts:

  1. VMware PowerCLI – Introduction
  2. Useful Tools
  3. Basic commands to generate and export reports
  4. Monitoring VMs with PowerCLI
  5. Managing VMs using PowerCLI
  6. Managing multiple VMs based on their tags
  7. Monitoring ESXi hosts with PowerCLI
  8. Managing ESXi hosts using PowerCLI
  9. Managing virtual networks using PowerCLI
  10. Managing Cluster-wide settings using PowerCLI
  11. Complete ESXi configuration with a single script

 

If you have any sugestions what else should be included in such course, do not hesitate to contact me via comments.