# Hyper-V Replication Status Output

**URL:** <https://forums.powershell.org/t/hyper-v-replication-status-output/20333>\
**Category:** PowerShell Help\
**Created:** [September 2, 2022, 3:34pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333 "2022-09-02T15:34:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LukeMelling](https://avatars.discourse-cdn.com/v4/letter/l/e480ec/32.png) [@LukeMelling](https://forums.powershell.org/u/LukeMelling)\
**Post date:** [September 2, 2022, 3:34pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/1 "2022-09-02T15:34:30Z")

</div>

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  
#\>

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 2, 2022, 4:09pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/2 "2022-09-02T16:09:47Z")

</div>

Luke,  
Welcome to the forum. 👋🏽

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](https://aws1.discourse-cdn.com/business5/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

> [@LukeMelling](#):
>
> Can you see why this would be happening?

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. 😉

---

<div class="post-metadata">

**Author:** ![LukeMelling](https://avatars.discourse-cdn.com/v4/letter/l/e480ec/32.png) [@LukeMelling](https://forums.powershell.org/u/LukeMelling)\
**Post date:** [September 2, 2022, 6:34pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/3 "2022-09-02T18:34:33Z")

</div>

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

```auto
$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:

```auto
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…

```auto
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.

---

<div class="post-metadata">

**Author:** ![LukeMelling](https://avatars.discourse-cdn.com/v4/letter/l/e480ec/32.png) [@LukeMelling](https://forums.powershell.org/u/LukeMelling)\
**Post date:** [September 2, 2022, 6:45pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/4 "2022-09-02T18:45:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 2, 2022, 6:56pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/5 "2022-09-02T18:56:14Z")

</div>

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:

```auto
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. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/hyper-v-replication-status-output/20333/6 "2024-05-16T20:16:39Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


