Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions scripts_wip/Win_Battery_Capacity_Check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<#
.Synopsis
Checks the battery full charge capacity VS the design capacity
.DESCRIPTION
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.

If the total full charge capacity is less than the minimum capacity amount, an error is returned.
#>

[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[int]#The minimum battery full charge capacity (as a percentage of design capacity by default). Defaults to 85 percent.
$minimumBatteryCapacity = 85,

[Parameter(Mandatory = $false)]
[switch]#Set the check condition to absolute mWh values instead of a percentage
$absoluteValues
)

try{
$searcher = New-Object System.Management.ManagementObjectSearcher("root\wmi","SELECT * FROM BatteryStaticData")
$batteryStatic = $searcher.Get()
#CIM approach threw errors when Get-WMIObject did not - WMI approach is not available in PSv7, so took .NET approach
$batteryCharge = Get-CimInstance -Namespace "root\wmi" -ClassName "BatteryFullChargedCapacity" -ErrorAction Stop
} catch {
Write-Output "No battery detected"
exit 0
}

if (-not $batteryStatic -or -not $batteryCharge) {
Write-Output "No battery detected"
exit 0
}

$chargeCapacity = $batteryCharge.FullChargedCapacity
$designCapacity = $batteryStatic.DesignedCapacity

$available = [math]::Round(($chargeCapacity / $designCapacity) * 100,2)
$label = "%"
if ($absoluteValues) {
$available = $chargeCapacity
$label = "mWh"
}

If($available -le $minimumBatteryCapacity)
{
Write-Output "The battery needs investigating. Full charge capacity is below the threshold of $minimumBatteryCapacity $label ($available $label available of design capacity $designCapacity mWh."
Exit 1
} else {
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."
Exit 0
}
26 changes: 26 additions & 0 deletions scripts_wip/Win_CPU_Uptime_Check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.Synopsis
Checks Uptime of the computer
.DESCRIPTION
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.

If the totalhours of uptime of the computer is greater than or equal to the warning limit, an error is returned.
#>

[cmdletbinding()]
Param(
[Parameter(Mandatory = $false)]
[int]#Warn if the uptime total hours is over this limit. Defaults to 2.5 days.
$maximumUptimeHoursWarningLimit = 60
)

$uptime = (get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime)
#v7 introduces Get-Uptime, but using WMI is backwards compatiable with v5

If($uptime.TotalHours -ge $maximumUptimeHoursWarningLimit){
"Uptime is over threshold ($($uptime.TotalHours)/$maximumUptimeHoursWarningLimit)"
Exit 1
}

"Uptime is below threshold ($($uptime.TotalHours)/$maximumUptimeHoursWarningLimit)"
Exit 0
92 changes: 92 additions & 0 deletions scripts_wip/Win_Disk_HealthCheck.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<#
.Synopsis
Outputs Drive Health
.DESCRIPTION
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.

Uses the Windows Storage Reliabilty Counters first (the information behind Settings - Storage - Disks & Volumes - %DiskID% - Drive health) to report on drive health.

Will exit if running on a virtual machine.

.NOTES
Learing taken from "Win_Disk_SMART2.ps1" by nullzilla, and modified by: redanthrax
#>

# Requires -Version 5.0
# Requires -RunAsAdministrator
[cmdletbinding()]
Param(
[Parameter(Mandatory = $false)]
[int]#Warn if the temperature (in degrees C) is over this limit
$TemperatureWarningLimit = 55,

[Parameter(Mandatory = $false)]
[int]#Warn if the "wear" of the drive (as a percentage) is above this
$maximumWearAllowance = 20,

[Parameter(Mandatory = $false)]
[switch]#Outputs a full report, instead of warnings only
$fullReport
)

BEGIN {
# If this is a virtual machine, we don't need to continue
$Computer = Get-CimInstance -ClassName 'Win32_ComputerSystem'
if ($Computer.Model -like 'Virtual*') {
exit
}
}

PROCESS {
Try{
#Using Windows Storage Reliabilty Counters first (the information behind Settings - Storage - Disks & Volumes - %DiskID% - Drive health)
$physicalDisks = Get-PhysicalDisk -ErrorAction Stop
$storageResults = @()
foreach ($disk in $physicalDisks) {
$reliabilityCounter = $null
try {
$reliabilityCounter = $disk | Get-StorageReliabilityCounter -ErrorAction Stop
}
catch {
Write-Error "No Storage Reliability Counter for '$($disk.FriendlyName)'. This usually means the driver/controller isn't exposing it."
}

$storageResults += [pscustomobject]@{
FriendlyName = $disk.FriendlyName
SerialNumber = $disk.SerialNumber
BusType = $disk.BusType
HealthStatus = $disk.HealthStatus
OperationalStatus = ($disk.OperationalStatus -join ", ")
Temperature = $reliabilityCounter.Temperature
Wear = $reliabilityCounter.Wear
ReadErrorsTotal = $reliabilityCounter.ReadErrorsTotal
WriteErrorsTotal = $reliabilityCounter.WriteErrorsTotal
ReallocatedSectors = $reliabilityCounter.ReallocatedSectors
PowerOnHours = $reliabilityCounter.PowerOnHours
}

If(
$disk.HealthStatus.ToLower() -ne "healthy" -or
($disk.OperationalStatus | Where-Object -FilterScript { $_.ToLower() -ne "ok" }) -or
$reliabilityCounter.Wear -ge $maximumWearAllowance -or
$reliabilityCounter.Temperature -ge $TemperatureWarningLimit
){
Write-Error -Message "$($disk.FriendlyName) has conditions that require investigation. $storageResults"
}
}

If($fullReport) { $storageResults }

} catch {
Write-Error -Message "Get-PhysicalDisk failed. This can happen on older OS builds or restricted environments."
}
}

END{
if ($error) {
Write-Output $error
exit 1
}
Write-Output "All drives report as healthy"
Exit 0
}
Loading