Hyper-V Replication Status Output

Hi guys, I have the following script, if I check the variable $ReplicatingVMs it returns everything within the “VMName” output, but if I run the $FailedVMs variable it only returns one of the machines failing to replicate. Can you see why this would be happening?

$FailedVMs = “Replication Failures: Not_Set”
$HRReportFailure = 0
$FailedVMs = “”
$Replicating_VMs = Measure-VMReplication

$Replicating_VMs | ForEach-Object {
if ($.State -ne ‘Replicating’) {
$HRReportFailure = 1
if ($FailedVMs = “Replication Failures: Not_Set”) {
$FailedVMs = $
.VMName
} else {
$FailedVMs = ", " + $_.VMName
}
}
}

if ($HRReportFailure -ne 0) {
$FailedVMs = "Replication Failures: " + $FailedVMs
} else {
$FailedVMs = “No failures, Hyper-V replication is healthy”
}
<# Debug variables
$FailedVMs
$Replicating_VMs
#>

Luke,
Welcome to the forum. :wave:t4:

As you might have noticed the forum software messed up your code. So please … when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Without having experiences with Hyper-V-Replications it looks like you’re overcomplicating this task a lot. What do you actually wnat to achieve? If I got it right you would only need to use Where-Object or Group-Object to get the information you’re looking for.

Regardless of that - could you please share some (sanitized) sample data along with your code (formatted as code as well). So we might be able to reproduce your issue and hopefully fix it. :wink:

Hi Olaf, thanks for the warm welcome.
I’ve re-pasted the script below.

$FailedVMs = "Replication Failures: Not_Set"
$HRReportFailure = 0
$FailedVMs = ""
$Replicating_VMs = Measure-VMReplication

$Replicating_VMs | ForEach-Object {
    if ($_.State -ne 'Replicating') {
        $HRReportFailure = 1
        if ($FailedVMs = "Replication Failures: Not_Set") {
            $FailedVMs = $_.VMName
        } else {
            $FailedVMs = ", " + $_.VMName
        }
    }
}

if ($HRReportFailure -ne 0) {
    $FailedVMs = "Replication Failures: " + $FailedVMs
} else {
    $FailedVMs = "No failures, Hyper-V replication is healthy"
}

When I run the script manually, the output should be based on the “FailedVMs” variable.
If I run the variable manually then this is the output:

PS C:\Windows\system32> $FailedVMs
Replication Failures: RDS04

If I run the actual command “Measure-VMReplication”, this is the output and you can see the $FailedVMs variable should return more than the one VM…

VMName       State                        Health   LReplTime           PReplSize(M) AvgLatency AvgReplSize(M) Relationship
------       -----                        ------   ---------           ------------ ---------- -------------- ------------
EMS01 Error                        Critical 22/08/2022 15:28:07 95,558.79               0.00           Simple      
RDG01 WaitingForStartResynchronize Critical 15/08/2022 16:19:57 0.00                    0.00           Simple      
RDS04 WaitingForStartResynchronize Critical 15/08/2022 16:25:40 0.00                    0.00           Simple

Let me know if I can provide further information which may help.

Sorry I forgot to mention as well, this is a script we’ll be running in our remote management platform to check the status and flag any VM’s which are failing replication at the moment.

Hmmm … if I got it right you just want to get all VMs with a status other than Replicating. So this should be enough then:

Measure-VMReplication | 
    Where-Object { $_.State -ne 'Replicating'} |
        Select-Object -Property Name

Of course you can collect the output in a variable for further steps or enrich the output with some nice and fluffy prosa around if needed. :wink: