Monitoring with PowerShell Chapter 2: Hyper-V replication and remediation

Hi All,

As I’ve explained in my previous the series is taking a bit of a turn here and we’re going to start some blogs about remediation instead of just monitoring. I’ll link back to a previous blog and will explain how we automatically react to these issues within our RMM, if you do not have an RMM – Don’t worry! We’ll include the monitoring + remediation script so you can combine the scripts any way you’d like.

The first monitoring and remediation we’re getting on is Hyper-V replication, where we will try to resolve the replication if the server was unreachable for some time, force a resync, and clear the statistics.

We’re getting started on monitoring the current Hyper-V Replication state, to do this we use the following small script:


$VMList = Get-VMReplication  -ErrorAction SilentlyContinue

ForEach ($VM in $VMList)	{
if ($VM.health -eq "Critical")	{ $critical = $critical + 1	}
if ($VM.health -eq "Warning")	{ $warning = $warning + 1   }
if ($VM.state -ne "Replicating"){ $Critical = $Critical + 1 }
                                }
if($critical -gt 0){$Critical = "unhealthy"}
if($warning -gt 0){$warning = "unhealthy"}

if(!$critical ){ $Critical  = "Healthy"}
if(!$warning ){ $warning  = "Healthy"}

Now this script gives us the result we need to start our automatic remediation path; We know that VM’s are not replicating if we have a state of $Critical = unhealthy, and we know that we have Replication Warnings (Most likely related to statistics) when $warning is set to unhealthy. At this point in time we’ll get a fork in the road and some options; do we want our monitoring script to also be our remediation script, or do we want separate scripts that are launched based on the condition of the replication?

If you want the former, the script only needs a couple of extra lines, but monitors AND resolves the issues immediately when found, I do not like this approach very much as it can give you skewed results and you do not control when/how the remediation runs.


$VMList = Get-VMReplication  -ErrorAction SilentlyContinue

ForEach ($VM in $VMList)	{
                if ($VM.state -match "WaitingForStartResynchronize"){ Resume-VMReplication -VMName $VM.Name -Resynchronize -Verbose   }
		if ($VM.health -eq "Critical")	{ $critical = $critical + 1	}
		if ($VM.health -eq "Warning")	{ $warning = $warning + 1   }
                if ($VM.state -ne "Replicating"){ $Critical = $Critical + 1 }
                               }
if($critical -gt 0){$Critical = "unhealthy"}
if($warning -gt 0){$warning = "unhealthy"}

if(!$critical ){ $Critical  = "Healthy"}
if(!$warning ){ $warning  = "Healthy"}


ForEach ($State in $ReplState)
{
$Name = $State.Name
$CurrentState = $State.State
}
}

After running this script you will most likely notice that it’s still giving you a warning, you can decide to wait for the replication to run long enough to resolve this on its own, or you can decide to reset the statistics right before self-healing:


$VMList = Get-VMReplication  -ErrorAction SilentlyContinue

ForEach ($VM in $VMList)	{
					write-host $VM.name is $VM.health
                    if ($VM.state -match "WaitingForStartResynchronize"){
                    Reset-VMReplicationStatistics -VMName $Name
                    Resume-VMReplication -VMName $Name -Resynchronize -Verbose
                    }
					if ($VM.health -eq "Critical")	{ $critical = $critical + 1	}
					if ($VM.health -eq "Warning")	{ $warning = $warning + 1   }
                    if ($VM.state -ne "Replicating"){ $Critical = $Critical + 1 }


if($critical -gt 0){$Critical = "unhealthy"}
if($warning -gt 0){$warning = "unhealthy"}

if(!$critical ){ $Critical  = "Healthy"}
if(!$warning ){ $warning  = "Healthy"}


ForEach ($State in $ReplState)
{

$Name = $State.Name
$CurrentState = $State.State



}
}

If you decided to split the scripts as per my advice, you can use the first script in this blog post to use as your monitoring set, and the following script as your automated resolution script, remember that this is only the resolution in case the replication failed due to the other side being unreachable, and does not resolve certificate errors or other errors.



$ReplState = Get-VMReplication
ForEach ($State in $ReplState) {
$ping = Test-Connection -computername $State.ReplicaServerName
if (-Not $ping ) { write-host "Cannot Ping $($state.ReplicaServerName)" }
else {
$Name = $State.Name
$CurrentState = $State.State
if ($State -match "WaitingForStartResynchronize"){
Reset-VMReplicationStatistics -VMName $State.Name
Resume-VMReplication -VMName $State.Name -Resynchronize -Verbose
}
}
}

And that’s it! Have fun ?

Recent Articles

The return of CyberDrain CTF

CyberDrain CTF returns! (and so do I!)

It’s been since september that I actually picked up a digital pen equivalent and wrote anything down. This was due to me being busy with life but also my side projects like CIPP. I’m trying to get back into the game of scripting and blogging about these scripts. There’s still so much to automate and so little time, right? ;)

Monitoring with PowerShell: Monitoring Acronis Backups

Intro

This is a monitoring script requested via Reddit, One of the reddit r/msp users wondered how they can monitor Acronis a little bit easier. I jumped on this because it happened pretty much at the same time that I was asked to speak at the Acronis CyberSummit so it kinda made sense to script this so I have something to demonstrate at my session there.

Monitoring with PowerShell: Monitoring VSS Snapshots

Intro

Wow! It’s been a while since I’ve blogged. I’ve just been so swamped with CIPP that I’ve just let the blogging go entirely. It’s a shame because I think out of all my hobbies it’s one I enjoy the most. It’s always nice helping others achieve their scripting target. I even got a couple of LinkedIn questions asking if I was done with blogging but I’m not. Writing always gives me some more piece of mind so I’ll try to catch up again. I know I’ve said that before but this time I’ll follow through. I’m sitting down right now and scheduling the release of 5 blogs in one go. No more whining and no more waiting.