-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-SystemInfo.ps1
352 lines (289 loc) · 12.2 KB
/
Get-SystemInfo.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<#PSScriptInfo
.VERSION 2.0.0
.GUID 21f7b5b3-f9bd-4611-a846-9372c3a89275
.AUTHOR asheroto
.COMPANYNAME asheroto
.TAGS PowerShell Windows get system info information hardware firmware details disk memory network pending reboot usage shutdown event last
.PROJECTURI https://github.com/asheroto/Get-SystemInfo
.RELEASENOTES
[Version 1.0.0] - Initial Release.
[Version 1.0.1] - Added TPM information support.
[Version 1.0.2] - Added graphics card information support.
[Version 2.0.0] - Redesigned for improved functionality with robust support for object-oriented usage, allowing easy access to specific diagnostic sections.
<#
.SYNOPSIS
Gathers detailed system diagnostics, including configuration, hardware, network, and OS status.
.DESCRIPTION
This function provides a complete overview of system information, hardware specifications, network details, and pending reboot status. It is designed to be used either as a standalone script for console output or programmatically as a function to retrieve diagnostics as an object.
.PARAMETER CheckForUpdate
Checks if there is an update available for the script. The latest version information is retrieved from GitHub.
.PARAMETER UpdateSelf
Updates the script to the latest version available in the PowerShell Gallery.
.PARAMETER Version
Displays the current version of the script.
.PARAMETER Help
Displays detailed help information for the script, including usage examples.
.PARAMETER Silent
Suppresses console output when running the script. Useful for retrieving the diagnostics as an object without any visible output.
.EXAMPLE
.\Get-SystemInfo.ps1
Runs the script and displays all diagnostics in a well-formatted console output.
.EXAMPLE
. .\Get-SystemInfo.ps1 -Silent
$info = Get-SystemInfo
Retrieves all diagnostics as an object for programmatic access.
.EXAMPLE
Get-SystemInfo -CheckForUpdate
Checks for updates to the script.
.EXAMPLE
Get-SystemInfo -UpdateSelf
Updates the script to the latest version from the PowerShell Gallery.
.INPUTS
None.
.OUTPUTS
[PSCustomObject]
Returns a custom object containing system diagnostics, including sections such as System, Hardware, TPM, OS, CPU, Memory, Disks, Graphics, NetworkAdapters, PendingReboot, and ShutdownEvents.
.NOTES
Author: asheroto
Version: 2.0.0
Repository: https://github.com/asheroto/Get-SystemInfo
.LINK
https://github.com/asheroto/Get-SystemInfo
#>
[CmdletBinding()]
param (
[switch]$CheckForUpdate,
[switch]$UpdateSelf,
[switch]$Version,
[switch]$Help,
[switch]$Silent
)
# Script information
$CurrentVersion = '2.0.0'
$RepoOwner = 'asheroto'
$RepoName = 'Get-SystemInfo'
$PowerShellGalleryName = 'Get-SystemInfo'
# Preferences
$ProgressPreference = 'SilentlyContinue'
$ConfirmPreference = 'None'
if ($Version.IsPresent) {
$CurrentVersion
exit 0
}
if ($Help) {
Get-Help -Name $MyInvocation.MyCommand.Source -Full
exit 0
}
function Get-SystemInfo {
$result = [PSCustomObject]@{
System = [PSCustomObject]@{
Hostname = $env:COMPUTERNAME
}
Hardware = [PSCustomObject]@{
MakeModel = $null
SerialNumber = $null
FirmwareManufacturer = $null
FirmwareVersion = $null
}
TPM = [PSCustomObject]@{
IsActivated = $false
IsEnabled = $false
IsOwned = $false
Version = $null
}
OS = [PSCustomObject]@{
Version = $null
DisplayVersion = $null
Architecture = $null
Type = $null
InstallDate = $null
LastBootTime = $null
Uptime = $null
}
CPU = [PSCustomObject]@{
MakeModel = $null
SpeedGHz = $null
Usage = $null
Cores = $null
Threads = $null
}
Memory = [PSCustomObject]@{
TotalGB = $null
UsedGB = $null
UsagePercent = $null
DIMMs = @()
}
Disks = @()
Graphics = @()
NetworkAdapters = @()
PendingReboot = @()
ShutdownEvents = @()
}
# Populate Hardware Information
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
$bios = Get-CimInstance -ClassName Win32_BIOS
$result.Hardware.MakeModel = "$($cs.Manufacturer) $($cs.Model)"
$result.Hardware.SerialNumber = $bios.SerialNumber
$result.Hardware.FirmwareManufacturer = $bios.Manufacturer
$result.Hardware.FirmwareVersion = $bios.SMBIOSBIOSVersion
# Populate TPM Information
$tpm = Get-CimInstance -Namespace "root\cimv2\security\microsofttpm" -ClassName Win32_Tpm
if ($tpm) {
$result.TPM.IsActivated = $tpm.IsActivated_InitialValue
$result.TPM.IsEnabled = $tpm.IsEnabled_InitialValue
$result.TPM.IsOwned = $tpm.IsOwned_InitialValue
$result.TPM.Version = ($tpm.SpecVersion -split ',')[0]
}
# Populate OS Information
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$uptime = (Get-Date) - $os.LastBootUpTime
$displayVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DisplayVersion).DisplayVersion
$result.OS.Version = $os.Caption
$result.OS.DisplayVersion = $displayVersion
$result.OS.Architecture = $os.OSArchitecture
$result.OS.InstallDate = $os.InstallDate
$result.OS.LastBootTime = $os.LastBootUpTime
$result.OS.Uptime = "$([math]::Floor($uptime.TotalDays)) days, $($uptime.Hours) hours, $($uptime.Minutes) minutes"
# Determine OS type (Workstation/Server)
$result.OS.Type = if ($os.ProductType -eq 1) {
"Workstation"
} elseif ($os.ProductType -eq 2 -or $os.ProductType -eq 3) {
"Server"
} else {
"Unknown"
}
# Populate CPU Information
$cpu = Get-CimInstance -ClassName Win32_Processor
$result.CPU.MakeModel = $cpu.Name
$result.CPU.SpeedGHz = [math]::Round($cpu.MaxClockSpeed / 1000, 2)
$result.CPU.Usage = "$(($cpu | Measure-Object -Property LoadPercentage -Average).Average)%"
$result.CPU.Cores = $cpu.NumberOfCores
$result.CPU.Threads = $cpu.NumberOfLogicalProcessors
# Populate Memory Information
$result.Memory.TotalGB = [math]::Round($cs.TotalPhysicalMemory / 1GB, 2)
$result.Memory.UsedGB = $result.Memory.TotalGB - [math]::Round($os.FreePhysicalMemory / 1MB, 2)
$result.Memory.UsagePercent = [math]::Round(($result.Memory.UsedGB / $result.Memory.TotalGB) * 100, 2)
$result.Memory.DIMMs = Get-CimInstance -ClassName Win32_PhysicalMemory | ForEach-Object {
[PSCustomObject]@{
DIMMNumber = $_.DeviceLocator
SizeGB = if ($_.Capacity) { [math]::Round($_.Capacity / 1GB, 2) } else { "N/A" }
Model = if ($_.PartNumber) { $_.PartNumber } else { "Unknown" }
SpeedMHz = if ($_.Speed) { $_.Speed } else { "Unknown" }
Manufacturer = if ($_.Manufacturer) { $_.Manufacturer } else { "Unknown" }
}
}
# Populate Disk Information
$result.Disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
[PSCustomObject]@{
Drive = $_.DeviceID
TotalSizeGB = [math]::Round($_.Size / 1GB, 2)
FreeSpaceGB = [math]::Round($_.FreeSpace / 1GB, 2)
UsagePercent = [math]::Round((($_.Size - $_.FreeSpace) / $_.Size) * 100, 2)
}
}
# Populate Graphics Card Information
$result.Graphics = Get-CimInstance -ClassName Win32_VideoController | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
MemoryGB = [math]::Round($_.AdapterRAM / 1GB, 2)
DriverVersion = $_.DriverVersion
DriverDate = $_.DriverDate
}
}
$result.NetworkAdapters = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true } | ForEach-Object {
$adapter = Get-CimInstance -Query "SELECT * FROM Win32_NetworkAdapter WHERE Index = $($_.Index)"
[PSCustomObject]@{
Adapter = $_.Description
IP = $_.IPAddress -join ", "
MAC = $_.MACAddress
SpeedMbps = if ($adapter.Speed) { [math]::Round($adapter.Speed / 1e6, 2) } else { "Unknown" }
DHCPEnabled = $_.DHCPEnabled
}
}
# Populate Pending Reboot Information
$result.PendingReboot = @()
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
$result.PendingReboot += [PSCustomObject]@{
Source = "Windows Update"
Details = "Reboot required for pending updates."
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"
}
}
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
$result.PendingReboot += [PSCustomObject]@{
Source = "Component-Based Servicing"
Details = "Reboot required for servicing stack changes."
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending"
}
}
if ((Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager").PendingFileRenameOperations) {
$result.PendingReboot += [PSCustomObject]@{
Source = "Pending File Rename Operations"
Details = "Reboot required for file operations."
Path = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager"
}
}
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Cluster\PendingReboot") {
$result.PendingReboot += [PSCustomObject]@{
Source = "Cluster Pending Reboot"
Details = "Cluster changes require a reboot."
Path = "HKLM:\SOFTWARE\Microsoft\Cluster\PendingReboot"
}
}
# Populate Shutdown Events
$result.ShutdownEvents = Get-WinEvent -FilterHashtable @{ LogName = 'System'; ID = 1074 } |
Select-Object -First 5 -Property TimeCreated, Message
return $result
}
if ($CheckForUpdate) {
CheckForUpdate -RepoOwner $RepoOwner -RepoName $RepoName -CurrentVersion $CurrentVersion -PowerShellGalleryName $PowerShellGalleryName
}
if ($UpdateSelf) {
UpdateSelf
}
# Main execution
if (-not $Silent) {
$info = Get-SystemInfo
function Write-Section($text) {
<#
.SYNOPSIS
Prints a text block surrounded by a section divider for enhanced output readability.
.DESCRIPTION
This function takes a string input and prints it to the console, surrounded by a section divider made of hash characters.
It is designed to enhance the readability of console output.
.PARAMETER text
The text to be printed within the section divider.
.EXAMPLE
Write-Section "Downloading Files..."
This command prints the text "Downloading Files..." surrounded by a section divider.
#>
Write-Output ""
Write-Output ("#" * ($text.Length + 4))
Write-Output "# $text #"
Write-Output ("#" * ($text.Length + 4))
Write-Output ""
}
Write-Section "System Information"
$info.System | Format-List
Write-Section "Hardware Information"
$info.Hardware | Format-List
Write-Section "TPM Information"
$info.TPM | Format-List
Write-Section "OS Information"
$info.OS | Format-List
Write-Section "CPU Information"
$info.CPU | Format-List
Write-Section "Memory Information"
$info.Memory | Select-Object -Property * -ExcludeProperty DIMMs | Format-List
$info.Memory.DIMMs | Format-Table -AutoSize
Write-Section "Disk Information"
$info.Disks | Format-Table -AutoSize
Write-Section "Graphics Information"
$info.Graphics | Format-Table -AutoSize
Write-Section "Network Adapters"
$info.NetworkAdapters | Format-Table -AutoSize
Write-Section "Pending Reboot Information"
$info.PendingReboot | Format-Table -AutoSize
Write-Section "Last Shutdown Events"
$info.ShutdownEvents | Format-Table -AutoSize
}