Do you have problems with time synchronization in your Active Directory domain or are you only interested in monitoring and determining the system time of your servers, workstations or domain-controllers? If your answer is yes, read on. In this article I will provide you with an advanced PowerShell function that will remotely retrieve the system time of single computers of your choosing or a set of domain joined computers.
The Objective
I want it to be as simple as possible: Get the system time of all or certain computers of your Active Directory domain with just one command. Let’s go.
Local System Time
Simple thing, huh? Let’s get the party started.
Local System Time of a specific Computer
Local System Time of all Domain-Controllers
Now lets go one step further.
Local System Time of all Domain-Joined Windows Servers
Which brings me to the main part, the function itself.
Get-Time
This part contains only the code you need later on. For help using Get-Time in your PowerShell session move on to the next part.
function Get-Time { # .SYNOPSIS # Get-Time is an advanced Powershell function. It obtains the local system time of a scope of computers or the system time of particular remote computers. # .DESCRIPTION # Uses the buil-in function Get-Date. Define scope or computer name. # .PARAMETER # Scope # Define the scope. Possible values: "AllServer", "DomainController", "Computer" # .PARAMETER # Computer # Provide computer name of remote computer. # .EXAMPLE # Get-Time -Scope AllServer # .NOTES # Author: Patrick Gruenauer, MVP PowerShell # Web: https://sid-500.com [CmdletBinding()] param ( [Parameter(Mandatory=$false, HelpMessage='Enter the following values: AllServer, DomainController, Computer')] [ValidateSet("AllServer", "DomainController", "Computer")] $Scope, [parameter(Mandatory=$false)] $Computer="$env:Computername" ) $server=(Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name $dc=Get-ADDomainController -Filter * | Select-Object -ExpandProperty Name $result=@() switch ($Scope) { 'AllServer' { foreach ($s in $server) { $t=Invoke-Command -ComputerName $s {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction SilentlyContinue $result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{ 'Server'= $s 'Time' = $t })} } 'DomainController' { foreach ($d in $dc) { $t=Invoke-Command -ComputerName $d {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction SilentlyContinue $result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{ 'Server'= $d 'Time' = $t })} } } If ($Computer -ne "$env:computername") { Try { $t=Invoke-Command -ComputerName $Computer {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction Stop $result +=New-Object -TypeName PSObject -Property ([ordered]@{ 'Computer'= $Computer 'Time' = $t }) } Catch { $result+=New-Object -TypeName PSObject -Property ([ordered]@{ 'Computer'=$Computer 'Time'='Computer could not be reached' }) } } If (($computer -eq "$env:computername") -and ($scope -eq $null)) { Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime } Write-Output $result }
How to use it
Copy the code above into PowerShell ISE (ise.exe) and run the code. Then type the command and have fun with it.
If you want to make the function permanently available, so that the function is available every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name the folder Get-Time. Then save the code as .psm1 file in that folder. The screenshot below will help you.
Categories: Cyber Security, PowerShell, Windows Server
Patrick,
In the code I see the sections for ALLServer and DomainController, however there isn’t one for Computer. When you run Get-Time -Scope Computer, there is no output. Output for the other two work grate. I need the Computer one to work if you don’t mind. Thanks!
LikeLike
Yes. This one is only for server computers.
LikeLike
Great job Patrick, thanks for this very useful script.
AD
LikeLike
It’s my pleasure.
LikeLike
I don’t get any output. Where is the output file located?
LikeLike
Doesn’t seem to do anything.. Broken?
LikeLike
What is winrm is disable, how do you run this
LikeLike
I am not catching where the output is being placed. or if its being placed.
LikeLike
how to get the time details for 50 servers simultaneously , when i enter details of computer seprated by “,” it is only working for 4 computer not more that computer
LikeLike
It should work for more than 4 …
LikeLike
this script is great, especially the last part explained how to use this script. There are a lot of good scripts out there today, but most of them lack of instruction, authors probably assume the readers know how to handle their script, which displays the lack of disciplinary from most of the developers or code writers.
Thank you very much!
LikeLiked by 1 person
Thank you for the kind words!
LikeLike