-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerCheckOnHost.ps1
116 lines (90 loc) · 3.71 KB
/
ServerCheckOnHost.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
109
110
111
function Update-ServerReport {
[CmdletBinding()]
[OutputType([int])]
param(
#location of output file
[Parameter(Mandatory=$false)]
$output_location = "C:\temp\ServerReport",
#Severity of message
[Parameter(Mandatory=$true)]
[validateset("Info","Warning", "Error")]
$message_severity,
#Message to be logged.
[Parameter(Mandatory=$true)]
$Message
)
$date = Get-Date -format g
$filePath = "$output_location\"
$dateString = get-date -UFormat %d-%m-%Y
$fileName = "ServerReport$dateString.log"
$file = "$filePath$fileName"
if (!(Test-Path $file)){
New-Item -Path $filePath -name $fileName -ItemType "file" -Force | Out-Null
}
$log_message = "$date :: $($message_severity) - $message `n"
$log_message | Out-File -FilePath $file -Append -Force
}
$servers = Get-VM
foreach($serv in $servers){
#region Hyper-V Running Test
$nic = $serv.networkadapters | where{$_.status -eq "OK"} | select -ExpandProperty ipaddresses | where {$_ -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"}
If($serv.State -ne "Running"){
$messageHypervState = "$($serv.Name) - Server not running in Hyper-V: $($serv.state)"
Write-Warning $messageHypervState
Update-ServerReport -message_severity Error -Message $messageHypervState
}else{
$messageHypervState = "$($serv.name) - Hyper-V: Running"
write-host -BackgroundColor Green $messageHypervState
Update-ServerReport -message_severity Info -Message $messageHypervState
}
#endregion
#region Ping Test
$ping = Test-Connection $nic -Count 4 -Quiet
if(!($ping)){
$messagePing = "$($serv.Name) - no Ping responce"
Write-Warning $messagePing
Update-ServerReport -message_severity Error -Message $messagePing
}else{
$messagePing = "$($serv.name) - Ping: Good"
write-host -BackgroundColor Green $messagePing
Update-ServerReport -message_severity Info -Message $messagePing
}
#endregion
#region server uptime
$lastBootUp = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$currentTime = Get-Date
$lastBoot = New-TimeSpan -Start $lastBootUp -End $currentTime
Write-Output "$($lastBoot.Days) Days $($lastBoot.Hours) Hours $($lastboot.Minutes) Minutes"
#endregion
#region WMI Test
try{
$WMITest = Get-WmiObject -Class win32_process -ErrorAction Stop
}
catch{
$messageWMI = "$($serv.Name) - no WMI responce. Check PING and Hyper-V status, WMI might not be open on this server."
Write-Warning $messageWMI
Update-ServerReport -message_severity Warning $messageWMI
}
if(!($WMITest)){
$messageWMI = "$($serv.Name) - no WMI responce. Check PING and Hyper-V status, WMI might not be open on this server."
Write-Warning $messageWMI
Update-ServerReport -message_severity Warning -Message $messageWMI
}else{
$messageWMI = "$($serv.name) - WMI: Good"
write-host -BackgroundColor Green $messageWMI
Update-ServerReport -message_severity Info -Message $messageWMI
}
#endregion
#region AdminShare Test
$adminShare = "$($serv.name)\c`$"
If(Test-Path $adminShare){
$messageAdmin = "$($serv.name) - Admin Share (C`$): Good `n"
write-host -BackgroundColor Gray $messageAdmin
Update-ServerReport -message_severity Info -Message $messageAdmin
}else{
$messageAdmin = "$($serv.name) - Admin Share (C`$): Error cannot location Admin Share"
Write-Warning $messageAdmin
Update-ServerReport -message_severity Error -Message $messageAdmin
}
#endregion
}