DiskCheck/disk_check.ps1

95 lines
2.8 KiB
PowerShell

# DiskCheck PS script
# Checking a target computer for hard drive failures
#
# Written by Wyatt J. Miller
# Licensed by Mozilla Public License v2
# 2021
#
$logFile="C:\ACS\disk_check.txt"
$computerName=$env:ComputerName
$rebootTime=Get-Date "1:00"
$requireReboot=$false
$volumes=Get-Volume
Function Write-Log{
Param ([string]$logstring)
if (-not(Test-Path -Path $logFile -PathType Leaf)) {
New-Item -ItemType File -Path $logFile -Force
}
Add-Content $logFile -value $logstring
}
# function to scan the disk whether we have to use a drive letter
# (i.e. C:, D:, etc) or a filesystem label (i.e. Windows, etc.)
Function Scan-Disk{
Param ([string]$volume_identifier)
$result=Repair-Volume -DriveLetter $i.DriveLetter -Scan
if ($result.value -gt 0) {
Repair-Volume -DriveLetter $i.DriveLetter -Scan -OfflineScanAndFix
$requireReboot = $true
} else {
Write-Output "$($i.FileSystemLabel): Drive is OK!"
Write-Log "Date: $($now), Drive Letter: $($i.DriveLetter), Volume Name: $($i.FileSystemLabel), Result: $($result), $($computerName)"
}
}
Write-Output "Checking disks for $($computerName)"
foreach ($i in $volumes) {
if ($i.FileSystemLabel -eq $null) {
$result=Repair-Volume -DriveLetter $i.DriveLetter -Scan
if ($result.value -gt 0) {
Repair-Volume -DriveLetter $i.DriveLetter -Scan -OfflineScanAndFix
$requireReboot = $true
} else {
Write-Output "$($i.DriveLetter): Drive is OK!"
Write-Log "Date: $($now), Drive Letter: $($i.DriveLetter), Result: $($result), $($computerName)"
}
} else {
if ($i.DriveLetter -eq $null) {
Write-Output "Volume is not identifiable! Skipping..."
continue
}
$result=Repair-Volume -FileSystemLabel $i.FileSystemLabel -Scan
if ($result.value -gt 0) {
Repair-Volume -FileSystemLabel $i.FileSystemLabel -Scan -OfflineScanAndFix
$requireReboot = $true
} else {
Write-Output "$($i.FileSystemLabel): Drive is OK!"
Write-Log "Date: $($now), Volume Name: $($i.FileSystemLabel), Result: $($result), $($computerName)"
}
}
}
if ($requireReboot -eq $true) {
# TODO: Reboot logic goes here
Write-Output "Reboot required!"
do
{
Write-Output 'Sleeping while waiting for time window'
Write-Output 'Current Time:' + $now.TimeOfDay
$now = Get-Date
Start-Sleep -s 1200 #20 Minutes (1200 Seconds)
}
while ($now.TimeOfDay -gt $earliestRebootTime.TimeOfDay -and $now.TimeOfDay -lt $latestRebootTime.TimeOfDay)
#Reboot PC with Faulty Disk for Offline Repair
Restart-Computer -ComputerName $target -Force -Delay -Wait
#Wait for Reboot and Scan PC with Faulty Disk
$result = Repair-Volume CimSession $cimSession -FileSystemLabel $volume.FileSystemLabel -Scan
#Report Un-Repairable Disks
Write-Log $now +','+ $result + ',' + $targetComputer, +',' + 'Unable to repair'
}