繁体   English   中英

如何检查域控制器的时间同步?

[英]How to check Time synchronization for domain controllers?

我正在尝试编写一个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
    }

}

我再次编写代码,它现在有效,这里是代码:


$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM