75 lines
2.6 KiB
PowerShell
75 lines
2.6 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Check the status of VM Replications.
|
|
|
|
.DESCRIPTION
|
|
This script will check the replication status on VM's primairy and secondairy replication on the host the script is running on,
|
|
This script should be deployed as a Check Script.
|
|
|
|
.OUTPUTS
|
|
Errorcodes:
|
|
0 - All OK
|
|
1 - There is an error in one of the replications
|
|
|
|
.EXAMPLE
|
|
Check_scripts/Win - Hyper-V replication health.ps1
|
|
|
|
.NOTES
|
|
Author: D.de Kooker <info@dcomputers.nl>
|
|
Source: n/a
|
|
|
|
.CHANGELOG
|
|
17-09-2023 - Initial script.
|
|
#>
|
|
|
|
# Import the Hyper-V module if not already loaded
|
|
if (-not (Get-Module -Name Hyper-V -ListAvailable)) {
|
|
Import-Module Hyper-V
|
|
}
|
|
|
|
# Specify the Hyper-V server you want to check
|
|
$HyperVServer = "localhost" # Use "localhost" to check the local Hyper-V server
|
|
|
|
# Get a list of all replicated virtual machines on the specified Hyper-V server
|
|
$ReplicatedVMs = Get-VMReplication -ComputerName $HyperVServer
|
|
|
|
$ErrorFound = $false # Initialize an error flag
|
|
|
|
# Loop through each replicated VM and check its replication state
|
|
foreach ($VM in $ReplicatedVMs) {
|
|
$ReplicationStatus = Get-VMReplication -VMName $VM.Name -ComputerName $HyperVServer
|
|
$ExtendedReplicationStatus = Get-VMReplication -VMName $VM.Name -ComputerName $HyperVServer -ExtendedReplication
|
|
|
|
Write-Host "VM Name: $($VM.Name)"
|
|
Write-Host "Primary Replication State: $($ReplicationStatus.ReplicationState)"
|
|
Write-Host "Primary Last Successful Replication Time: $($ReplicationStatus.LastReplicationTime)"
|
|
|
|
Write-Host "Extended Replication State: $($ExtendedReplicationStatus.ReplicationState)"
|
|
Write-Host "Extended Last Successful Replication Time: $($ExtendedReplicationStatus.LastReplicationTime)"
|
|
|
|
# Check for errors in primary replication state
|
|
if ($ReplicationStatus.ReplicationState -ne "Normal") {
|
|
$ErrorFound = $true
|
|
$LastError = $ReplicationStatus.LastErrorDescription
|
|
Write-Host "Last Primary Error: $LastError"
|
|
}
|
|
|
|
# Check for errors in extended replication state
|
|
if ($ExtendedReplicationStatus.ReplicationState -ne "Normal") {
|
|
$ErrorFound = $true
|
|
$LastExtendedError = $ExtendedReplicationStatus.LastErrorDescription
|
|
Write-Host "Last Extended Error: $LastExtendedError"
|
|
}
|
|
|
|
Write-Host "------------------------"
|
|
}
|
|
|
|
# Output the error code based on whether errors were found or not
|
|
if ($ErrorFound) {
|
|
Write-Host "Errors were found in VM replication."
|
|
exit 1 # Use exit code 1 to indicate errors found
|
|
} else {
|
|
Write-Host "No errors found in VM replication."
|
|
exit 0 # Use exit code 0 to indicate no errors found
|
|
}
|