-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorrect-SystemTime.ps1
108 lines (83 loc) · 4.15 KB
/
Correct-SystemTime.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Function Correct-SystemTime{
<#
.SYNOPSIS
Function to correct wrong time and date on remote machines
by Steven Wight
.DESCRIPTION
Correct-SystemTime -ComputerName <Hostname> -Domain <domain> (default = POSHYT)
.EXAMPLE
Correct-SystemTime Computer01
.Notes
This assumes the correct time and date on the machine it's being run from
#>
[CmdletBinding()]
Param(
[Parameter()] [String] [ValidateNotNullOrEmpty()] $ComputerName,
[Parameter()] [String] [ValidateNotNullOrEmpty()] $Domain = "POSHYT"
)
#Clear Variables encase function has been used before in session (never know!)
$Computer = $AdCheck = $PathTest = $TimeAndDate = $RemoteTimeAndDate = $null
# Get Computer info from AD
try{
$Computer = (Get-ADComputer $ComputerName -properties DNSHostname,description,OperatingSystem -server $Domain -ErrorAction stop)
$AdCheck = $true
}Catch{
Write-Host -ForegroundColor Red "Machine $($ComputerName) not found in AD"
$Computer = $_.Exception.Message
$AdCheck = $false
}
# Check machine is online
if($True -eq $AdCheck){
$PathTest = Test-Connection -Computername $Computer.DNSHostname -BufferSize 16 -Count 1 -Quiet
} #End of If ADcheck is True
#if Machine is online
if($True -eq $PathTest) {
#Output machine is online to the console
Write-host -ForegroundColor Green "$($ComputerName) is online"
#Get remote machines Time and date
$RemoteTimeAndDate = Invoke-Command -ComputerName $Computer.DNSHostname -ScriptBlock { return Get-Date -Format "dddd MM/dd/yyyy HH:mm" }
#get local machines date and time
$TimeAndDate = Get-date -Format "dddd MM/dd/yyyy HH:mm"
#if time is out
if($RemoteTimeAndDate -ne $TimeAndDate){
Write-Host ""
Write-Host -ForegroundColor RED "$($ComputerName) time is out"
Write-Host -ForegroundColor RED "Remote Time - $($RemoteTimeAndDate)"
Write-Host -ForegroundColor RED "Local Time - $($TimeAndDate)"
Write-Host ""
$Continue = Read-Host -Prompt 'Do you wish to correct? - Press Y to continue'
if ("Y" -eq $Continue.ToUpper()) {
Write-Host ""
Write-Warning -Message "Correcting time on $($ComputerName)"
Write-Host ""
#get local machines date and time
$TimeAndDate = Get-date
#Correct time on remote machine
$RemoteTimeAndDate = Invoke-Command -ComputerName $Computer.DNSHostname -ScriptBlock { Set-Date -Date $using:TimeAndDate
return Get-Date -Format "dddd MM/dd/yyyy HH:mm" }
#confirm time and date was set correctly
if($RemoteTimeAndDate -eq $TimeAndDate){
#output if successful
Write-Host ""
Write-Host -ForegroundColor Green "$($ComputerName) time was successfully corrected"
Write-Host ""
}else{
#output if unsuccessful and what the difference is
Write-Host ""
Write-Host -ForegroundColor RED "$($ComputerName) issue correcting time"
Write-Host -ForegroundColor RED "Remote Time - $($RemoteTimeAndDate)"
Write-Host -ForegroundColor RED "Remote Time - $($TimeAndDate)"
Write-Host ""
}
}
}else{
#output if time is okay
Write-Host ""
Write-Host -ForegroundColor Green "$($ComputerName) time is correct"
Write-Host ""
}
}else{#If machine wasn't online
#Output machine is online to the console
Write-host -ForegroundColor Red "$($ComputerName) is offline"
}# End of If
}# end of Function