DiskCheck/disk_check.ps1

47 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-06-16 18:18:34 -05:00
# DiskCheck PS script
# Checking a target computer for hard drive failures
#
# 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-06-16 20:09:35 -05:00
Write-Output "Checking disks for $($computerName)"
foreach ($i in $volumes.DriveLetter) {
if ($i.DriveLetter -eq "") {
2021-06-16 20:06:29 -05:00
Write-Output ""
2021-06-16 18:18:34 -05:00
} else {
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
} else {
2021-06-16 20:05:10 -05:00
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: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!"
}