06 Sep 2013 @ 7:42 AM 

Some time ago I was asked to write a PowerShell script to do an inventory of the time server (NTP) configuration on clients.
Today I was asked the same, so I grabbed my old script, expanded it a little and here you go :-)

function Get-TimeServer {
<#
	.Synopsis
	Gets the time server as configured on a computer.
	.DESCRIPTION
	Gets the time server as configured on a computer.
	The default is localhost but can be used for remote computers.
	.EXAMPLE
	Get-TimeServer -ComputerName "Server1"
	.EXAMPLE
	Get-TimeServer -ComputerName "Server1","Server2"
	.EXAMPLE
	Get-TimeServer -Computer "Server1","Server2"
	.EXAMPLE
	Get-TimeServer "Server1","Server2"
	.NOTES
	Written by Jeff Wouters.
#>
	[CmdletBinding(SupportsShouldProcess=$true)]
	param ( 
		[parameter(mandatory=$true,position=0)][alias("computer")][array]$ComputerName="localhost"
	)
	begin {
		$HKLM = 2147483650
	}
	process {
		foreach ($Computer in $ComputerName) {
			$TestConnection = Test-Connection -ComputerName $Computer -Quiet -Count 1
			$Output = New-Object -TypeName psobject
			$Output | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $Computer
            $Output | Add-Member -MemberType 'NoteProperty' -Name 'TimeServer' -Value "WMI Error"
			$Output | Add-Member -MemberType 'NoteProperty' -Name 'Type' -Value "WMI Error"
			if ($TestConnection) {				
				try {
                    $reg = [wmiclass]"\\$Computer\root\default:StdRegprov"
				    $key = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
				    $servervalue = "NtpServer"
				    $server = $reg.GetStringValue($HKLM, $key, $servervalue)
				    $ServerVar = $server.sValue -split ","
				    $Output.TimeServer = $ServerVar[0]
    				$typevalue = "Type"
                    $type = $reg.GetStringValue($HKLM, $key, $typevalue)
                    $Output.Type = $Type.sValue				
				    $Output
                } catch {
                }
			} else {
			}
		}
	}
}
Posted By: Jeff Wouters
Last Edit: 16 Apr 2014 @ 11:54 AM

EmailPermalink
Tags


  • Shay Levy

    You might want to check the w32tm utility:

    w32tm /query /computer:pc1 /source

  • http://www.jeffwouters.nl Jeff Wouters

    That output is tricky to put into a CSV and do some selecting/sorting on it, that’s why we’v used the script I posted :-)

  • weiyentan

    Thanks for the article. Can we run it against a list of servers in a text file?

  • http://www.jeffwouters.nl Jeff Wouters

    Hi Weiyentan,
    Sure, you can call it like so:
    Get-TimeServer -ComputerName (get-content C:\Users\Jeff\Desktop\123.txt)
    Jeff.


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 328
  • Comments » 468
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.