PowerShell/Microsoft/Run_System_File_Checker.ps1

56 lines
1.4 KiB
PowerShell

# Script by Timur@0x01337.com
# Date: 2023-11-24
<#
.DESCRIPTION
Runs the "sfc /scannow" command that finds and fixes errors in Windows system files and returns the result of operation.
Requires administrator privileges.
.PARAMETER help
Displays a detailed usage description of this script.
.EXAMPLE
PS> .\Run-System-File-Checker.ps1
.EXAMPLE
PS> .\Run-System-File-Checker.ps1 -help
#>
# Getting command line parameters
param (
[parameter(Mandatory = $false)][switch]$help
)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (!$isAdmin) {
Write-Error "This script requires administrator privileges"
exit 1
}
# Writing help message
if ($help) {
get-help $MyInvocation.MyCommand.Path -Full
exit 0
}
$command = "sfc"
$arguments = "/scannow"
Write-Host "Running $command $arguments"
try {
$process = Start-Process -Wait -PassThru -NoNewWindow -FilePath $command -ArgumentList $arguments
}
catch {
Write-Error "$_, exit code: $($process.ExitCode)"
exit 1
}
if (!$process -or $process.ExitCode -ne 0) {
Write-Error "$command $arguments failed, exit code: $($process.ExitCode)"
exit 1
}
Write-Host "$command $arguments finish, exit code: $($process.ExitCode)"
exit 0