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

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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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.