您的位置:首页 > 运维架构 > Shell

PowerShell 通过NTP/NT5DS同步时间

2011-11-02 09:41 501 查看
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.ps1
Synchronize the client time.

PARAMETERS:
-type The type <NTP/NT5DS> of synchronizing the time.
-help print help file

SYNTAX:
SyncTime.ps1
Generates an error.You must supply a type name.
SyncTime.ps1 -type NTP
Using NTP to synchronize the time to the server "time.windows.com"
SyncTime.ps1 -type NT5DS
Using 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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: