Guarda una demo×
×

Guarda NinjaOne in azione!

Inviando questo modulo, accetto La politica sulla privacy di NinjaOne.

Ottimizza la tua gestione IT: Padroneggiare il monitoraggio della replica Hyper-V con PowerShell

Punti chiave

  • Monitoraggio automatico della replica Hyper-V: Lo script automatizza il processo di monitoraggio dello stato di replica Hyper-V, migliorando l’efficienza nella gestione dell’ambiente virtuale.
  • Soglie di avviso personalizzabili: Gli amministratori possono impostare soglie personalizzate per gli avvisi di replica utilizzando parametri come -FailedFor.
  • Sono necessarie autorizzazioni elevate: L’esecuzione dello script per monitorare lo stato della replica Hyper-V richiede i privilegi di amministratore per accedere alle informazioni dettagliate sulla replica delle VM.
  • Personalizzazione specifica dell’ambiente: Utilizza variabili d’ambiente e campi personalizzati per adattare la funzionalità dello script per monitorare lo stato della replica Hyper-V ad ambienti IT specifici.
  • Strumento di gestione IT proattivo: Lo script per monitorare lo stato della replica Hyper-V funge da strumento proattivo per i professionisti IT, garantendo l’identificazione e la risoluzione tempestiva dei problemi di replica.
  • Miglioramento rispetto ai metodi manuali: Offre un approccio più completo e automatizzato rispetto ai controlli manuali o agli script di base.
  • Parte integrante della gestione dell’infrastruttura IT: Se integrato con piattaforme come NinjaOne, lo script per monitorare lo stato della replica Hyper-V aggiunge un valore significativo alla gestione e alla protezione dell’infrastruttura IT.
  • Richiede PowerShell 5.1 o versioni più recenti: Assicurati compatibilità e disponibilità di tutte le funzionalità usando la versione appropriata di PowerShell.

La gestione efficace degli ambienti virtuali è fondamentale per i professionisti IT, in quanto garantisce prestazioni e sicurezza ottimali. Un aspetto fondamentale è il monitoraggio e la gestione della replica Hyper-V, una funzionalità ampiamente utilizzata nelle reti virtualizzate per scopi di ridondanza e failover. Il monitoraggio efficiente dello stato di replica Hyper-V garantisce la continuità aziendale e l’integrità dei dati in caso di guasti imprevisti.

Background

La replica Hyper-V è un meccanismo che consente alle virtual machine (VM) di replicarsi da un host Hyper-V a un altro, fornendo una soluzione di disaster recovery fondamentale. I professionisti IT e i Managed Service Provider (MSP) hanno bisogno di strumenti affidabili per monitorare questo processo di replica. Lo script di cui parliamo serve esattamente a questo scopo, e consente agli amministratori di monitorare in modo efficiente l’integrità e lo stato delle repliche delle virtual machine e di reagire prontamente a eventuali anomalie.

Lo script per il monitoraggio della replica Hyper-V:

#Requires -Version 5.1

<#
.SYNOPSIS
    This will get information about the current status of Hyper-V Replication. If its abnormal it'll check the last replication time to see if it should alert on it.
.DESCRIPTION
    This will get information about the current status of Hyper-V Replication. If its abnormal it'll check the last replication time to see if it should alert on it.
.EXAMPLE 
    (No Parameters)
    Replication is currently failing!
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep 
   tion
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio 
   n,customscript_gen_2.ps1
 

    VMName      PrimaryServer            State   Health LastReplicationTime 
    ------      -------------            -----   ------ ------------------- 
    WIN10-TEST  SRV16-TEST.test.lan      Error Critical 4/13/2023 8:20:11 AM
    Win10-TEST2 SRV16-TEST.test.lan      Error Critical 4/13/2023 8:20:11 AM

PARAMETER: -FailedFor "30"
    Time in minutes any given vm replication is allowed to be abnormal.
    Ex. "20" will alert on a vm replication after its been in the abnormal state for 20 minutes.
.EXAMPLE
    -FailedFor "20"
    WARNING: Some of the vm's currently have replication paused!
 

    VMName      PrimaryServer                  State  Health LastReplicationTime 
    ------      -------------                  -----  ------ ------------------- 
    WIN10-TEST  SRV16-TEST.test.lan      Replicating  Normal 4/13/2023 8:40:04 AM
    Win10-TEST2 SRV16-TEST.test.lan        Suspended Warning 4/13/2023 8:32:06 AM

PARAMETER: -IncludePaused
    Script will consider paused vm's abnormal if this parameter is used.    
.EXAMPLE
    -IncludePaused
    Replication is currently failing!
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep 
   tion
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio 
   n,customscript_gen_2.ps1
 

    VMName      PrimaryServer                  State  Health LastReplicationTime 
    ------      -------------                  -----  ------ ------------------- 
    WIN10-TEST  SRV16-TEST.test.lan      Replicating  Normal 4/13/2023 8:40:04 AM
    Win10-TEST2 SRV16-TEST.test.lan        Suspended Warning 4/13/2023 8:32:06 AM

PARAMETER: -FromCustomField "ReplaceMeWithAnyIntegerCustomField"
    Name of an integer custom field that contains your desired FailedFor threshold.
    ex. "ReplicationAlertThreshold" where you have entered in your desired alert limit in the "ReplicationAlertThreshold" custom field rather than in a parameter.
.EXAMPLE
    -FromCustomField "ReplaceMeWithAnyIntegerCustomField"
    WARNING: Some of the vm's currently have replication paused!
 

    VMName      PrimaryServer                  State  Health LastReplicationTime 
    ------      -------------                  -----  ------ ------------------- 
    WIN10-TEST  SRV16-TEST.test.lan      Replicating  Normal 4/13/2023 8:40:04 AM
    Win10-TEST2 SRV16-TEST.test.lan        Suspended Warning 4/13/2023 8:32:06 AM
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Server 2016
    Release Notes: Renamed script and added Script Variable support
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/it/condizioni-utilizzo
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [int]$FailedFor = "60",
    [Parameter()]
    [String]$FromCustomField,
    [Parameter()]
    [Switch]$IncludePaused = [System.Convert]::ToBoolean($env:includePausedReplications)
)
begin {

    if ($env:allowedToFailForxMinutes -and $env:allowedToFailForxMinutes -notlike "null") { $FailedFor = $env:allowedToFailForxMinutes }
    if ($env:retrieveAllowedFailureTimeFromCustomField -and $env:retrieveAllowedFailureTimeFromCustomField -notlike "null" ) { $FromCustomField = $env:retrieveAllowedFailureTimeFromCustomField }

    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }

    function Test-IsSystem {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        return $id.Name -like "NT AUTHORITY*" -or $id.IsSystem
    }

    if (!(Test-IsElevated) -and !(Test-IsSystem)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }

    # This function is to make it easier to parse Ninja Custom Fields.
    function Get-NinjaProperty {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            [String]$Name,
            [Parameter()]
            [String]$Type,
            [Parameter()]
            [String]$DocumentName
        )

        # If we're requested to get the field value from a Ninja document we'll specify it here.
        $DocumentationParams = @{}
        if ($DocumentName) { $DocumentationParams["DocumentName"] = $DocumentName }

        # These two types require more information to parse.
        $NeedsOptions = "DropDown","MultiSelect"

        # Grabbing document values requires a slightly different command.
        if ($DocumentName) {
            # Secure fields are only readable when they're a device custom field
            if ($Type -Like "Secure") { throw "$Type is an invalid type! Please check here for valid types. https://ninjarmm.zendesk.com/hc/en-us/articles/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality" }

            # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
            Write-Host "Retrieving value from Ninja Document..."
            $NinjaPropertyValue = Ninja-Property-Docs-Get -AttributeName $Name @DocumentationParams 2>&1

            # Certain fields require more information to parse.
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2>&1
            }
        }
        else {
            # We'll redirect error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
            $NinjaPropertyValue = Ninja-Property-Get -Name $Name 2>&1

            # Certain fields require more information to parse.
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2>&1
            }
        }

        # If we received some sort of error it should have an exception property and we'll exit the function with that error information.
        if ($NinjaPropertyValue.Exception) { throw $NinjaPropertyValue }
        if ($NinjaPropertyOptions.Exception) { throw $NinjaPropertyOptions }

        # This switch will compare the type given with the quoted string. If it matches, it'll parse it further; otherwise, the default option will be selected.
        switch ($Type) {
            "Attachment" {
                # Attachments come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Checkbox" {
                # Checkbox's come in as a string representing an integer. We'll need to cast that string into an integer and then convert it to a more traditional boolean.
                [System.Convert]::ToBoolean([int]$NinjaPropertyValue)
            }
            "Date or Date Time" {
                # In Ninja Date and Date/Time fields are in Unix Epoch time in the UTC timezone the below should convert it into local time as a datetime object.
                $UnixTimeStamp = $NinjaPropertyValue
                $UTC = (Get-Date "1970-01-01 00:00:00").AddSeconds($UnixTimeStamp)
                $TimeZone = [TimeZoneInfo]::Local
                [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)
            }
            "Decimal" {
                # In ninja decimals are strings that represent a decimal this will cast it into a double data type.
                [double]$NinjaPropertyValue
            }
            "Device Dropdown" {
                # Device Drop-Downs Fields come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Device MultiSelect" {
                # Device Multi-Select Fields come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Dropdown" {
                # Drop-Down custom fields come in as a comma-separated list of GUIDs; we'll compare these with all the options and return just the option values selected instead of a GUID.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Options | Where-Object { $_.GUID -eq $NinjaPropertyValue } | Select-Object -ExpandProperty Name
            }
            "Integer" {
                # Cast's the Ninja provided string into an integer.
                if($NinjaPropertyValue){
                    [int]$NinjaPropertyValue
                }else{
                    $NinjaPropertyValue
                }
            }
            "MultiSelect" {
                # Multi-Select custom fields come in as a comma-separated list of GUID's we'll compare these with all the options and return just the option values selected instead of a guid.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Selection = ($NinjaPropertyValue -split ',').trim()

                foreach ($Item in $Selection) {
                    $Options | Where-Object { $_.GUID -eq $Item } | Select-Object -ExpandProperty Name
                }
            }
            "Organization Dropdown" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization Location Dropdown" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization Location MultiSelect" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization MultiSelect" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Time" {
                # Time fields are given as a number of seconds starting from midnight. This will convert it into a datetime object.
                $Seconds = $NinjaPropertyValue
                $UTC = ([timespan]::fromseconds($Seconds)).ToString("hh\:mm\:ss")
                $TimeZone = [TimeZoneInfo]::Local
                $ConvertedTime = [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)

                Get-Date $ConvertedTime -DisplayHint Time
            }
            default {
                # If no type was given or not one that matches the above types just output what we retrieved.
                $NinjaPropertyValue
            }
        }
    }
}
process {
    if ($FromCustomField) {
        try{
            $CustomFieldValue = Get-NinjaProperty -Name $FromCustomField -Type "Integer"
        }catch{
            Write-Warning "$($_.ToString())"
        }

        if($CustomFieldValue){
            $FailedFor = $CustomFieldValue
        }else{
            Write-Warning "Custom Field $FromCustomField was empty?"
        }
    }

    if($FailedFor -gt 0){
        $FailedFor = $FailedFor * -1
    }

    $Threshold = (Get-Date).AddMinutes($FailedFor)
    Write-Host "Checking vm's that have not replicated prior to $Threshold."

    $Failed = New-Object System.Collections.Generic.List[string]
    $UnhealthyVMs = Get-VMReplication | Where-Object { $_.Health -notlike "Normal" -and $_.LastReplicationTime -lt $Threshold -and $_.State -notlike "Suspended" }
    $PausedVMs = Get-VMReplication | Where-Object { $_.LastReplicationTime -lt $Threshold -and $_.State -like "Suspended" }

    if ($UnhealthyVMs) {
        $Failed.Add($UnhealthyVMs)
    }
    
    if ($PausedVMs) {
        Write-Warning "Some of the vm's currently have replication paused!"

        if(-not $IncludePaused){
            Write-Warning "Please use 'Include Paused Replications' to include paused replications in the alert. Otherwise, they will be skipped."
        }
    }

    if ($PausedVMs -and $IncludePaused) {
        $Failed.Add($PausedVMs)
    }

    if ($Failed) {
        Write-Error "Hyper-V Replication is currently failing!"
        Get-VMReplication | Format-Table -Property VMName, PrimaryServer, State, Health, LastReplicationTime | Out-String | Write-Host
        exit 1
    }
    else {
        Write-Host "No failing replications detected prior to $Threshold."

        Get-VMReplication | Format-Table -Property VMName, PrimaryServer, State, Health, LastReplicationTime | Out-String | Write-Host
        exit 0
    }
}end {
    
    
    
}

 

Accedi a oltre 700 script nel Dojo NinjaOne

Ottieni l’accesso

Analisi dettagliata dello script per monitorare lo stato della replica Hyper-V

Lo script è un’utility PowerShell progettata per monitorare lo stato della replica Hyper-V. Controlla lo stato di replica e di integrità di ogni virtual machine, e invia avvisi gli amministratori in caso di problemi. I componenti chiave dello script includono:

  • Cmdlet binding e parametri: Lo script per monitorare lo stato della replica Hyper-V utilizza funzioni avanzate che consentono parametri come -FailedFor, -FromCustomField e -IncludePaused.
  • Variabili d’ambiente e funzioni: Utilizza funzioni personalizzate e variabili d’ambiente per determinare la presenza di autorizzazioni elevate e recuperare i valori dei campi personalizzati.
  • Processo principale: Lo script per monitorare lo stato della replica Hyper-V valuta lo stato e il tempo di replica di ciascuna virtual machine, confrontandoli con le soglie specificate. Se le virtual machine non si sono replicate entro l’intervallo di tempo stabilito o si trovano in uno stato anomalo, viene emesso un avviso.

Questo script per monitorare lo stato della replica Hyper-V potrebbe essere rappresentato visivamente con un diagramma di flusso, che ne illustra le fasi dall’inizializzazione all’impostazione dei parametri, passando per la verifica dei permessi e arrivando fino all’analisi finale e agli avvisi.

Casi d’uso potenziali

Immagina un amministratore IT di un’azienda di medie dimensioni con un solido ambiente virtualizzato che utilizza Hyper-V. Potrebbe distribuire questo script per monitorare regolarmente lo stato di replica delle virtual machine, identificando e risolvendo rapidamente eventuali problemi di replica e garantendo così la continuità aziendale.

Confronti

Tradizionalmente, il monitoraggio della replica Hyper-V prevede controlli manuali o script meno sofisticati. Questo script, tuttavia, automatizza il processo e fornisce un monitoraggio più completo e personalizzabile. Supera i comandi PowerShell di base o i controlli manuali della GUI in termini di efficienza e scalabilità.

Domande frequenti

D: Questo script per monitorare lo stato della replica Hyper-V può essere eseguito su qualsiasi versione di PowerShell?
R: Richiede PowerShell 5.1 o successivo.

D: È necessario eseguire lo script per monitorare lo stato della replica Hyper-V con i privilegi di amministratore?
R: Sì, lo script verifica la presenza di privilegi elevati necessari per accedere ai dettagli della replica della virtual machine.

D: Come posso personalizzare la soglia di avviso?
R: Utilizza il parametro -FailedFor per impostare un intervallo di tempo personalizzato in minuti.

Implicazioni

Da un lato questo script migliora il monitoraggio della replica Hyper-V, dall’altro sottolinea la necessità di misure di sicurezza IT proattive. Il rilevamento e la risoluzione tempestivi dei problemi di replica sono fondamentali per mantenere l’integrità e la disponibilità dei dati.

Raccomandazioni

  • Esegui regolarmente lo script per garantire un monitoraggio continuo.
  • Regola il parametro -FailedFor in base alle esigenze specifiche dell’ambiente.
  • Assicurati che PowerShell sia installato nella versione 5.1 o successive, ed eseguito con le autorizzazioni necessarie.

Considerazioni finali

Nel contesto della gestione degli ambienti virtualizzati, strumenti come NinjaOne forniscono una piattaforma completa per il monitoraggio e la gestione dell’infrastruttura IT. L’integrazione di uno script come questo in una suite più ampia di strumenti NinjaOne può migliorare la capacità di un’organizzazione di gestire gli ambienti Hyper-V in modo efficiente, garantendo affidabilità e prestazioni.

Passi successivi

La creazione di un team IT efficiente ed efficace richiede una soluzione centralizzata che funga da principale strumento per la fornitura di servizi. NinjaOne consente ai team IT di monitorare, gestire, proteggere e supportare tutti i dispositivi, ovunque essi si trovino, senza la necessità di una complessa infrastruttura locale.

Per saperne di più su NinjaOne Endpoint Management, fai un tour dal vivo, o inizia la tua prova gratuita della piattaforma NinjaOne.

Categorie:

Ti potrebbe interessare anche

Termini e condizioni NinjaOne

Cliccando sul pulsante “Accetto” qui sotto, dichiari di accettare i seguenti termini legali e le nostre condizioni d’uso:

  • Diritti di proprietà: NinjaOne possiede e continuerà a possedere tutti i diritti, i titoli e gli interessi relativi allo script (compreso il copyright). NinjaOne ti concede una licenza limitata per l’utilizzo dello script in conformità con i presenti termini legali.
  • Limitazione d’uso: Puoi utilizzare lo script solo per legittimi scopi personali o aziendali interni e non puoi condividere lo script con altri soggetti.
  • Divieto di ripubblicazione: In nessun caso ti è consentito ripubblicare lo script in una libreria di script appartenente o sotto il controllo di un altro fornitore di software.
  • Esclusione di garanzia: Lo script viene fornito “così com’è” e “come disponibile”, senza garanzie di alcun tipo. NinjaOne non promette né garantisce che lo script sia privo di difetti o che soddisfi le tue esigenze o aspettative specifiche.
  • Assunzione del rischio: L’uso che farai dello script è da intendersi a tuo rischio. Riconosci che l’utilizzo dello script comporta alcuni rischi intrinseci, che comprendi e sei pronto ad assumerti.
  • Rinuncia e liberatoria: Non riterrai NinjaOne responsabile di eventuali conseguenze negative o indesiderate derivanti dall’uso dello script e rinuncerai a qualsiasi diritto legale o di equità e a qualsiasi rivalsa nei confronti di NinjaOne in relazione all’uso dello script.
  • EULA: Se sei un cliente NinjaOne, l’uso dello script è soggetto al Contratto di licenza con l’utente finale (EULA) applicabile.