IT infrastructures and services management was and still is today an important part of my ‘business as usual‘ set of activities. Regardless of the tools and technologies implemented what I look for are always two features for monitoring and automation, simplicity and efficiency.
If monitoring is essential for having visibility on the overall infrastructure and for being proactive, automation is necessary for delivering consistent results in a timely manner especially in complex scenarios.
Any programming language is a secret weapon from complex monitoring or basic automation task, but as we all know… with great power comes great responsibility! Every line of code needs to be tested and hopefully maintained for years to come and needs to survive to its own creator.
This article will mix a bit of product knowledge and few powershell lines of code with the aim of getting the best of both with the best results and no compromises.
After a few years of usage, I wanted to share my positive impressions on a couple of products Microsoft System Center Virtual Machine Manager(SCVMM) and VEEAM Backup and Replication ( B&R – VeeamZip). These aren’t the only solutions for virtualisation and backup that I use, but for a number of reasons are the one that I use for this article, but you can easily modify this script to work with your backups solution.
VEEAM
I spoke about VEEAM before, there are a lot of valid competitors, but I’m really a fan and I love the simplicity of their products: https://www.scriptinglibrary.com/uncategorized/powershell-veeam-backup-and-replication-veeamzip/
SYSTEM CENTER VIRTUAL MACHINE MANAGER
I give for granted that you’re familiar with it and you’re currently using it. If that is not the case a good place to start is the official documentation:
- https://docs.microsoft.com/en-us/powershell/module/virtualmachinemanager/?view=systemcenter-ps-2019
- https://docs.microsoft.com/en-us/system-center/vmm/?view=sc-vmm-2019
I think VMM is a product that has a huge potential and with a really small effort be really the central management for your infrastructure.
If you manage from a few hundred to thousands of VM it will very hard to manage the manually without a centralized solution and good governance.
By governance, I mean a structured approach, processes, and workflows that will determine the lifecycle of a VM from the moment where it was provisioned and when will be potentially decommissioned. If System Center VMM offers templates to automate the provision of Virtual Machines and VEEAM can create a compressed backup of that VM these tools are performing independent tasks, separate configurations and separate reports.
How to Integrate SCVMM and VEEAM (or other tools)
I chose not to create a third tool for getting the visibility that I need because PowerShell is the common denominator of both tools or depend on a third-party tool for displaying a dashboard-like interface like Power BI for example.
Hyper V has no custom attributes for VMs, but via SCVMM you can add TAGs, Cost Center and any other meaningful field like owner, department, project, reference to other app or your own one like the JIRA ticket that started the automated or manual deployment, and like in this case when the ‘LastBackup’ was performed like in this example.
In VMM if you select one VM you can choose Custom Properties and Manage Custom properties and add “LastBackup”. That custom properties will be available for all your VMs, if you want to have an idea of the code you need to write you can also visualise the code clicking on the “View Script” button.
Now that you have your property for each VM you need to fetch the right information from the Backup solution. In this context, the source of truth can be a database or a filesystem. In my example, I chose the backup repository that is on a file share.
The backup files of VEEAM have a quite consistent naming convention, I said quite consistent because is changed over this couple of years.
For instance, if you list your server backup the format could be either of these:
- YOURSERVERD2018-01-01T08300.vbk
- YOURSERVERD2019-01-01T08300_XXXX.vbk
If in your repository you have multiple backup files filtering by file extension (.vbk) and removing all backups that are not completed (<22MB) is a good idea.
In my case, I’ve listed my list of full backups performed by VEEAM sorting by name in descending order, storing the result into an hash-table and using a try-catch helped my to keep just the properties I need Name and DateTime for my Key-Value pair.
The result is a small script of 50 lines of code, simple to maintain and easy to customise or extend if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Import-Module -Name "virtualmachinemanager" #Paolo Frigo, https://www.scriptinglibrary.com # SETTINGS $BackupRepository = "\\YOUR\REPOSITORY\" $VMList = Get-SCVirtualMachine -VMMServer "scvmm.fqdn" function Split-VEEAMBackupFile($VMBackupFileName){ if ($VMBackupFileName.Substring($VMBackupFileName.length-9,1) -eq "_"){ $VeeamDateSize = 27 } else{ $VeeamDateSize = 22 } $VMNAme = $VMBackupFileName.Substring(0,$VMBackupFileName.Length-$VeeamDateSize) $DateString = $VMBackupFileName.Substring($VMNAme.Length+1, 10) $TimeString = "$($VMBackupFileName.Substring($VMNAme.Length+12,2)):$($VMBackupFileName.Substring($VMNAme.Length+14,2)):$($VMBackupFileName.Substring($VMNAme.Length+16,2))" $Datetime = "$DateString $Timestring" | Get-Date return $VMNAme,$Datetime } function Set-LastBackupProperty { param( [Parameter(Mandatory=$true, Position=0)] [string] $VMName, [Parameter(Mandatory=$true, Position=1)] [string] $LastBackupTime ) $VM = $VMList | Where-Object {$_.Name -match "$VMName"} #Get-SCVirtualMachine -Name "$VMName" $CustomProp = Get-SCCustomProperty -Name "LastBackup" $VM | Set-SCCustomPropertyValue -CustomProperty $CustomProp -Value "$LastBackupTime" } $BackupFiles = Get-ChildItem $BackupRepository |Sort-Object Name -Descending | Where-Object {$_.Name -like "*.vbk" -and $_.Length -gt 22MB}| Select-Object -exp name $VeeamFullBackup = @{} foreach ($VMBackupFileName in $BackupFiles){ $BackupRecord = Split-VEEAMBackupFile($VMBackupFileName) try{ $VeeamFullBackup.add($BackupRecord[0],$BackupRecord[1]) } catch{ #Write-warning "The Backup for $($BackupRecord[0]) has already a more recent backup than the one taken at $($BackupRecord[1]) " } } foreach ($vmname in $VeeamFullBackup.Keys){ $Datetime = $VeeamFullBackup["$vmname"].ToString("dd/MM/yyyy hh:mm") #Write-Output "Setting for $vmname the LastBackup custom Property to $Datetime" Set-LastBackupProperty -VMNAme "$vmname" -LastBackupTime "$Datetime" } exit 0 |
According to your needs you can schedule to run this script to update your VMM properties as many times per day you want.
WRAP UP
Simple scripts or customisations don’t replace third-party tools (like VEEAM ONE in this case) or bigger managed solutions but can be exactly what you need with little o no maintenance required and can have quite a big impact on the result you can achieve using exactly the same tool.
As usual this script is available on my GitHub repository.