Checking Windows Time Service on Servers using PowerShell Script

In this Post, We will be talking about Windows Time Service or you can also call W32Time which is implemented in a dynamic link library called W32Time.dll and installed by default in “C:\Windows\System32” folder while installation of Operating System like windows 2008. The purpose for developing W32Time is to synchronize the date and time for all computers running in an AD DS domain. That means, the windows network using AD alias of Active Directory require that every machine clocks within in a specific domain should be almost synchronized so the machine can log on the domain using Kerberos Authentication. Inaccuracy in the Windows Time Service can create trouble and may result in failure for application such as accounting, email servers, SQL, transaction systems, security logging, firewalls,etc.)

Checking Windows Time Service on Servers using PowerShell Script


Download Script 23 downloads

Creating a PowerShell Script to fetch Windows Time Service information from remote servers and Exporting to CSV File

Let us take a scenario, suppose we are working in an infrastructure where we have thousand no. of servers running windows. Now we need to pull out information related to Windows Time Service State, Status, Start mode, on all those servers. So, how can we do this with PowerShell?

So, here we need windows time service information. That means we are going to access one of specific service from windows services and PowerShell has already introduced a WMI class “Win32_Service” that represents a service on a computer system running Windows.

Now our task became easy. We will simply access the wmi class Win32_Service using Get-WmiObject and then after getting control to all those windows services we will apply filter to find Windows Time Service (W32Time) among those services.

Eg : Get-WmiObject -Class Win32_Service -Filter "Name='W32Time'"
Output
ExitCode  : 1077
Name      : W32Time
ProcessId : 0
StartMode : Manual
State     : Stopped
Status    : OK

This result was for your single local computer.

windows-time-service-and-powershell
Let's implement it for N no. of Servers

Line no. 8 : We are trying to check if the servers is Reachable, So that we could avoid running W32Time query on unreachable servers.

Line no. 20 : We created a custom ps object which will keep data for unreachable servers

Line no. 34 : This time we created custom ps object for those servers where W32Time is returning result after its execution.

Line no. 48 : Here, Each time we are just appending the returned result data and exporting it to csv at the same time while the execution of script.


  $ServerList =Get-Content servers.txt

$ErrorActionPrefernce ='silentlycontinue'
foreach($Server in $ServerList)
                                {
$ServerName  =$Server.Trim()
Write-Host " "
Write-Host "Checking Windows Time Service on the server " $ServerName
Write-Host " "
if(!(Test-Connection -Cn $ServerName -Count 1 -ea 0 -quiet))
   {

Write-Host "---------------------------  "
Write-Host "Problem occured while to connecting to Server " $ServerName -ForegroundColor yellow
Write-Host " "

$Result = [pscustomobject][ordered]@{
    ServerName = $ServerName
    Service    = "Windows Time"
    StartMode  = "UnReachable"
    State      = "UnReachable"
    Status     = "UnReachable"
                                        }
   } else
          {

$Service = Get-WmiObject -Class Win32_Service  -Filter "Name='W32Time'" -ComputerName $ServerName  |
Select StartMode,state,status

$Result = [pscustomobject][ordered]@{
     ServerName = $ServerName
    Service    = "Windows Time"
    StartMode  = $Service.StartMode
    State      = $Service.state
    Status = $Service.Status;
                                   }



        }

$Result
$Result |
 Export-Csv WindowsTime.Csv -Append -NoTypeInformation

                                  }




   

People Reaction : 0

Rohit Sharma
Name : Email : Website :
Rohit Sharma

Hi There! My name is Rohit and I am working in the one of MNC as Web Apps developer. I have been in this tech industry for last 3.6 years. This blog is just a part of my career journey.
Ready to make new mistakes without repeating the previous ones.

"All life is an experiment. The more experiments you make the better"


© 2020 WriteSomeCode. All Right Reserved. A Rohit Sharma Blog. Creative Commons License licensed under a Creative Commons Attribution 4.0 International License