Make vRA testing easier with these PowerCLI scripts

A while ago I posted a bunch of PowerCLI scripts that made my life easier when building vSphere environments. Lately I have been doing a lot of testing on vRealize Automation 7 for a customer. Amongst other things this also included migration testing and therefore I needed to swap back to pre-migrated state a lot. After shutting down, snapshotting, starting back up, reverting, starting back up on 13 VM’s manually a few times I got sick of it. I put together a few very simple scripts that made my life a lot easier and efficient.

Shut down script

This is an obvious one, it is used to shutdown VM’s. Specifically the vRA VM’s and in the correct order. This is important because vRA can get real nasty on you if not shutdown in the correct order. Think of message bus or elasticsearch issues.

# ------------------------------------------------------------------------------------------------------------------
# Script to shut down complete vRA7 environment in correct order.
# By Wesley van Ede, dutchvblog.com
# ------------------------------------------------------------------------------------------------------------------
 
Shutdown-VMGuest -vm "IaaS Dem-w 2" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Dem-w 1" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Agent 2" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Agent 1" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Mgr/Dem-o 2" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Mgr/Dem-o 1" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Web 2" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "IaaS Web 1" -Confirm:$false
Sleep -Seconds 15
Shutdown-VMGuest -vm "vRA VA 3" -Confirm:$false
Sleep -Seconds 30
Shutdown-VMGuest -vm "vRA VA 2" -Confirm:$false
Sleep -Seconds 30
Shutdown-VMGuest -vm "vRA VA 1" -Confirm:$false
Sleep -Seconds 10
Shutdown-VMGuest -vm "IaaS DB" -Confirm:$false

Create snapshots

The next script ensures that all VM’s that make up the vRA environment get a new snapshot with name and description. Using a description on what is included in a snapshot sounds like it is overrated (you can have some info in the name), but I found that when the number of snapshots grows it gets harder to track what is included in each step.

# ------------------------------------------------------------------------------------------------------------------
# Script to create a new snapshot on multiple VM's.
# By Wesley van Ede, dutchvblog.com
# ------------------------------------------------------------------------------------------------------------------
 
$name = "Name of the snapshot"
$desc = "
- Multi-line
- description to
- keep track
- of what is included in the snapshot
- Stuff from 7.4 (before upgrade to 7.5)
"
 
Import-Csv C:\powercli\vravms.txt | Foreach-Object {
 
    New-Snapshot -VM $_.vmname -Name $name -Confirm:$false -Description $desc -RunAsync:$true
}

Note that this script uses a text (csv) file called vravms.txt. This holds all VM’s that need to get snapshotted. Note that the first line is the column vmname and the rest are the actual VM names, makes sense?

"vmname"
"vra-vm1"
"vra-vm2"
"vra-vm3"
"web-1"
"web-2"
"mgr-1"
"mgr-2"
"agent-1"
"agent-2"
"dem-1"
"dem-2"
"iaas-db"

Startup vRA

Now let’s see how to start the complete vRA environment in the correct order with the correct amount of time in between the different VM’s. The time-outs are needed because some elements need more time to start and if you are too quick it might not work as expected. Of course the times can vary depending on the speed of your environment. But I found this works fine for most environments.

# ------------------------------------------------------------------------------------------------------------------
# Script to start vRA 7 environment in correct order with correct timing.
# By Wesley van Ede, dutchvblog.com
# ------------------------------------------------------------------------------------------------------------------
 
Start-vm -vm "IaaS DB" -Confirm:$false
Sleep -Seconds 10
Start-vm -vm "vRA VA 1" -Confirm:$false
Sleep -Seconds 20
Start-vm -vm "vRA VA 2" -Confirm:$false
Sleep -Seconds 20
Start-vm -vm "vRA va 3" -Confirm:$false
Sleep -Seconds 300
Start-vm -vm "IaaS Web 1" -Confirm:$false
Sleep -Seconds 30
Start-vm -vm "IaaS Web 2" -Confirm:$false
Sleep -Seconds 240
Start-vm -vm "IaaS Mgr/Dem-o 1" -Confirm:$false
Sleep -Seconds 30
Start-vm -vm "Iaas Mgr/Dem-o 2" -Confirm:$false
Sleep -Seconds 240
Start-vm -vm "IaaS Agent 1" -Confirm:$false
Sleep -Seconds 20
Start-vm -vm "IaaS Agent 2" -Confirm:$false
Sleep -Seconds 10
Start-vm -vm "IaaS Dem-w 1" -Confirm:$false
Sleep -Seconds 10
Start-vm -vm "IaaS Dem-w 2" -Confirm:$false

Reverting snapshots

Last but not least, reverting snapshots. If you need to go back and forth between two states this might com in handy. This script allows you to revert all VM’s back to the previously created snapshot. This uses the same input file as the snapshot creation so you always snapshot and revert the same VM’s to minimise inconsistency.

# ------------------------------------------------------------------------------------------------------------------
# Script to revert the latest snapshot for list of VM's.
# By Wesley van Ede, dutchvblog.com
# ------------------------------------------------------------------------------------------------------------------
 
Import-Csv vravms.txt | Foreach-Object {
 
    $snap = Get-Snapshot -VM $_.vmname | sort-object -Property Created -Descending | Select -First 1
        Set-VM -VM $_.vmname -Snapshot $snap -Confirm:$false    
    }

UPDATE: There was a line missing from this script, this is now fixed

Conclusion

Alright, I know these are not as elaborate as the previous scripts but they have been very helpful. If you need to stop, snapshot, start, revert, start a few times a day these are a welcome addition to your tools. Another reason I posted this is because I see a lot of interest in the PowerCLI scripts from my previous post, this tells me people are looking for things like this so I just share if I think it is useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

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