Browsed by
Month: October 2020

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 
Getting Support Log Bundle using REST API

Getting Support Log Bundle using REST API

Starting from vSphere 7U1 we have got a new tool to generate and download a support log bundle from vCenter Server. It is REST API call used to achieve these tasks. It broadens an already wide range of methods of gathering vm-support log bundle. It has several interesting features.

First, it works even if the vCSA service is offline, whereas a vCSA management interface should be up and running.

Second, once started it generates a support bundle and stores it on vCSA disk. A downloaded bundle is deleted after 30 minutes. As you know, even in a small vSphere environment downloading a log bundle could be time consuming, therefor, if a generated bundle download task is in progress, the bundle deletion will be postponed for 30 minutes.

To be able to use this new method a user authenticated in SSO must be a member of a new SSO group – SystemConfiguration.SupportUsers. Users belonging to this group are entitled only to call support bundle REST API. They don’t have any other privileges to the environment. SSO Administrator role is a member of this group by default.

Now, let’s play with the API calls.

To enumerate every component which is gathered by support bundle REST API, you can use the following GET command:

GET https://vcsa_fqdn:5480/rest/appliance/support-bundle/components

To generate a bundle you can use this POST statement:

POST https://vcsa_fqdn:5480/rest/appliance/support-bundle?action=create&vmw-task=True

This statement takes a few parameters:

  • Description – A text description of a the started task
  • Components (optional) – You can provide a list of previously listed components. If you leave this parameter empty, then logs from all components will be gathered.
  • Partition (optional) – You can define a place where generated log bundle will be stored, for example /storage/core. If you leave it blank, a default storage location (/storage/log) will be used.

This statement will return a task ID. You can use this task ID to get the information about a status of a task.

To return the status of a task you have started, issue this command providing a task ID at the end:

GET https://vcsa_fqdn:5480/rest/cis/tasks/<task-id>

If you need more detailed information, use this command to return the complete information about the generation task:

GET https://vcsa_fqdn:5480/rest/appliance/support-bundle

You will get all the information (description, status, generation time, expiration time, bundle size) including URL, which you can use to download the generated support bundle.

If the support bundle REST API fails, you will be informed with an error message providing information about a failure reason.

I recommend using Postman to issue these commands because you can prepare an Environment, a Collection, named for example vCSA Support Bundle where you can store the commands above mentioned. You can use curl as well, if it’s your favourite multi-tool.