PowerShell 通过NTP/NT5DS同步时间

来源:互联网 发布:centos 6.5 搭建lamp 编辑:程序博客网 时间:2024/05/16 15:48

PowerShell通过NTP/NT5DS同步时间.


##############################################################################SyncTime.ps1                                                             ##Client time synchronization                                             ##Set the time from ntp server##If the computer is in domain,using NT5DS to synchronize the time to domain hierarchy.Else using NTP to synchronize the time to the server time.windows.com##Hsing Hsu##meng-xing.xu@hp.com##2011.11.1############################################################################param($type,$help)function funHelp(){$helpText=@"DESCRIPTION:    NAME:SyncTime.ps1Synchronize the client time.    PARAMETERS:-type The type <NTP/NT5DS> of synchronizing the time.-help print help file    SYNTAX:SyncTime.ps1Generates an error.You must supply a type name.SyncTime.ps1 -type NTPUsing NTP to synchronize the time to the server "time.windows.com"SyncTime.ps1 -type NT5DSUsing NT5DS to synchronize the time to domain hierarchy.SyncTime.ps1 -help ?Displays the help topic for the script."@    $helpText    exit}if($help){    "Obtaining help ..."    funhelp}if(!$type){Write-Host "A value for type is required.Try this: SyncTime.ps1 -help ?" -Foreground Yellow}switch($type){    "NTP"    {        ## set the shell execute policy        set-executionpolicy remotesigned        ## get the current path        $mypath=get-location        ## set the NTP server        ## NTP = synchronize to manually configured source        set-location hklm:system\currentcontrolset\services\w32time\Parameters        set-itemproperty . Type NTP        ## Set the AnnounceFlags.        ##10(2+8) if thats the Default may just mean that it will not announce itself as a time server.        ##0 Timeserv_Announce_No, Reliable_Timeserv_Announce_No. The domain controller does not advertise time service.        ##1 Timeserv_Announce_Yes. The domain controller always advertises time service.        ##2 Timeserv_Announce_Auto. The domain controller automatically determines whether it should advertise time service.        ##4 Reliable_Timeserv_Announce_Yes. The domain contoller will always advertise reliable time service.        ##8 Reliable_Timeserv_Announce_Auto. The domain controller automatically determines whether it should advertise reliable time service.        cd ../Config        set-itemproperty . AnnounceFlags 10        ## start the NTPServer        cd ../TimeProviders/NtpServer        set-itemproperty . Enabled 1        ## set the time source        cd ../../Parameters        set-itemproperty . NtpServer time.windows.com        ## set the location of the path        set-location $mypath.path        ## stop and restart the w32time server        net stop w32time        net start w32time        ## update the time with the NTP server        w32tm /resync           }    "NT5DS"    {        ## set the shell execute policy        set-executionpolicy remotesigned        ## get the current path        $mypath=get-location        ## set the NTP server        ## Nt5DS = synchronize to domain hierarchy [default]        set-location hklm:system\currentcontrolset\services\w32time\Parameters        set-itemproperty . Type NT5DS        ## Set the AnnounceFlags.        ## 5 is set the server announces itself as a reliable time source.        cd ../Config        set-itemproperty . AnnounceFlags 5        ## start the NTPServer        cd ../TimeProviders/NtpServer        set-itemproperty . Enabled 1        ## set the location of the path        set-location $mypath.path        ## stop and restart the w32time server        net stop w32time        net start w32time        ## update the time with the NTP server        w32tm /resync                }    DEFAULT    {Write-Host "You must supply a value for the type to synchronize the time.Try this: SyncTime.ps1 -help ?" -Foreground Yellow    }}




原创粉丝点击