-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-server-status.psm1
More file actions
147 lines (119 loc) · 3.63 KB
/
get-server-status.psm1
File metadata and controls
147 lines (119 loc) · 3.63 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Function Get-ComputerName {
<#
.Synopsis
Prompts the user for a computername.
.DESCRIPTION
Validates the computername to make sure it is valid by executing a WMI call against it.
.EXAMPLE
Get-ComputerName
#>
Do {
$ComputerName = Read-Host "Enter the name of the machine to target"
}
Until (Get-WmiObject -Class Win32_Bios -ComputerName $ComputerName -ErrorAction SilentlyContinue)
$ComputerName
}
Function Get-Uptime {
<#
.Synopsis
Gets the uptime of a computer.
.DESCRIPTION
Uses WMI to retrieve the last boot time for a computer and calulcates uptime.
.PARAMETER ComputerName
Mandatory parameter of computer to query.
.PARAMETER Units
Optional parameter of uptime units.
Must be either "days" or "hours".
.EXAMPLE
Get-Uptime -ComputerName server1
.EXAMPLE
Get-Uptime -ComputerName server1 -Units Hours
.INPUTS
String computername
.OUTPUTS
Decimal number of uptime in total days or total hours.
.NOTES
Default output is in days. Use the -Units parameter to specify "days" or "hours".
#>
Param(
[parameter(Mandatory)]
[String]
$ComputerName,
[ValidateSet("Days", "Hours")]
[String]
$Units="Days"
)
$WmiOS = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName
$Uptime = (New-TimeSpan $WmiOS.ConvertToDateTime($WmiOS.LastBootUpTime))
Switch ($Units) {
"Days" { $Uptime.TotalDays }
"Hours" { $Uptime.TotalHours }
}
}
Function Get-LastPatch {
<#
.Synopsis
Gets the last patch applied to a computer.
.DESCRIPTION
Uses Get-HotFix to retrieve the last patch applied to a computer.
.PARAMETER ComputerName
Mandatory parameter of computer to query.
.EXAMPLE
Get-LastPatch -ComputerName server1
.INPUTS
String computername
.OUTPUTS
Hotfix object for last patch applied.
.NOTES
The date time value of the hotfix InstalledOn property does not have a time value. Therefore it is not convenient to determine the precise last patch applied. The result returned will be one of the patches applied on the last day, not necessarily the exact last one.
#>
Param(
[String]$ComputerName
)
Get-HotFix -ComputerName $ComputerName | Sort-Object InstalledOn | Select-Object -Last 1
}
Function Get-Report {
<#
.Synopsis
Gets the uptime and last patch date for a list of computers in a text file.
.DESCRIPTION
Gets the uptime and last patch date for a list of computers in a text file.
.PARAMETER Path
Path to the text file of computer names.
.EXAMPLE
Get-Report -Path .\servers.txt
.EXAMPLE
Get-Report -Path .\servers.txt -Verbose
.NOTES
Use the -Verbose common parameter to echo each computer name as it is processed.
#>
Param(
[parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[String]
$Path = ".\servers.txt"
)
# Empty array to hold the report
$Report = @()
ForEach ($Target in (Get-Content $Path)) {
Write-Verbose "Collecting data from $Target."
# Check to see if the computer is online before reporting on it
If (Get-WmiObject -Class Win32_Bios -ComputerName $Target -ErrorAction SilentlyContinue) {
$Report += [PSCustomObject]@{
Server = $Target;
UptimeHours = (Get-Uptime -Units Hours -ComputerName $Target);
PatchDate = (Get-LastPatch -ComputerName $Target).InstalledOn.ToShortDateString()
}
} Else {
$Report += [PSCustomObject]@{
Server = $Target;
UptimeHours = "Offline";
PatchDate = "Offline"
}
}
}
# Print all the results
$Report
}
Export-ModuleMember -Function *