2021-06-16 18:18:34 -05:00
|
|
|
# DiskCheck PS script
|
|
|
|
# Checking a target computer for hard drive failures
|
2022-06-12 19:08:21 -05:00
|
|
|
# Only works for Windows 7+ right now, plan to working for different operating systems
|
2021-06-16 18:18:34 -05:00
|
|
|
#
|
|
|
|
# Written by Wyatt J. Miller
|
|
|
|
# Licensed by Mozilla Public License v2
|
|
|
|
# 2021
|
|
|
|
#
|
|
|
|
|
2021-06-16 19:43:11 -05:00
|
|
|
$logFile="C:\ACS\disk_check.txt"
|
2021-06-16 19:49:27 -05:00
|
|
|
$computerName=$env:ComputerName
|
2021-06-16 18:18:34 -05:00
|
|
|
$rebootTime=Get-Date "1:00"
|
|
|
|
$requireReboot=$false
|
2021-06-16 19:15:50 -05:00
|
|
|
$volumes=Get-Volume
|
2021-06-16 18:18:34 -05:00
|
|
|
|
|
|
|
Function Write-Log{
|
|
|
|
Param ([string]$logstring)
|
|
|
|
|
2021-06-16 19:43:11 -05:00
|
|
|
if (-not(Test-Path -Path $logFile -PathType Leaf)) {
|
2021-06-16 19:46:52 -05:00
|
|
|
New-Item -ItemType File -Path $logFile -Force
|
2021-06-16 19:43:11 -05:00
|
|
|
}
|
|
|
|
|
2021-06-16 18:18:34 -05:00
|
|
|
Add-Content $logFile -value $logstring
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:03:25 -05:00
|
|
|
# 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)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:09:35 -05:00
|
|
|
Write-Output "Checking disks for $($computerName)"
|
|
|
|
|
2021-06-18 19:58:18 -05:00
|
|
|
foreach ($i in $volumes) {
|
2021-07-05 17:03:25 -05:00
|
|
|
if ($i.FileSystemLabel -eq $null) {
|
2021-06-18 19:57:06 -05:00
|
|
|
$result=Repair-Volume -DriveLetter $i.DriveLetter -Scan
|
2021-06-16 20:00:20 -05:00
|
|
|
|
|
|
|
if ($result.value -gt 0) {
|
2021-06-18 19:57:06 -05:00
|
|
|
Repair-Volume -DriveLetter $i.DriveLetter -Scan -OfflineScanAndFix
|
2021-06-16 20:00:20 -05:00
|
|
|
$requireReboot = $true
|
2021-07-05 17:03:25 -05:00
|
|
|
} 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
|
2021-06-16 20:00:20 -05:00
|
|
|
} else {
|
2021-06-16 20:05:10 -05:00
|
|
|
Write-Output "$($i.FileSystemLabel): Drive is OK!"
|
2021-07-05 17:03:25 -05:00
|
|
|
Write-Log "Date: $($now), Volume Name: $($i.FileSystemLabel), Result: $($result), $($computerName)"
|
2021-06-16 20:00:20 -05:00
|
|
|
}
|
2021-06-16 18:18:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($requireReboot -eq $true) {
|
|
|
|
# TODO: Reboot logic goes here
|
|
|
|
Write-Output "Reboot required!"
|
2021-07-16 08:49:58 -05:00
|
|
|
|
|
|
|
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
|
2022-06-12 19:08:21 -05:00
|
|
|
Write-Log "$($now) $($result) $($targetComputer): Unable to repair"
|
2021-06-16 18:18:34 -05:00
|
|
|
}
|