答案 0 :(得分:1)
您应该能够从注册表中获取信息。请尝试:
$key = 'HKLM:\SYSTEM\CurrentControlSet\services\W32Time\Parameters'
$enabled = switch ((Get-ItemProperty $key).Type) {
{ 'NTP','NT5DS','AllSync' -contains $_ } { $true }
'NoSync' { $false }
default { throw "Invalid type value $_." }
}
您也可以使用哈希表而不是switch
语句:
$types = @{
'NTP' = $true
'NT5DS' = $true
'AllSync' = $true
'NoSync' = $false
}
$key = 'HKLM:\SYSTEM\CurrentControlSet\services\W32Time\Parameters'
$enabled = $types[(Get-ItemProperty $key).Type]
请注意,根据Windows Time Service documentation,Type
条目可以有4个不同的值,其中3个表示已启用同步。
答案 1 :(得分:0)
来自Google "powershell get ntp settings"的第一个链接
在这里粘贴代码(感谢Jeff Wouters)
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 {
}
}
}
}