Using Subversion with PowerCLI for vCentre operations

I thought I would write a post about using Subversion (SVN)  with PowerCLI for managing changes to Infrastructure, with the aim of reducing report emails in my inbox, and reduce the amount of storage space required to store a set of historical records about events that have happened on my vCentre environment.

PowerCLI allows you to interact in a scripted manner from a commandline window with your VMware ESXi estate, generating custom reports or pulling back status information amongst lots of other functionality. I installed PowerCLI onto my vCentre management server.

To that end I wrote a post about building subversion and DAV_svn on SLES 11 SP2-x86 from source on my test environment to store a repository of the original files and the diffs of any changes to those files.

I did try to use TortoiseSVNs TortoiseProc.exe on the vCentre Admin server, but found that because its primarily for GUI-based operations, you are still required to click the OK button after scripting a commit. So I abandoned that approach and went straight for the subversion command line client for windows.

Once I installed the subversion client for windows available from http://sourceforge.net/projects/win32svn and checked that I could do a series of manual checkouts,adds,updates and commits to the SVN server I created in my previous post, I went on to amend my existing PowerCLI scripts to incorporate version control functionality.

In the script I provide you will notice that I have not included any initial ‘add’, as I manually performed that step during testing. – However, the script could be amended to allow you to do that too.

So my first step was to checkout a working copy of the repository I created for changes to my vCentre environment. For this I created a folder on the vCentre Admin server at C:workingcopy,  then did an:

svn checkout http://<svnserverip>/repos/repos --username myuser

Initially this was an empty repository, so I manually copied over a few test files that I already had from PowerCLI generated daily mails I have about changes to infrastructure into my working copy on my vCentre Admin server, then did an:

svn add Matt_snapshotlist_VMInfo.html

Next I added the SVN integration into my PowerCLI script. You can see it at the very end of the script below. – You might want to modify the script to use Git/bitbucket if you choose to!

Add-PSSnapin VMware.VimAutomation.Core
Remove-Item "C:workingcopyMatt_snapshotlist_VMInfo.html" -Force -ErrorAction SilentlyContinue
disconnect-VIServer *
$vcserver1="x.x.x.xx"
$vcserver2="x.x.x.xx"
$vcserver3="x.x.x.xx"
$vcserver4="x.x.x.xx"
$vcserver5="x.x.x.xx"
$vcserver6="x.x.x.xx"
$vcserver7="x.x.x.xx"

connect-VIServer $vcserver1 -User myuser -Password password
connect-VIServer $vcserver2 -User myuser -Password password
connect-VIServer $vcserver3 -User myuser -Password password
connect-VIServer $vcserver4 -User myuser -Password password
connect-VIServer $vcserver5 -User myuser -Password password
connect-VIServer $vcserver6 -User myuser -Password password
connect-VIServer $vcserver7 -User myuser -Password password
$report = @()
foreach($vm in Get-VM)
{
$GetSnaps = $vm|Get-Snapshot| Select VM, Name, Description, @{N="DaysOld"; E={((Get-Date) - $_.Created).Days}} | ? {$_.DaysOld -gt 7 }
$report += $GetSnaps
}
disconnect-VIServer $vcserver1 -Confirm:$false
disconnect-VIServer $vcserver2 -Confirm:$false
disconnect-VIServer $vcserver3 -Confirm:$false
disconnect-VIServer $vcserver4 -Confirm:$false
disconnect-VIServer $vcserver5 -Confirm:$false
disconnect-VIServer $vcserver6 -Confirm:$false
$myStyle = @"
<title>ESXi Snapshot Age Report</title>
<style>
body {background-color: grey;}
table {border-collapse: collapse; border-width: 1px;
border-style: solid; border-color: black;}
tr {padding: 5px;}
th {border-width: 1px; border-style: solid; border-color: black;
background-color: blue; color: white;}
td {border-width: 1px; border-style: solid; border-color: black;
background-color: white;}
</style>
"@
$report| ConvertTo-Html -Body $body -Head $myStyle|Set-Content "C:workingcopyMatt_snapshotlist_VMInfo.html"

$svnprogA = "svn.exe"
$svnArgumentsA = @(
'update'
'--username <myusername>'
'--password <mypassword>'
'c:workingcopy'
)
start-process $svnprogA -ArgumentList $svnArgumentsA
$svnprog = "svn.exe"
$svnArguments = @(
'commit -m "committing snapshotage from Subversion for Windows"'
'--username <myusername>'
'--password <mypassword>'
'c:workingcopy'
)
start-process $svnprog -ArgumentList $svnArguments

Next I added the PowerCLI script to the windows task scheduler to run every day.

The great thing about this for me is that it has now stopped a stack of daily emails, but any changes to the information are still logged. If there are no changes day to day, there is no longer any need to have multiple copies of the same file, as Subversion is dealing with all that for me.

I also have the ability to see that any changes are being written away by pulling back a list of the latest commits using ‘svn log’

svn log -v -l 5

If I want to see what changes happened week to week I can use ‘svn diff’ to check changes from version 1.0 to version 1.1,etc.

svn diff -r 1:2 Matt_snapshotlist_VMInfo.html.

This also means that I have a method of going back in time potentially to restore a configuration to a previous state.  – I know I can get similar functionality from vCentre/VMware specific tools, but if I want to create something quick and bespoke I now have that ability!

Hope you found the article interesting!

You can grab this great book about using PowerCLI to strengthen what you can to with vSphere/ESXi.

 

(c) Matt Palmer 15-May-2013

 

 

[EDIT]: If you are running on v5.1 PowerCLI you may want to take a look at my post here:
PowerCLI v5.1 Guest VM Export script As it details a few changed that will need to be made to the code shown above, if you are running a more recent version of PowerCLI.