Cyber Security

PowerShell: Retrieve and monitor System Time of all Computers with Get-Time

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

Bild1.png

Simple thing, huh? Let’s get the party started.

Local System Time of a specific Computer

Bild4.png

Local System Time of all Domain-Controllers

Bild3.png

Now lets go one step further.

Local System Time of all Domain-Joined Windows Servers

Bild5.png

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.

Unbenannt.png

12 replies »

  1. 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!

    Like

  2. 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

    Like

  3. 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!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.