Как проверить синхронизацию времени для контроллеров домена? - PullRequest
0 голосов
/ 30 апреля 2019

Я пытаюсь написать сценарий PowerShell, чтобы предупредить меня, если один из контроллеров домена не синхронизируется по электронной почте, я попытался запустить сценарий, но у меня была проблема с отправкой электронного письма, вот код. Не могли бы вы помочь мне и рассказать, чего не хватает в моем коде? Я не получил никакого электронного письма, так как я могу отправить результаты сценария на мой электронный адрес?

function Get-Time {
<#
    .SYNOPSIS
    Gets the time of a windows server
    .DESCRIPTION
    Uses WMI to get the time of a remote server
    .PARAMETER ServerName
    The Server to get the date and time from
    .EXAMPLE
    PS C:\> Get-Time localhost
    .EXAMPLE
    PS C:\> Get-Time server01.domain.local -Credential (Get-Credential)
#>
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $ServerName,
        $Credential
    )

    try {
        if ($Credential) {
            $DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername -Credential $Credential
        } else {
            $DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername
        }
    } catch {
        throw
    }

    $w32tm = Invoke-Command -Computer $Servers -ArgumentList $Servers -Scriptblock {
        Param ($Servers)
        foreach ($Server in $Servers) {
            $Check = w32tm /monitor /computers:$Server /nowarn
            $ICMP = (($Check | Select-String "ICMP")-Replace "ICMP: " , "").Trim()
            $ICMPVal = [int]($ICMP -split "ms")[0]
            $Source = w32tm /query /source
            $Name = Hostname

            switch ($ICMPVal) {
                {$ICMPVal -le 0} {$Status = "Optimal time synchronisation"}
                #you probably need another value here since you'll get no status if it is between 0 and 2m
                {$ICMPVal -lt 100000} {$Status = "0-2 Minute time difference"}
                {$ICMPVal -ge 100000} {$Status = "Warning, 2 minutes time difference"}
                {$ICMPVal -ge 300000} {$Status = "Critical. Over 5 minutes time difference!"}
            }
            $String = $Name + " - $Status " + "- $ICMP " + " - Source: $Source"
            Write-Output $String
        }
    }

    $Servers = "localhost","DC001"

    $Servers | Foreach {
        Get-Time $_

        $results = foreach ($Server in $Servers) {
            Get-Time $Server
        }
        $Servers = "localhost","DC001"

        $From = "abc@company.com"
        $To = "abc@company.com"
        $Cc = ""
        $Subject = "Time Skew Results"
        $Body = $Servers | ConvertTo-Html | Out-String
        $SMTPServer = "imail.company.com"

        Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -BodyAsHTML
    }

}

1 Ответ

0 голосов
/ 01 мая 2019

Я снова написал код, и теперь он работает, вот код:


$w32tm = Invoke-Command -Computer $Servers -ArgumentList $Servers -Scriptblock {
    Param ($Servers)
    Foreach ($Server in $Servers)
    {
        $Check = w32tm /monitor /computers:$Server /nowarn
        $ICMP = (($Check | Select-String "ICMP")-Replace "ICMP: " , "").Trim()
        $ICMPVal = [int]($ICMP -split "ms")[0]
        $Source = w32tm /query /source
        $Name = Hostname

        Switch ($ICMPVal)
            {
                #{$ICMPVal -le 0} {$Status = "Optimal time synchronisation"}
                #{$ICMPVal -lt 100000} {$Status = "0-2 Minute time difference"}
                {$ICMPVal -ge 100000} {$Status = "Warning, 2 minutes time difference"}
                {$ICMPVal -ge 300000} {$Status = "Critical. Over 5 minutes time difference!"}
            }
        if ($ICMPVal -gt 100000)
        {
        $String = "The Domain Controller:   " + $Name + "  has " + "  - $Status " + "  - $ICMP " + "   - Source: $Source"
        $From = "abc@company.com"
        $To = "abc@company.com"
        $Cc = ""
        $Subject = "Time Synchronization Alert "
        $Body =  Write-Output $String 
        $SMTPServer = "imail.company.com"
        Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -BodyAsHTML
        }




    }


}
$w32tm
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...