{"id":49903,"date":"2010-05-08T00:01:00","date_gmt":"2010-05-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/05\/08\/hey-scripting-guy-weekend-scripter-configuring-an-authoritative-time-server-in-windows-server\/"},"modified":"2010-05-08T00:01:00","modified_gmt":"2010-05-08T00:01:00","slug":"hey-scripting-guy-weekend-scripter-configuring-an-authoritative-time-server-in-windows-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-configuring-an-authoritative-time-server-in-windows-server\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Configuring an Authoritative Time Server in Windows Server"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I finally had a chance to sit down and catch my breath from the 2010 Scripting Games. I reviewed some awesome scripts, and learned many new tricks. I happened to notice that my desktop computer clock was about five minutes slow. I checked and the domain controller was also slow. After looking around, I realized I had never set an external time source for my domain controller\u2014something that was kind of easy to miss because it is running Windows Server 2008 R2 Core Edition. <\/p>\n<p class=\"MsoNormal\">After doing a bit of research, I ran across an excellent knowledge base article that talks about configuring an authoritative time server in Windows Server. The article <a href=\"http:\/\/support.microsoft.com\/kb\/816042\"><font face=\"Segoe\">KB816042<\/font><\/a> contains a number of registry entries. Hmm, I thought, an opportunity to write a Windows PowerShell script. <\/p>\n<p class=\"MsoNormal\">The complete ConfigureInternetTimeSource.ps1 script is seen here. <\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ConfigureInternetTimeSource.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Param($computer = &#8220;Hyperv&#8221;, $user = &#8220;nwtraders\\administrator&#8221;)<br \/>$psSession = New-PSSession -ComputerName $computer -Credential $user <\/p>\n<p>Invoke-Command -Session $PSSession -ScriptBlock { <br \/><span>&nbsp;<\/span>$timeRoot = &#8220;HKLM:\\SYSTEM\\CurrentControlSet\\services\\W32Time&#8221;<br \/><span>&nbsp;<\/span>$ntpServer = &#8220;time.windows.com,0x1 nist1-ny.ustiming.org,0x1&#8221;<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\parameters&#8221; -name type -Value &#8220;NTP&#8221;<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\parameters&#8221; -name NtpServer -Value $ntpServer<br \/><span>&nbsp; <\/span><br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name AnnounceFlags -Value 5<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name MaxPosPhaseCorrection -Value 1800<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name MaxNegPhaseCorrection -Value 1800<br \/><span>&nbsp;&nbsp;&nbsp; <\/span><br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\TimeProviders\\NtpServer&#8221; -name Enabled -Value 1<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\TimeProviders\\NtpClient&#8221; -name SpecialPollInterval -Value 900<br \/><span>&nbsp; <\/span><br \/><span>&nbsp;<\/span>Register-WmiEvent -Query `<br \/><span>&nbsp; <\/span>&#8220;select * from __InstanceModificationEvent within 5 where targetinstance isa &#8216;win32_service'&#8221; `<br \/><span>&nbsp; <\/span>-SourceIdentifier stopped<\/p>\n<p><span>&nbsp;<\/span>Stop-Service -Name w32Time<br \/><span>&nbsp;<\/span>Wait-Event -SourceIdentifier stopped<br \/><span>&nbsp;<\/span>Start-Service -Name w32Time<br \/><span>&nbsp;<\/span><br \/><span>&nbsp;<\/span>Unregister-Event -SourceIdentifier stopped<br \/><span>&nbsp;<\/span>Remove-Event -SourceIdentifier stopped<br \/>} # end invoke-command<\/p>\n<p>Remove-PSSession -Session $psSession<\/font><\/span><\/p>\n<p class=\"MsoNormal\">Because I want to run this script against a remote computer, I create a Windows PowerShell remote session:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$psSession = New-PSSession -ComputerName $computer -Credential $user<\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the session has been created, I use it to set the registry keys. But because I want all of my commands to be executed at the same time, I use a single <b>Invoke-Command<\/b> ScriptBlock. The command therefore begins as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Invoke-Command -Session $PSSession -ScriptBlock {<\/font><\/span><\/p>\n<p class=\"MsoNormal\">Everything else is basically between the curly brackets for the ScriptBlock. The commands to modify the registry are seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$timeRoot = &#8220;HKLM:\\SYSTEM\\CurrentControlSet\\services\\W32Time&#8221;<br \/><span>&nbsp;<\/span>$ntpServer = &#8220;time.windows.com,0x1 nist1-ny.ustiming.org,0x1&#8221;<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\parameters&#8221; -name type -Value &#8220;NTP&#8221;<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\parameters&#8221; -name NtpServer -Value $ntpServer<br \/><span>&nbsp; <\/span><br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name AnnounceFlags -Value 5<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name MaxPosPhaseCorrection -Value 1800<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\config&#8221; -name MaxNegPhaseCorrection -Value 1800<br \/><span>&nbsp;&nbsp;&nbsp; <\/span><br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\TimeProviders\\NtpServer&#8221; -name Enabled -Value 1<br \/><span>&nbsp;<\/span>Set-ItemProperty -path &#8220;$timeroot\\TimeProviders\\NtpClient&#8221; -name SpecialPollInterval -Value 900<\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the registry entries have been made, it is time to stop and to start the <b>w32time<\/b> service. Before stopping the <b>w32time<\/b> service, I create a temporary WMI event consumer by using the <b>Register-WmiEvent<\/b> cmdlet. This will monitor the status of services on the computer. When an <b>__InstanceModificationEvent<\/b> occurs (such as stopping or starting a service), an event is triggered. The code to do this is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Register-WmiEvent -Query `<br \/><span>&nbsp; <\/span>&#8220;select * from __InstanceModificationEvent within 5 where targetinstance isa &#8216;win32_service'&#8221; `<br \/><span>&nbsp; <\/span>-SourceIdentifier stopped<\/font><\/span><\/p>\n<p class=\"MsoNormal\">Now I stop the <b>w32time<\/b> service, and wait for the WMI event to be generated. After the event has been generated, I know that it is safe to start the service back up again:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Stop-Service -Name w32Time<\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>Wait-Event -SourceIdentifier stopped<\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>Start-Service -Name w32Time<\/font><\/span><\/p>\n<p class=\"MsoNormal\">The last thing to do is to clean up a little bit by unregistering the event consumer and deleting the events:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Unregister-Event -SourceIdentifier stopped<\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>Remove-Event -SourceIdentifier stopped<\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">} # end invoke-command<\/font><\/span><\/p>\n<p class=\"MsoNormal\">When I am finished doing that, I remove the Windows PowerShell session by using the <b>Remove-PSSession<\/b> cmdlet as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Remove-PSSession -Session $psSession<\/font><\/span><\/p>\n<p class=\"MsoNormal\">Regedit can be used remotely to view registry keys in <b>Hkey_Local_Machine<\/b> and in <b>Hkey_Users<\/b>. Some of the registry settings modified by the script are seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of some registry settings modified by script\" alt=\"Image of some registry settings modified by script\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/7619\/00\/00\/03\/32\/96\/06\/WES-05-08-10-01.JPG\" width=\"600\" height=\"372\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/7619\/00\/00\/03\/32\/96\/06\/WES-05-08-10-01.JPG\"><font face=\"Segoe\"><\/font><\/a><\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I finally had a chance to sit down and catch my breath from the 2010 Scripting Games. I reviewed some awesome scripts, and learned many new tricks. I happened to notice that my desktop computer clock was about five minutes slow. I checked and the domain controller was [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[86,51,31,26,57,3,130,61,45],"class_list":["post-49903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-information","tag-getting-started","tag-operating-system","tag-registry","tag-remoting","tag-scripting-guy","tag-servers","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I finally had a chance to sit down and catch my breath from the 2010 Scripting Games. I reviewed some awesome scripts, and learned many new tricks. I happened to notice that my desktop computer clock was about five minutes slow. I checked and the domain controller was [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/49903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=49903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/49903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=49903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=49903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=49903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}