909 lines
31 KiB
PowerShell
909 lines
31 KiB
PowerShell
#requires -Version 5.1
|
|
|
|
<#
|
|
WindowsMaintenanceReport.ps1
|
|
|
|
Produces a structured JSON report instead of a human-formatted TXT report.
|
|
|
|
Modes:
|
|
Default Compact maintenance snapshot
|
|
-Mid Hardware/software/startup/driver inventory
|
|
-Full Everything, including networking, event logs, processes,
|
|
firewall, devices, DISM and SFC verification.
|
|
|
|
Examples:
|
|
.\WindowsMaintenanceReport.ps1
|
|
.\WindowsMaintenanceReport.ps1 -Mid
|
|
.\WindowsMaintenanceReport.ps1 -Full
|
|
.\WindowsMaintenanceReport.ps1 -Mid -OutputDirectory C:\Temp
|
|
.\WindowsMaintenanceReport.ps1 -Full -EventLogDays 30
|
|
|
|
The report is UTF-8 JSON and is designed to be both human-readable and
|
|
machine-readable. PowerShell 5.1 is supported without extra modules.
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
param(
|
|
[switch]$Mid,
|
|
[switch]$Full,
|
|
|
|
[string]$OutputDirectory = '',
|
|
|
|
[ValidateRange(1, 3650)]
|
|
[int]$EventLogDays = 7,
|
|
|
|
[ValidateRange(1, 5000)]
|
|
[int]$MaxEventErrors = 100,
|
|
|
|
[ValidateRange(3, 100)]
|
|
[int]$JsonDepth = 12
|
|
)
|
|
|
|
$ErrorActionPreference = 'Continue'
|
|
|
|
if ($Mid -and $Full) {
|
|
throw 'Choose either -Mid or -Full, not both.'
|
|
}
|
|
|
|
function Test-IsAdministrator {
|
|
try {
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
|
return $principal.IsInRole(
|
|
[Security.Principal.WindowsBuiltInRole]::Administrator
|
|
)
|
|
}
|
|
catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Test-DirectoryWritable {
|
|
param([string]$Path)
|
|
|
|
try {
|
|
if (-not (Test-Path -LiteralPath $Path)) {
|
|
New-Item -ItemType Directory -Path $Path -Force -ErrorAction Stop | Out-Null
|
|
}
|
|
|
|
$testFile = Join-Path $Path (
|
|
'.winreport_write_test_' +
|
|
[guid]::NewGuid().ToString('N') +
|
|
'.tmp'
|
|
)
|
|
|
|
[System.IO.File]::WriteAllText($testFile, 'test')
|
|
Remove-Item -LiteralPath $testFile -Force -ErrorAction SilentlyContinue
|
|
return $true
|
|
}
|
|
catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Get-InstalledPrograms {
|
|
$paths = @(
|
|
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
|
'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
|
'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
|
|
)
|
|
|
|
Get-ItemProperty $paths -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.DisplayName } |
|
|
Select-Object DisplayName, DisplayVersion, Publisher,
|
|
InstallDate, InstallLocation |
|
|
Sort-Object DisplayName -Unique
|
|
}
|
|
|
|
function Get-StartupRegistryEntries {
|
|
$startupKeys = @(
|
|
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Run',
|
|
'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce',
|
|
'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run',
|
|
'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run',
|
|
'HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce'
|
|
)
|
|
|
|
foreach ($key in $startupKeys) {
|
|
if (Test-Path $key) {
|
|
$item = Get-ItemProperty $key -ErrorAction SilentlyContinue
|
|
|
|
foreach ($property in $item.PSObject.Properties) {
|
|
if ($property.Name -notlike 'PS*') {
|
|
[pscustomobject][ordered]@{
|
|
registry_path = $key
|
|
name = $property.Name
|
|
value = [string]$property.Value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function Get-ProblemDevices {
|
|
if (Get-Command Get-PnpDevice -ErrorAction SilentlyContinue) {
|
|
Get-PnpDevice -PresentOnly -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.Status -ne 'OK' } |
|
|
Select-Object Status, Class, FriendlyName, InstanceId
|
|
}
|
|
else {
|
|
Get-CimInstance Win32_PnPEntity -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.ConfigManagerErrorCode -ne 0 } |
|
|
Select-Object Name, PNPClass, ConfigManagerErrorCode, DeviceID
|
|
}
|
|
}
|
|
|
|
function Invoke-ReportSection {
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$Name,
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
[scriptblock]$Script,
|
|
|
|
[switch]$Quiet
|
|
)
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host ('Collecting: ' + $Name)
|
|
}
|
|
|
|
try {
|
|
$data = @(& $Script)
|
|
|
|
return [pscustomobject][ordered]@{
|
|
status = 'ok'
|
|
count = $data.Count
|
|
data = $data
|
|
error = $null
|
|
}
|
|
}
|
|
catch {
|
|
return [pscustomobject][ordered]@{
|
|
status = 'error'
|
|
count = 0
|
|
data = @()
|
|
error = $_.Exception.Message
|
|
}
|
|
}
|
|
}
|
|
|
|
function New-SkippedSection {
|
|
param([string]$Reason)
|
|
|
|
[pscustomobject][ordered]@{
|
|
status = 'skipped'
|
|
count = 0
|
|
data = @()
|
|
error = $Reason
|
|
}
|
|
}
|
|
|
|
function New-UnavailableSection {
|
|
param([string]$Reason)
|
|
|
|
[pscustomobject][ordered]@{
|
|
status = 'unavailable'
|
|
count = 0
|
|
data = @()
|
|
error = $Reason
|
|
}
|
|
}
|
|
|
|
function Get-StorageReport {
|
|
param([bool]$IsAdmin)
|
|
|
|
$logical = @(
|
|
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" |
|
|
Select-Object DeviceID, VolumeName, FileSystem,
|
|
@{Name='size_gb';Expression={[math]::Round($_.Size / 1GB, 2)}},
|
|
@{Name='free_gb';Expression={[math]::Round($_.FreeSpace / 1GB, 2)}},
|
|
@{Name='free_percent';Expression={
|
|
if ($_.Size) {
|
|
[math]::Round(($_.FreeSpace / $_.Size) * 100, 1)
|
|
}
|
|
else {
|
|
$null
|
|
}
|
|
}}
|
|
)
|
|
|
|
$physical = @(
|
|
Get-CimInstance Win32_DiskDrive |
|
|
Select-Object Model, InterfaceType, MediaType,
|
|
@{Name='size_gb';Expression={[math]::Round($_.Size / 1GB, 2)}},
|
|
FirmwareRevision, SerialNumber, Status, DeviceID, Index
|
|
)
|
|
|
|
$storageHealth = @()
|
|
$reliability = @()
|
|
|
|
if (Get-Command Get-PhysicalDisk -ErrorAction SilentlyContinue) {
|
|
try {
|
|
$physicalDisks = @(Get-PhysicalDisk -ErrorAction Stop)
|
|
|
|
$storageHealth = @(
|
|
$physicalDisks |
|
|
Select-Object FriendlyName, MediaType, HealthStatus,
|
|
OperationalStatus,
|
|
@{Name='size_gb';Expression={[math]::Round($_.Size / 1GB, 2)}},
|
|
DeviceId, BusType
|
|
)
|
|
|
|
if ($IsAdmin) {
|
|
foreach ($disk in $physicalDisks) {
|
|
try {
|
|
$counter = $disk |
|
|
Get-StorageReliabilityCounter -ErrorAction Stop |
|
|
Select-Object Temperature, Wear, ReadErrorsTotal,
|
|
WriteErrorsTotal, PowerOnHours
|
|
|
|
$reliability += [pscustomobject][ordered]@{
|
|
disk = $disk.FriendlyName
|
|
status = 'ok'
|
|
data = $counter
|
|
error = $null
|
|
}
|
|
}
|
|
catch {
|
|
$reliability += [pscustomobject][ordered]@{
|
|
disk = $disk.FriendlyName
|
|
status = 'unavailable'
|
|
data = $null
|
|
error = $_.Exception.Message
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
$storageHealth = @(
|
|
[pscustomobject][ordered]@{
|
|
status = 'unavailable'
|
|
error = $_.Exception.Message
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
[pscustomobject][ordered]@{
|
|
logical_volumes = $logical
|
|
physical_disks = $physical
|
|
storage_health = $storageHealth
|
|
reliability_counters = $reliability
|
|
reliability_requires_admin = $true
|
|
}
|
|
}
|
|
|
|
function Get-DefenderReport {
|
|
if (-not (Get-Command Get-MpComputerStatus -ErrorAction SilentlyContinue)) {
|
|
return [pscustomobject][ordered]@{
|
|
available = $false
|
|
error = 'Microsoft Defender cmdlets unavailable.'
|
|
}
|
|
}
|
|
|
|
try {
|
|
$def = Get-MpComputerStatus -ErrorAction Stop
|
|
|
|
return $def |
|
|
Select-Object AntivirusEnabled, AntispywareEnabled,
|
|
RealTimeProtectionEnabled, BehaviorMonitorEnabled,
|
|
IoavProtectionEnabled, NISEnabled,
|
|
AntivirusSignatureLastUpdated,
|
|
AntispywareSignatureLastUpdated,
|
|
QuickScanAge, FullScanAge
|
|
}
|
|
catch {
|
|
return [pscustomobject][ordered]@{
|
|
available = $false
|
|
error = $_.Exception.Message
|
|
}
|
|
}
|
|
}
|
|
|
|
function Invoke-NativeCommand {
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$FilePath,
|
|
|
|
[string[]]$Arguments = @()
|
|
)
|
|
|
|
try {
|
|
$output = @(& $FilePath @Arguments 2>&1 | ForEach-Object { [string]$_ })
|
|
$exitCode = $LASTEXITCODE
|
|
|
|
[pscustomobject][ordered]@{
|
|
executable = $FilePath
|
|
arguments = $Arguments
|
|
exit_code = $exitCode
|
|
output = $output
|
|
}
|
|
}
|
|
catch {
|
|
[pscustomobject][ordered]@{
|
|
executable = $FilePath
|
|
arguments = $Arguments
|
|
exit_code = $null
|
|
output = @()
|
|
error = $_.Exception.Message
|
|
}
|
|
}
|
|
}
|
|
|
|
$isAdmin = Test-IsAdministrator
|
|
|
|
if ([string]::IsNullOrWhiteSpace($OutputDirectory)) {
|
|
# Resolve the Windows Documents folder, including redirected/OneDrive paths.
|
|
$candidate = [Environment]::GetFolderPath(
|
|
[Environment+SpecialFolder]::MyDocuments,
|
|
[Environment+SpecialFolderOption]::DoNotVerify
|
|
)
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($candidate) -and
|
|
(Test-DirectoryWritable $candidate)) {
|
|
$OutputDirectory = $candidate
|
|
}
|
|
elseif (Test-DirectoryWritable $env:TEMP) {
|
|
$OutputDirectory = $env:TEMP
|
|
}
|
|
else {
|
|
throw 'Could not find a writable output directory.'
|
|
}
|
|
}
|
|
elseif (-not (Test-DirectoryWritable $OutputDirectory)) {
|
|
throw "Output directory is not writable: $OutputDirectory"
|
|
}
|
|
|
|
$mode = 'Compact'
|
|
if ($Mid) { $mode = 'Mid' }
|
|
if ($Full) { $mode = 'Full' }
|
|
|
|
$startedAt = Get-Date
|
|
$timestamp = $startedAt.ToString('yyyyMMdd_HHmmss')
|
|
$computerName = $env:COMPUTERNAME
|
|
$reportName = 'windows_system_report_' + $computerName + '_' + $timestamp + '.json'
|
|
$reportPath = Join-Path $OutputDirectory $reportName
|
|
|
|
$report = [ordered]@{
|
|
schema_version = 1
|
|
report_type = 'windows_system_maintenance'
|
|
mode = $mode
|
|
generated_at = $startedAt.ToString('o')
|
|
completed_at = $null
|
|
|
|
host = [ordered]@{
|
|
computer_name = $computerName
|
|
user_name = $env:USERNAME
|
|
administrator = $isAdmin
|
|
powershell_version = [string]$PSVersionTable.PSVersion
|
|
}
|
|
|
|
parameters = [ordered]@{
|
|
event_log_days = $EventLogDays
|
|
max_event_errors = $MaxEventErrors
|
|
}
|
|
|
|
warnings = @()
|
|
summary = $null
|
|
sections = [ordered]@{}
|
|
}
|
|
|
|
if (-not $isAdmin) {
|
|
$report.warnings += 'Script is not elevated. Admin-only diagnostics are skipped.'
|
|
}
|
|
|
|
if ($mode -eq 'Compact') {
|
|
Write-Host 'Collecting compact Windows maintenance report...'
|
|
|
|
$os = Get-CimInstance Win32_OperatingSystem
|
|
$system = Get-CimInstance Win32_ComputerSystem
|
|
$bios = Get-CimInstance Win32_BIOS
|
|
$cpu = Get-CimInstance Win32_Processor | Select-Object -First 1
|
|
$gpus = @(Get-CimInstance Win32_VideoController -ErrorAction SilentlyContinue)
|
|
$logicalDisks = @(Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3")
|
|
$programs = @(Get-InstalledPrograms)
|
|
$appx = @(Get-AppxPackage -ErrorAction SilentlyContinue)
|
|
$startupRegistry = @(Get-StartupRegistryEntries)
|
|
$startupCommands = @(Get-CimInstance Win32_StartupCommand -ErrorAction SilentlyContinue)
|
|
$autoServices = @(
|
|
Get-CimInstance Win32_Service -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.StartMode -eq 'Auto' }
|
|
)
|
|
$stoppedAuto = @($autoServices | Where-Object { $_.State -ne 'Running' })
|
|
$problemDevices = @(Get-ProblemDevices)
|
|
$latestHotfix = Get-HotFix -ErrorAction SilentlyContinue |
|
|
Sort-Object InstalledOn -Descending |
|
|
Select-Object -First 1
|
|
|
|
$uptime = (Get-Date) - $os.LastBootUpTime
|
|
|
|
$diskSummary = @(
|
|
$logicalDisks | ForEach-Object {
|
|
[pscustomobject][ordered]@{
|
|
drive = $_.DeviceID
|
|
volume_name = $_.VolumeName
|
|
filesystem = $_.FileSystem
|
|
size_gb = if ($_.Size) {[math]::Round($_.Size / 1GB, 2)} else {$null}
|
|
free_gb = if ($_.FreeSpace -ne $null) {[math]::Round($_.FreeSpace / 1GB, 2)} else {$null}
|
|
free_percent = if ($_.Size) {[math]::Round(($_.FreeSpace / $_.Size) * 100, 1)} else {$null}
|
|
}
|
|
}
|
|
)
|
|
|
|
$diskHealth = @()
|
|
if (Get-Command Get-PhysicalDisk -ErrorAction SilentlyContinue) {
|
|
try {
|
|
$diskHealth = @(
|
|
Get-PhysicalDisk -ErrorAction Stop |
|
|
Select-Object FriendlyName, MediaType, HealthStatus,
|
|
OperationalStatus,
|
|
@{Name='size_gb';Expression={[math]::Round($_.Size / 1GB, 2)}}
|
|
)
|
|
}
|
|
catch {
|
|
$report.warnings += ('Physical disk health unavailable: ' + $_.Exception.Message)
|
|
}
|
|
}
|
|
|
|
$defender = Get-DefenderReport
|
|
|
|
$report.summary = [ordered]@{
|
|
system = [ordered]@{
|
|
manufacturer = $system.Manufacturer
|
|
model = $system.Model
|
|
system_type = $system.SystemType
|
|
}
|
|
|
|
operating_system = [ordered]@{
|
|
caption = $os.Caption
|
|
version = $os.Version
|
|
build_number = $os.BuildNumber
|
|
architecture = $os.OSArchitecture
|
|
install_date = $os.InstallDate
|
|
last_boot = $os.LastBootUpTime
|
|
}
|
|
|
|
bios = [ordered]@{
|
|
manufacturer = $bios.Manufacturer
|
|
name = $bios.Name
|
|
smbios_version = $bios.SMBIOSBIOSVersion
|
|
release_date = $bios.ReleaseDate
|
|
serial_number = $bios.SerialNumber
|
|
}
|
|
|
|
cpu = [ordered]@{
|
|
name = $cpu.Name
|
|
manufacturer = $cpu.Manufacturer
|
|
cores = $cpu.NumberOfCores
|
|
logical_processors = $cpu.NumberOfLogicalProcessors
|
|
max_clock_mhz = $cpu.MaxClockSpeed
|
|
}
|
|
|
|
memory = [ordered]@{
|
|
total_gb = [math]::Round($system.TotalPhysicalMemory / 1GB, 2)
|
|
free_gb = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
|
|
}
|
|
|
|
gpus = @(
|
|
$gpus | Select-Object Name, DriverVersion, VideoModeDescription
|
|
)
|
|
|
|
storage = [ordered]@{
|
|
logical_volumes = $diskSummary
|
|
physical_health = $diskHealth
|
|
}
|
|
|
|
software = [ordered]@{
|
|
desktop_program_count = $programs.Count
|
|
appx_package_count = $appx.Count
|
|
}
|
|
|
|
startup = [ordered]@{
|
|
registry_entry_count = $startupRegistry.Count
|
|
command_count = $startupCommands.Count
|
|
}
|
|
|
|
services = [ordered]@{
|
|
automatic_count = $autoServices.Count
|
|
automatic_not_running = $stoppedAuto.Count
|
|
}
|
|
|
|
devices = [ordered]@{
|
|
problem_count = $problemDevices.Count
|
|
}
|
|
|
|
latest_hotfix = if ($latestHotfix) {
|
|
[ordered]@{
|
|
hotfix_id = $latestHotfix.HotFixID
|
|
description = $latestHotfix.Description
|
|
installed_by = $latestHotfix.InstalledBy
|
|
installed_on = $latestHotfix.InstalledOn
|
|
}
|
|
}
|
|
else {
|
|
$null
|
|
}
|
|
|
|
defender = $defender
|
|
|
|
uptime = [ordered]@{
|
|
last_boot = $os.LastBootUpTime
|
|
days = $uptime.Days
|
|
hours = $uptime.Hours
|
|
minutes = $uptime.Minutes
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
# ----------------------------------------------------------------------
|
|
# MID + FULL COMMON SECTIONS
|
|
# ----------------------------------------------------------------------
|
|
|
|
$report.sections.operating_system = Invoke-ReportSection 'Operating system' {
|
|
Get-CimInstance Win32_OperatingSystem |
|
|
Select-Object Caption, Version, BuildNumber, OSArchitecture,
|
|
InstallDate, LastBootUpTime, LocalDateTime,
|
|
@{Name='total_memory_gb';Expression={[math]::Round($_.TotalVisibleMemorySize / 1MB, 2)}},
|
|
@{Name='free_memory_gb';Expression={[math]::Round($_.FreePhysicalMemory / 1MB, 2)}}
|
|
}
|
|
|
|
$report.sections.computer_system = Invoke-ReportSection 'Computer system' {
|
|
Get-CimInstance Win32_ComputerSystem |
|
|
Select-Object Manufacturer, Model, SystemType,
|
|
@{Name='total_physical_memory_gb';Expression={[math]::Round($_.TotalPhysicalMemory / 1GB, 2)}},
|
|
Domain, PartOfDomain
|
|
}
|
|
|
|
$report.sections.bios = Invoke-ReportSection 'BIOS' {
|
|
Get-CimInstance Win32_BIOS |
|
|
Select-Object Manufacturer, Name, SMBIOSBIOSVersion,
|
|
ReleaseDate, SerialNumber
|
|
}
|
|
|
|
$report.sections.cpu = Invoke-ReportSection 'CPU' {
|
|
Get-CimInstance Win32_Processor |
|
|
Select-Object Name, Manufacturer, NumberOfCores,
|
|
NumberOfLogicalProcessors, MaxClockSpeed,
|
|
CurrentClockSpeed, LoadPercentage
|
|
}
|
|
|
|
$report.sections.memory_modules = Invoke-ReportSection 'Memory modules' {
|
|
Get-CimInstance Win32_PhysicalMemory |
|
|
Select-Object BankLabel, DeviceLocator,
|
|
@{Name='capacity_gb';Expression={[math]::Round($_.Capacity / 1GB, 2)}},
|
|
Speed, ConfiguredClockSpeed, Manufacturer, PartNumber
|
|
}
|
|
|
|
$report.sections.motherboard = Invoke-ReportSection 'Motherboard' {
|
|
Get-CimInstance Win32_BaseBoard |
|
|
Select-Object Manufacturer, Product, Version, SerialNumber
|
|
}
|
|
|
|
$report.sections.gpus = Invoke-ReportSection 'GPU / display adapters' {
|
|
Get-CimInstance Win32_VideoController |
|
|
Select-Object Name, DriverVersion, VideoModeDescription
|
|
}
|
|
|
|
$report.sections.storage = Invoke-ReportSection 'Storage / free space / health' {
|
|
Get-StorageReport -IsAdmin $isAdmin
|
|
}
|
|
|
|
$report.sections.installed_programs = Invoke-ReportSection 'Installed programs' {
|
|
Get-InstalledPrograms
|
|
}
|
|
|
|
if (Get-Command Get-AppxPackage -ErrorAction SilentlyContinue) {
|
|
$report.sections.appx_packages = Invoke-ReportSection 'Microsoft Store / AppX packages' {
|
|
Get-AppxPackage -ErrorAction SilentlyContinue |
|
|
Select-Object Name, Version, Publisher, Architecture |
|
|
Sort-Object Name
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.appx_packages =
|
|
New-UnavailableSection 'Get-AppxPackage is unavailable.'
|
|
}
|
|
|
|
$report.sections.startup_registry = Invoke-ReportSection 'Startup registry locations' {
|
|
Get-StartupRegistryEntries |
|
|
Sort-Object registry_path, name
|
|
}
|
|
|
|
$report.sections.automatic_services = Invoke-ReportSection 'Automatic services' {
|
|
Get-CimInstance Win32_Service |
|
|
Where-Object { $_.StartMode -eq 'Auto' } |
|
|
Select-Object Name, DisplayName, State, StartMode, StartName |
|
|
Sort-Object State, DisplayName
|
|
}
|
|
|
|
$report.sections.signed_drivers = Invoke-ReportSection 'Signed drivers' {
|
|
Get-CimInstance Win32_PnPSignedDriver |
|
|
Where-Object { $_.DeviceName } |
|
|
Select-Object DeviceName, Manufacturer, DriverVersion, DriverDate |
|
|
Sort-Object DeviceName
|
|
}
|
|
|
|
$report.sections.hotfixes = Invoke-ReportSection 'Windows updates / hotfixes' {
|
|
Get-HotFix -ErrorAction SilentlyContinue |
|
|
Sort-Object InstalledOn -Descending |
|
|
Select-Object HotFixID, Description, InstalledBy, InstalledOn
|
|
}
|
|
|
|
$report.sections.defender = Invoke-ReportSection 'Windows Defender status' {
|
|
Get-DefenderReport
|
|
}
|
|
|
|
if (Get-Command Get-ScheduledTask -ErrorAction SilentlyContinue) {
|
|
$report.sections.non_microsoft_scheduled_tasks =
|
|
Invoke-ReportSection 'Non-Microsoft scheduled tasks' {
|
|
Get-ScheduledTask -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.TaskPath -notlike '\Microsoft\*' } |
|
|
Select-Object TaskName, TaskPath, State |
|
|
Sort-Object TaskPath, TaskName
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.non_microsoft_scheduled_tasks =
|
|
New-UnavailableSection 'Get-ScheduledTask is unavailable.'
|
|
}
|
|
|
|
# ----------------------------------------------------------------------
|
|
# FULL-ONLY SECTIONS
|
|
# ----------------------------------------------------------------------
|
|
if ($Full) {
|
|
$report.sections.uptime = Invoke-ReportSection 'Uptime' {
|
|
$os = Get-CimInstance Win32_OperatingSystem
|
|
$uptime = (Get-Date) - $os.LastBootUpTime
|
|
|
|
[pscustomobject][ordered]@{
|
|
last_boot = $os.LastBootUpTime
|
|
days = $uptime.Days
|
|
hours = $uptime.Hours
|
|
minutes = $uptime.Minutes
|
|
}
|
|
}
|
|
|
|
$report.sections.startup_programs = Invoke-ReportSection 'Startup programs' {
|
|
Get-CimInstance Win32_StartupCommand |
|
|
Select-Object Name, Command, Location, User |
|
|
Sort-Object Name
|
|
}
|
|
|
|
$report.sections.startup_folders = Invoke-ReportSection 'Startup folders' {
|
|
$folders = @(
|
|
(Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup'),
|
|
(Join-Path $env:ProgramData 'Microsoft\Windows\Start Menu\Programs\Startup')
|
|
)
|
|
|
|
foreach ($folder in $folders) {
|
|
$exists = Test-Path $folder
|
|
$items = @()
|
|
|
|
if ($exists) {
|
|
$items = @(
|
|
Get-ChildItem $folder -Force -ErrorAction SilentlyContinue |
|
|
Select-Object Name, FullName, LastWriteTime
|
|
)
|
|
}
|
|
|
|
[pscustomobject][ordered]@{
|
|
path = $folder
|
|
exists = $exists
|
|
items = $items
|
|
}
|
|
}
|
|
}
|
|
|
|
$report.sections.automatic_services_not_running =
|
|
Invoke-ReportSection 'Automatic services not running' {
|
|
Get-CimInstance Win32_Service |
|
|
Where-Object {
|
|
$_.StartMode -eq 'Auto' -and $_.State -ne 'Running'
|
|
} |
|
|
Select-Object Name, DisplayName, State, StartMode
|
|
}
|
|
|
|
$report.sections.problem_devices = Invoke-ReportSection 'Devices with problems' {
|
|
Get-ProblemDevices
|
|
}
|
|
|
|
if (Get-Command Get-NetFirewallProfile -ErrorAction SilentlyContinue) {
|
|
$report.sections.firewall_profiles = Invoke-ReportSection 'Firewall profiles' {
|
|
Get-NetFirewallProfile -ErrorAction SilentlyContinue |
|
|
Select-Object Name, Enabled, DefaultInboundAction,
|
|
DefaultOutboundAction
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.firewall_profiles =
|
|
New-UnavailableSection 'Get-NetFirewallProfile is unavailable.'
|
|
}
|
|
|
|
if (Get-Command Get-NetAdapter -ErrorAction SilentlyContinue) {
|
|
$report.sections.network_adapters = Invoke-ReportSection 'Network adapters' {
|
|
Get-NetAdapter -ErrorAction SilentlyContinue |
|
|
Select-Object Name, InterfaceDescription, Status,
|
|
LinkSpeed, MacAddress
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.network_adapters =
|
|
New-UnavailableSection 'Get-NetAdapter is unavailable.'
|
|
}
|
|
|
|
$report.sections.ip_configuration = Invoke-ReportSection 'IP configuration' {
|
|
if (Get-Command Get-NetIPConfiguration -ErrorAction SilentlyContinue) {
|
|
Get-NetIPConfiguration -ErrorAction SilentlyContinue |
|
|
ForEach-Object {
|
|
[pscustomobject][ordered]@{
|
|
interface_alias = $_.InterfaceAlias
|
|
interface_index = $_.InterfaceIndex
|
|
ipv4_addresses = @($_.IPv4Address | ForEach-Object { $_.IPAddress })
|
|
ipv6_addresses = @($_.IPv6Address | ForEach-Object { $_.IPAddress })
|
|
ipv4_gateway = @($_.IPv4DefaultGateway | ForEach-Object { $_.NextHop })
|
|
ipv6_gateway = @($_.IPv6DefaultGateway | ForEach-Object { $_.NextHop })
|
|
dns_servers = @($_.DNSServer.ServerAddresses)
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
Invoke-NativeCommand -FilePath 'ipconfig.exe' -Arguments @('/all')
|
|
}
|
|
}
|
|
|
|
if (Get-Command Get-DnsClientServerAddress -ErrorAction SilentlyContinue) {
|
|
$report.sections.dns_servers = Invoke-ReportSection 'DNS servers' {
|
|
Get-DnsClientServerAddress -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.ServerAddresses.Count -gt 0 } |
|
|
Select-Object InterfaceAlias, AddressFamily,
|
|
@{Name='servers';Expression={@($_.ServerAddresses)}}
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.dns_servers =
|
|
New-UnavailableSection 'Get-DnsClientServerAddress is unavailable.'
|
|
}
|
|
|
|
if (Get-Command Get-NetTCPConnection -ErrorAction SilentlyContinue) {
|
|
$report.sections.active_tcp_listeners =
|
|
Invoke-ReportSection 'Active TCP listeners' {
|
|
Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue |
|
|
Select-Object LocalAddress, LocalPort, OwningProcess |
|
|
Sort-Object LocalPort
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.active_tcp_listeners =
|
|
Invoke-ReportSection 'Active TCP listeners' {
|
|
Invoke-NativeCommand -FilePath 'netstat.exe' -Arguments @('-ano')
|
|
}
|
|
}
|
|
|
|
$report.sections.power_plan = Invoke-ReportSection 'Power plan' {
|
|
Invoke-NativeCommand -FilePath 'powercfg.exe' -Arguments @('/getactivescheme')
|
|
}
|
|
|
|
$report.sections.battery = Invoke-ReportSection 'Battery' {
|
|
Get-CimInstance Win32_Battery -ErrorAction SilentlyContinue |
|
|
Select-Object Name, Status, EstimatedChargeRemaining,
|
|
EstimatedRunTime, BatteryStatus
|
|
}
|
|
|
|
$report.sections.top_processes_by_memory =
|
|
Invoke-ReportSection 'Top processes by memory' {
|
|
Get-Process |
|
|
Sort-Object WorkingSet64 -Descending |
|
|
Select-Object -First 30 ProcessName, Id,
|
|
@{Name='memory_mb';Expression={[math]::Round($_.WorkingSet64 / 1MB, 1)}},
|
|
CPU
|
|
}
|
|
|
|
$start = (Get-Date).AddDays(-$EventLogDays)
|
|
|
|
$report.sections.recent_system_errors =
|
|
Invoke-ReportSection ('Recent system errors - last ' + $EventLogDays + ' days') {
|
|
Get-WinEvent -FilterHashtable @{
|
|
LogName = 'System'
|
|
Level = 2
|
|
StartTime = $start
|
|
} -ErrorAction SilentlyContinue |
|
|
Select-Object -First $MaxEventErrors TimeCreated, Id,
|
|
ProviderName, Message
|
|
}
|
|
|
|
$report.sections.recent_application_errors =
|
|
Invoke-ReportSection ('Recent application errors - last ' + $EventLogDays + ' days') {
|
|
Get-WinEvent -FilterHashtable @{
|
|
LogName = 'Application'
|
|
Level = 2
|
|
StartTime = $start
|
|
} -ErrorAction SilentlyContinue |
|
|
Select-Object -First $MaxEventErrors TimeCreated, Id,
|
|
ProviderName, Message
|
|
}
|
|
|
|
if ($isAdmin) {
|
|
$report.sections.component_store =
|
|
Invoke-ReportSection 'Windows component store status' {
|
|
Invoke-NativeCommand -FilePath 'DISM.exe' -Arguments @(
|
|
'/Online',
|
|
'/Cleanup-Image',
|
|
'/CheckHealth'
|
|
)
|
|
}
|
|
|
|
$report.sections.system_file_check =
|
|
Invoke-ReportSection 'System file check - verify only' {
|
|
$tempSfc = Join-Path $env:TEMP (
|
|
'winreport_sfc_' +
|
|
[guid]::NewGuid().ToString('N') +
|
|
'.txt'
|
|
)
|
|
|
|
try {
|
|
cmd.exe /c "sfc.exe /verifyonly > `"$tempSfc`" 2>&1"
|
|
$exitCode = $LASTEXITCODE
|
|
$lines = @()
|
|
|
|
if (Test-Path $tempSfc) {
|
|
$bytes = [System.IO.File]::ReadAllBytes($tempSfc)
|
|
$text = [System.Text.Encoding]::Unicode.GetString($bytes)
|
|
$lines = @(
|
|
$text.Trim([char]0) -split "`r?`n" |
|
|
Where-Object { $_ -ne '' }
|
|
)
|
|
}
|
|
|
|
[pscustomobject][ordered]@{
|
|
executable = 'sfc.exe'
|
|
arguments = @('/verifyonly')
|
|
exit_code = $exitCode
|
|
output = $lines
|
|
}
|
|
}
|
|
finally {
|
|
Remove-Item $tempSfc -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
$report.sections.component_store =
|
|
New-SkippedSection 'DISM /CheckHealth requires an elevated session.'
|
|
|
|
$report.sections.system_file_check =
|
|
New-SkippedSection 'sfc /verifyonly requires an elevated session.'
|
|
}
|
|
|
|
$report.sections.important_directories =
|
|
Invoke-ReportSection 'Important directories' {
|
|
[pscustomobject][ordered]@{
|
|
windows_directory = $env:WINDIR
|
|
system_drive = $env:SystemDrive
|
|
program_files = $env:ProgramFiles
|
|
program_files_x86 = [Environment]::GetEnvironmentVariable('ProgramFiles(x86)')
|
|
program_data = $env:ProgramData
|
|
user_profile = $env:USERPROFILE
|
|
temp = $env:TEMP
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$report.completed_at = (Get-Date).ToString('o')
|
|
|
|
$json = $report | ConvertTo-Json -Depth $JsonDepth
|
|
|
|
# UTF-8 without BOM, matching the original script's encoding choice.
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
|
[System.IO.File]::WriteAllText($reportPath, $json, $utf8NoBom)
|
|
|
|
Write-Host ''
|
|
Write-Host ('Report mode: ' + $mode)
|
|
Write-Host ('Saved JSON report to: ' + $reportPath)
|
|
|
|
# Return the path as the script result, useful for automation.
|
|
$reportPath
|