Long Title… But first things first.
Ben Meadowcroft created several cmdlets and even a module using the SRM API. Having spent a good deal of time in the SRM API myself, let me tell you….IT IS A MESS. Long story short Ben, spent a lot of time creating this really helpful module, and you should buy him a beer sometime.
Also in order to use my script you need his module 🙂
Alright, let’s get to it.
First things first, what is the power on command that we want to run
$scriptblock={Ipconfig /all}
It isn’t very exciting, but it is something. Really though, this could be anything. It could be a batch file, or a ps1 script. Options are pretty limitless. So for now this will be our basic post power on command.
Next we will make our srm command.
$srmcommand=new-command -command $scriptblock -description "WinRecovery" -runinrecoveredvm -timeout 300
This will be our SRM command that is applied to protected VMs. The parameters are pretty straightforward.
We start getting into some loops here so it is hard to continue breaking up the script, so let’s see the rest of it and I can explain.
add-pssnapin vmware.vimautomation.core Import-Module E:\scripts\Meadowcroft.SRM\Meadowcroft.Srm.psm1 Connect-viserver "ProtectionServer" Connect-srmserver $scriptblock={Ipconfig /all} $srmcommand=new-command -command $scriptblock -description "WinRecovery" -runinrecoveredvm -timeout 300 #Grabs All SRM Plan $plans=Get-RecoveryPlan FOREACH ($plan in $plans) { #Retrieves all Windows VMs from each Plan $winvms=$plan |get-protectedvm |? {($_.vm.guest.guestfamily -like "*win*")} FOREACH ($winvm in $winvms) { $vmname=$winvm.vm.name $vmname $srmpostcommand=$srmcommand #We need to grab the recovery settings for the VM in order to set them later. $settings=get-recoverysettings -ProtectedVm $winvm -RecoveryPlan $plan #Retrieves Current Post Power On Commands $postcommands=$settings.postpoweroncallouts |% { $_ } #Remove All Currently assigned Recovery Commands ForEach ($postcommand in $postcommands) {remove-postrecoverycommand -recoverysettings $settings -command $postcommand} #Sets the Recovery Commands Add-PostRecoveryCommand -RecoverySettings $settings -command $srmpostcommand Set-RecoverySettings -RecoveryPlan $plan -ProtectedVm $winvm -RecoverySettings $settings } }
Cool! So let’s talk about a few things
Line 3: The vCenter server needs to be the server at the Protected Site, not the Recovery Site.
Line 4: Will connect automatically to the SRM server currently attached to the vCenter from Line 3.
Line 15: Here I pulled the VMs that are Windows VMs, but this could easily be anything you want to filter on. “$_.vm” becomes just the regular VM object we all know and love. This allows us to filter just as we would a regular VM in vCenter.
Line 22:Merely here because I wanted to know which VM it was currently setting.
Line 23: I had some additional filters here so I just removed them for these purposes.
Lines 31-33: When setting the post power on command, it does not overwrite what is already there. It only adds to them. This meant while I was perfecting what my post power on command, I needed to delete what was already set each time.
Line 36: Adds the SRM Command
Line 38: Sets the recovery settings for the VM with the newly added(line 36) Post Power on Command.
Fun stuff. Hopefully my dive into SRM cmdlets make life easier for you!
0 Comments