Skip to content

Commit 3160e96

Browse files
Merge pull request #304 from sid351/main
Added PowerShell Scripts into WIP folder
2 parents f7fc17f + 90758bb commit 3160e96

File tree

7 files changed

+458
-361
lines changed

7 files changed

+458
-361
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<#
2+
.Synopsis
3+
Checks the battery full charge capacity VS the design capacity
4+
.DESCRIPTION
5+
This was written specifically for use as a "Script Check" in mind, where it the output is deliberaly light unless a warning or error condition is found that needs more investigation.
6+
7+
If the total full charge capacity is less than the minimum capacity amount, an error is returned.
8+
#>
9+
10+
[CmdletBinding()]
11+
param(
12+
[Parameter(Mandatory = $false)]
13+
[int]#The minimum battery full charge capacity (as a percentage of design capacity by default). Defaults to 85 percent.
14+
$minimumBatteryCapacity = 85,
15+
16+
[Parameter(Mandatory = $false)]
17+
[switch]#Set the check condition to absolute mWh values instead of a percentage
18+
$absoluteValues
19+
)
20+
21+
try{
22+
$searcher = New-Object System.Management.ManagementObjectSearcher("root\wmi","SELECT * FROM BatteryStaticData")
23+
$batteryStatic = $searcher.Get()
24+
#CIM approach threw errors when Get-WMIObject did not - WMI approach is not available in PSv7, so took .NET approach
25+
$batteryCharge = Get-CimInstance -Namespace "root\wmi" -ClassName "BatteryFullChargedCapacity" -ErrorAction Stop
26+
} catch {
27+
Write-Output "No battery detected"
28+
exit 0
29+
}
30+
31+
if (-not $batteryStatic -or -not $batteryCharge) {
32+
Write-Output "No battery detected"
33+
exit 0
34+
}
35+
36+
$chargeCapacity = $batteryCharge.FullChargedCapacity
37+
$designCapacity = $batteryStatic.DesignedCapacity
38+
39+
$available = [math]::Round(($chargeCapacity / $designCapacity) * 100,2)
40+
$label = "%"
41+
if ($absoluteValues) {
42+
$available = $chargeCapacity
43+
$label = "mWh"
44+
}
45+
46+
If($available -le $minimumBatteryCapacity)
47+
{
48+
Write-Output "The battery needs investigating. Full charge capacity is below the threshold of $minimumBatteryCapacity $label ($available $label available of design capacity $designCapacity mWh."
49+
Exit 1
50+
} else {
51+
Write-Output "The battery is reporting ok. Full charge capacity is above the threshold of $minimumBatteryCapacity $label ($available $label available of design capacity $designCapacity mWh."
52+
Exit 0
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<#
2+
.Synopsis
3+
Checks Uptime of the computer
4+
.DESCRIPTION
5+
This was written specifically for use as a "Script Check" in mind, where it the output is deliberaly light unless a warning or error condition is found that needs more investigation.
6+
7+
If the totalhours of uptime of the computer is greater than or equal to the warning limit, an error is returned.
8+
#>
9+
10+
[cmdletbinding()]
11+
Param(
12+
[Parameter(Mandatory = $false)]
13+
[int]#Warn if the uptime total hours is over this limit. Defaults to 2.5 days.
14+
$maximumUptimeHoursWarningLimit = 60
15+
)
16+
17+
$uptime = (get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime)
18+
#v7 introduces Get-Uptime, but using WMI is backwards compatiable with v5
19+
20+
If($uptime.TotalHours -ge $maximumUptimeHoursWarningLimit){
21+
"Uptime is over threshold ($($uptime.TotalHours)/$maximumUptimeHoursWarningLimit)"
22+
Exit 1
23+
}
24+
25+
"Uptime is below threshold ($($uptime.TotalHours)/$maximumUptimeHoursWarningLimit)"
26+
Exit 0
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<#
2+
.Synopsis
3+
Outputs Drive Health
4+
.DESCRIPTION
5+
This was written specifically for use as a "Script Check" in mind, where it the output is deliberaly light unless a warning or error condition is found that needs more investigation.
6+
7+
Uses the Windows Storage Reliabilty Counters first (the information behind Settings - Storage - Disks & Volumes - %DiskID% - Drive health) to report on drive health.
8+
9+
Will exit if running on a virtual machine.
10+
11+
.NOTES
12+
Learing taken from "Win_Disk_SMART2.ps1" by nullzilla, and modified by: redanthrax
13+
#>
14+
15+
# Requires -Version 5.0
16+
# Requires -RunAsAdministrator
17+
[cmdletbinding()]
18+
Param(
19+
[Parameter(Mandatory = $false)]
20+
[int]#Warn if the temperature (in degrees C) is over this limit
21+
$TemperatureWarningLimit = 55,
22+
23+
[Parameter(Mandatory = $false)]
24+
[int]#Warn if the "wear" of the drive (as a percentage) is above this
25+
$maximumWearAllowance = 20,
26+
27+
[Parameter(Mandatory = $false)]
28+
[switch]#Outputs a full report, instead of warnings only
29+
$fullReport
30+
)
31+
32+
BEGIN {
33+
# If this is a virtual machine, we don't need to continue
34+
$Computer = Get-CimInstance -ClassName 'Win32_ComputerSystem'
35+
if ($Computer.Model -like 'Virtual*') {
36+
exit
37+
}
38+
}
39+
40+
PROCESS {
41+
Try{
42+
#Using Windows Storage Reliabilty Counters first (the information behind Settings - Storage - Disks & Volumes - %DiskID% - Drive health)
43+
$physicalDisks = Get-PhysicalDisk -ErrorAction Stop
44+
$storageResults = @()
45+
foreach ($disk in $physicalDisks) {
46+
$reliabilityCounter = $null
47+
try {
48+
$reliabilityCounter = $disk | Get-StorageReliabilityCounter -ErrorAction Stop
49+
}
50+
catch {
51+
Write-Error "No Storage Reliability Counter for '$($disk.FriendlyName)'. This usually means the driver/controller isn't exposing it."
52+
}
53+
54+
$storageResults += [pscustomobject]@{
55+
FriendlyName = $disk.FriendlyName
56+
SerialNumber = $disk.SerialNumber
57+
BusType = $disk.BusType
58+
HealthStatus = $disk.HealthStatus
59+
OperationalStatus = ($disk.OperationalStatus -join ", ")
60+
Temperature = $reliabilityCounter.Temperature
61+
Wear = $reliabilityCounter.Wear
62+
ReadErrorsTotal = $reliabilityCounter.ReadErrorsTotal
63+
WriteErrorsTotal = $reliabilityCounter.WriteErrorsTotal
64+
ReallocatedSectors = $reliabilityCounter.ReallocatedSectors
65+
PowerOnHours = $reliabilityCounter.PowerOnHours
66+
}
67+
68+
If(
69+
$disk.HealthStatus.ToLower() -ne "healthy" -or
70+
($disk.OperationalStatus | Where-Object -FilterScript { $_.ToLower() -ne "ok" }) -or
71+
$reliabilityCounter.Wear -ge $maximumWearAllowance -or
72+
$reliabilityCounter.Temperature -ge $TemperatureWarningLimit
73+
){
74+
Write-Error -Message "$($disk.FriendlyName) has conditions that require investigation. $storageResults"
75+
}
76+
}
77+
78+
If($fullReport) { $storageResults }
79+
80+
} catch {
81+
Write-Error -Message "Get-PhysicalDisk failed. This can happen on older OS builds or restricted environments."
82+
}
83+
}
84+
85+
END{
86+
if ($error) {
87+
Write-Output $error
88+
exit 1
89+
}
90+
Write-Output "All drives report as healthy"
91+
Exit 0
92+
}

0 commit comments

Comments
 (0)