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 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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 { } } } } |
You might want to check the w32tm utility:
w32tm /query /computer:pc1 /source
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 🙂
Thanks for the article. Can we run it against a list of servers in a text file?
Hi Weiyentan,
Sure, you can call it like so:
Get-TimeServer -ComputerName (get-content C:\Users\Jeff\Desktop\123.txt)
Jeff.
Thanks for the script, made some adjustments to accept pipeline input but its working great
Hi Marco,
Happy to read you found it useful 🙂
Jeff.
Hi Jeff,
I am aa little confused how you use Get-TimeServer -ComputerName (get-content C:\Users\Jeff\Desktop\123.txt) which is what i am trying to do with this script are you able to give more detail? I am not sure how I use this script