Skip to content

Commit cb03115

Browse files
committed
Add Get-NtpSourceType and Get-NtpClientConfig scripts for NTP configuration retrieval
1 parent 4e163f4 commit cb03115

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

General/Get-NtpSourceType.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$NTPTypePattern = "^Type: (?'TypeValue'\w+) \({1}\w+\)"
2+
$MatchNTP = w32tm.exe /query /configuration | Select-String -Pattern $NTPTypePattern
3+
$NTPSourceType = $MatchNTP.Matches.Groups[1].Value
4+
$NTPSourceType
5+
# Should return 'NT5DS' for a domain joined machine, 'NTP' for others, etc.
6+
7+
# Get the NTP Server and Type from the registry
8+
$RegistryPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\W32Time'
9+
[PSCustomObject]@{
10+
Type = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name Type).Type
11+
Server = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name NtpServer).NtpServer
12+
LastKnownGoodTime = [datetime]::FromFileTime( (Get-ItemProperty -Path "$RegistryPath\Config" -Name LastKnownGoodTime).LastKnownGoodTime )
13+
}

Windows/Get-NtpClientConfig.ps1

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function Get-NtpClientConfig {
2+
<#
3+
.SYNOPSIS
4+
Get basic NTP client configuration details from Windows.
5+
6+
.DESCRIPTION
7+
This function gets the NTP source server, source type, and last known good time for the NTP client without requiring local administrator privileges. Note: Performing a remote check does require administrative privileges on the remote computer.
8+
9+
.EXAMPLE
10+
Get-NtpConfig
11+
12+
Gets the NTP client configuration from the local host.
13+
14+
.EXAMPLE
15+
Get-NtpClientConfig -ComputerName 'COMPUTER01'
16+
17+
Gets the NTP client configuration from COMPUTER01.
18+
19+
.NOTES
20+
Author: Sam Erde
21+
Version: 0.1.0
22+
Modified: 2024-12-04
23+
#>
24+
[CmdletBinding()]
25+
param (
26+
# The computer to query.
27+
[Parameter(ValueFromPipeline, Position = 0)]
28+
[ValidateNotNullOrEmpty()]
29+
[string]$ComputerName = $env:COMPUTERNAME
30+
)
31+
32+
begin {
33+
$RegistryPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\W32Time'
34+
} # end begin
35+
36+
process {
37+
38+
if ($ComputerName -match $env:COMPUTERNAME -or $ComputerName -eq 'localhost') {
39+
[PSCustomObject]@{
40+
Type = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name Type).Type
41+
Server = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name NtpServer).NtpServer
42+
LastKnownGoodTime = [datetime]::FromFileTime( (Get-ItemProperty -Path "$RegistryPath\Config" -Name LastKnownGoodTime).LastKnownGoodTime )
43+
}
44+
} else {
45+
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
46+
[PSCustomObject]@{
47+
Type = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name Type).Type
48+
Server = (Get-ItemProperty -Path "$RegistryPath\Parameters" -Name NtpServer).NtpServer
49+
LastKnownGoodTime = [datetime]::FromFileTime( (Get-ItemProperty -Path "$RegistryPath\Config" -Name LastKnownGoodTime).LastKnownGoodTime )
50+
}
51+
}
52+
}
53+
} # end process
54+
55+
end {
56+
#
57+
} # end end
58+
59+
} # end function Get-NtpConfig

0 commit comments

Comments
 (0)