Powershell is addicting
It's been a while since my last tech post, but I couldn't help this one...
Recently I have more or less fallen into the role of maintaining the infrastructure at our company. We recently got decent server to run a few Virtual Machines on. I've had mixed feelings on VMs, but that is a story for another day. We decided to go with VMWare ESX, which after 3 VM's is humming along smoothly. One of the things we needed to do was make sure our VMs were backed up properly. I am aware that ESX has this baked into it, but we aren't interested in backing up the whole VM, just a single folder in most cases. We recently accomplished this with a somewhat complex BAT script.
I decided to see if PowerShell could make things easier.
At first, I found powershell a bit frustrating. After a while though, I found it a very effective way to get something done. For instance, I needed to loop through several SVN repositories, run
svnadmin hotcopy
on them, then copy that across a network. That was really a breeze with powershell, whereas it would be a bit more difficult in a BAT script, if not hard to maintain and read. This is how I accomplished the task:
$repositoriesPath = "C:\source\repositories\"$backupPath = "C:\backups\"$repositories = get-ChildItem $repositoriesPath | where {$_.PsIsContainer}foreach($repository in $repositories){$repositoryDirectory = $backupPath + $repositoryif (Test-Path $repositoryDirectory){Remove-Item -path $repositoryDirectory -recurse -force}New-Item $repositoryDirectory -type directoryecho "Hotcopying repository $repository"svnadmin.exe hotcopy ($repositoriesPath + $repository) $repositoryDirectory}
The reason for the hotcopy is that it is generally a bad idea to read the SVN repositories directly. If you attempt to backup a repository during a transaction, you might end up with a wedged Berkley DB or even a corruption.
This script makes sense to me, it's easy to maintain and change if needed. And since we have full access to the .NET BCL, we can easily newup a MailMessage object and send a confirmation email that the backup was successful, or that it failed and needs to be fixed.
For a 1.0 release of powershell, I am very impressed. This was actually my first time using powershell, and wanted to see how practical it was.