# Last Updated: 2022/12/09 # NOTES: # - You can have this script run locally on the Hyper-V server itself, or you can run it on a monitoring box and pass in an array of Hyper-V host names that it should check. # - The activity body will contain a bunch of VMs with "Normal" in the Health column if the replication is healthy, so the recommended Success criteria in the # CheckCentral check configuration would be (match all): # - Body Text - Contains - Normal # - Body Text - Doesn't Contain - Warning # - Body Text - Doesn't Contain - Critical ################### # Script Settings # ################### # Set the allowed TLS versions $TLSProtocol = [System.Net.SecurityProtocolType] 'Ssl3 , Tls, Tls11, Tls12, Tls13' [System.Net.ServicePointManager]::SecurityProtocol = $TLSProtocol # CheckCentral API key $apiToken = "" # Set your Hyper-V hosts here # If running it locally on a single host, you can leave it blank, like this: $hvHosts = @() $hvHosts = @("hvhost01", "hvhost02", "hvhost03") #################################################### # Script Processing (Don't modify below this line) # #################################################### function createCCActivity ($activitySubject, $activityBody) { Write-Output "`n[INFO] Creating the activity in CheckCentral...`n" # Build the activity object $ccActivity = [PSCustomObject]@{ activities = @( [PSCustomObject]@{ subject = $activitySubject body = $activityBody } ) } # Make the API call $activityBodyJson = $ccActivity | ConvertTo-Json $ccCreateActivitiesRequest = Invoke-WebRequest -Uri "https://api.checkcentral.cc/createActivities/?apiToken=$apiToken" -Method "POST" -ContentType "text/json" -Body $activityBodyJson # Confirm that the number of created activities matches the number of activities we sent in $ccActivitiesRequestJson = $ccCreateActivitiesRequest.Content | ConvertFrom-Json if ($ccActivitiesRequestJson.activities_saved -lt $ccActivitiesRequestJson.activities_received) { Write-Output "Failed to create $($ccActivitiesRequestJson.activities_received - $ccActivitiesRequestJson.activities_saved) activitie(s) in CheckCentral" } } # If the $hvHosts array is empty, set it to the hostname of the machine this script is running on if ($hvHosts.Length -eq 0) { $hvHosts = @("$env:COMPUTERNAME") } # Loop through the $hvHosts array to get the replication status for each host and send it to CC foreach($hvHost in $hvHosts) { # Set the activity subject $subject = "Hyper-V Replication Status: $hvHost" # Get the replication status from the host Write-Output "[INFO] Getting replication status for $hvHost..." $replicationStatus = Get-VMReplication -ComputerName $hvHost | Out-String if ($replicationStatus -eq "") { $replicationStatus = "No VMs are setup for replication on this server." } # Append the hostname of the machine that ran this script to the activity body $replicationStatus += "Report generated on $($env:COMPUTERNAME) ($(Get-Date))" # Send the activity to CC createCCActivity $subject $replicationStatus }