Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting back TIME information by Powershell

I have the below code that looks at OS system and based on the build type it returns the NTP server they point to.

However, it works fine on a match of 7601 (Windows Server 2008 R2) - but, for Windows Server 2003 servers I always get an error. For Windows Server 2003 servers you need to read a registry key.

If I did the same query on the registry key locally on a server it works fine...although it reports back other stuff aswell as the registry key..

here is the error:

NTPSource                                                   Server
---------                                                   ------
The following error occurred: The procedure number is ou... SERV1

Here is the code:

$servers = @('SERV1','SERV2')
$version = Get-WmiObject Win32_OperatingSystem -computer $servers | select buildnumber

foreach ($server in $servers){

    if ($version -match '7601')
    {
    $ntps = w32tm /query /computer:$server /source
    new-object psobject -property @{
    Server = $Server
    NTPSource = $ntps
    }
    }
elseif($version -match '3790')
{

    Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters' -Name Type

}   
}
like image 266
lara400 Avatar asked Apr 21 '26 21:04

lara400


1 Answers

Personally I use this to retrieve NTP server (Windows Server 2000/2003/2008/2008r2):

$Servers = (gc Computers.txt) # list of servers name
$pw = Get-Credential 
$HKLM = 2147483650
foreach( $Server in $Servers )
{
$reg = GWMI -list -namespace root\default -computername $server -Credential $pw | 
       where-object { $_.name -eq "StdRegProv" }
$key = $reg.GetStringValue($HKLM,"SYSTEM\CurrentControlSet\Services\W32Time\Parameters","NtpServer")

write-host "$server `t$($key.svalue)"

}
like image 108
CB. Avatar answered Apr 23 '26 12:04

CB.