SystemInfo_v2
SystemInfo_v2
ps1
# Custom PowerShell script to collect system information for security health checks
# Author: [Your Name]
# Date: April 30, 2025
# Audit logging
$AuditEntry = "Script executed on $env:COMPUTERNAME by $env:USERNAME at $(Get-
Date)"
$AuditEntry | Out-File -FilePath $AuditLog
# HTML report
$global:HtmlContent += "<h3>$Title</h3>"
if ($Data -is [array]) {
foreach ($Item in $Data) {
$global:HtmlContent +=
"<table><tr><th>Property</th><th>Value</th></tr>"
foreach ($Property in $Item.PSObject.Properties) {
$Value = Redact-Data -Value $Property.Value -FieldName
$Property.Name
$Class = if ($Property.Name -eq "Definition Status" -or
$Property.Name -eq "BitLocker Status") { if ($Value -eq "Up to date" -or $Value -eq
"Enabled") { "status-uptodate" } else { "status-outofdate" } } else { "" }
$global:HtmlContent += "<tr><td>$($Property.Name)</td><td
class='$Class'>$Value</td></tr>"
}
$global:HtmlContent += "</table>"
}
}
else {
$global:HtmlContent +=
"<table><tr><th>Property</th><th>Value</th></tr>"
foreach ($Property in $Data.PSObject.Properties) {
$Value = Redact-Data -Value $Property.Value -FieldName
$Property.Name
$Class = if ($Property.Name -eq "Definition Status" -or
$Property.Name -eq "BitLocker Status") { if ($Value -eq "Up to date" -or $Value -eq
"Enabled") { "status-uptodate" } else { "status-outofdate" } } else { "" }
$global:HtmlContent += "<tr><td>$($Property.Name)</td><td
class='$Class'>$Value</td></tr>"
}
$global:HtmlContent += "</table>"
}
# CSV report
if ($CsvFileName -and $Config.Sections -contains ($Title -replace " .*",
"")) {
$CsvPath = "$OutputDir\$CsvFileName"
$Data | Export-Csv -Path $CsvPath -NoTypeInformation -Force
}
}
catch {
Write-Warning "Error in $Title section: $_"
$global:ErrorLog += "Error in ${Title}: $_"
"Error in ${Title}: $_" | Out-File -FilePath $TextReport -Append
}
}
# 1. Windows Version
if ($Config.Sections -contains "OS") {
try {
$OS = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop
$OSInfo = [PSCustomObject]@{
"Windows Version" = $OS.Caption
"Build Number" = $OS.BuildNumber
"Service Pack" = $OS.ServicePackMajorVersion
"Install Date" = $OS.InstallDate
"Last Boot Time" = $OS.LastBootUpTime
}
$global:Summary."Windows Version" = "$($OS.Caption) ($($OS.BuildNumber))"
if ($OS.BuildNumber -lt $Config.Compliance.MinWindowsVersion) {
$global:Summary."Compliance Issues" += "Unsupported Windows version; "
}
Add-ReportSection -Title "Operating System" -Data $OSInfo -CsvFileName
"OS_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve OS info: $_"
$global:ErrorLog += "Failed to retrieve OS info: $_"
}
}
# 2. CPU Information
if ($Config.Sections -contains "CPU") {
try {
$CPU = Get-CimInstance -ClassName Win32_Processor -ErrorAction Stop
$CPUInfo = [PSCustomObject]@{
"Processor" = $CPU.Name
"Cores" = $CPU.NumberOfCores
"Threads" = $CPU.ThreadCount
"Current Clock Speed" = "$($CPU.CurrentClockSpeed) MHz"
"Max Clock Speed" = "$($CPU.MaxClockSpeed) MHz"
}
Add-ReportSection -Title "CPU" -Data $CPUInfo -CsvFileName
"CPU_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve CPU info: $_"
$global:ErrorLog += "Failed to retrieve CPU info: $_"
}
}
# 3. RAM Information
if ($Config.Sections -contains "RAM") {
try {
$OS = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop
$RAM = Get-CimInstance -ClassName Win32_PhysicalMemory -ErrorAction Stop
$TotalRAM = [math]::Round(($OS.TotalVisibleMemorySize / 1MB), 2)
$RAMInfo = [PSCustomObject]@{
"Total RAM" = "$TotalRAM GB"
"Used RAM" = "$([math]::Round(($OS.TotalVisibleMemorySize -
$OS.FreePhysicalMemory) / 1MB, 2)) GB"
"Memory Type" = $RAM[0].SMBIOSMemoryType
"Speed" = if ($RAM[0].Speed) { "$($RAM[0].Speed) MHz" } else
{ "Unknown" }
}
$global:Summary."Total RAM" = "$TotalRAM GB"
Add-ReportSection -Title "RAM" -Data $RAMInfo -CsvFileName
"RAM_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve RAM info: $_"
$global:ErrorLog += "Failed to retrieve RAM info: $_"
}
}
# 4. Disk Information
if ($Config.Sections -contains "Disk") {
try {
$Disks = Get-CimInstance -ClassName Win32_LogicalDisk -ErrorAction Stop |
Where-Object {$_.DriveType -eq 3}
foreach ($Disk in $Disks) {
$FreeSpaceGB = [math]::Round($Disk.FreeSpace / 1GB, 2)
$TotalSpaceGB = [math]::Round($Disk.Size / 1GB, 2)
$DiskInfo = [PSCustomObject]@{
"Drive Letter" = $Disk.DeviceID
"Volume Name" = $Disk.VolumeName
"Total Space" = "$TotalSpaceGB GB"
"Free Space" = "$FreeSpaceGB GB"
"Used Space" = "$([math]::Round(($Disk.Size - $Disk.FreeSpace) /
1GB, 2)) GB"
"Usage Chart" = Get-DiskUsageChart -FreeSpace $FreeSpaceGB -
TotalSpace $TotalSpaceGB
}
if ($Disk.DeviceID -eq "C:") {
$global:Summary."Disk C Free Space" = "$FreeSpaceGB GB"
if ($FreeSpaceGB -lt 20) {
$global:Summary."Compliance Issues" += "Low disk space on C:; "
}
}
$SafeDriveID = $Disk.DeviceID -replace ":", ""
Add-ReportSection -Title "Disk $($Disk.DeviceID)" -Data $DiskInfo -
CsvFileName "Disk_${SafeDriveID}_$env:COMPUTERNAME.csv"
}
}
catch {
Write-Warning "Failed to retrieve disk info: $_"
$global:ErrorLog += "Failed to retrieve disk info: $_"
}
}
if ($IsDefender) {
try {
$DefenderStatus = Get-MpComputerStatus -ErrorAction Stop
$AntivirusInfo."Definition Status" = if
($DefenderStatus.AntivirusSignatureLastUpdated -gt (Get-Date).AddDays(-
$Config.AntivirusUpdateDays)) { "Up to date" } else { "Out of date" }
$AntivirusInfo.Note = "Checked via Defender module. Last
updated: $($DefenderStatus.AntivirusSignatureLastUpdated)"
}
catch {
$AntivirusInfo."Definition Status" = "Unknown (Defender check
failed)"
$AntivirusInfo.Note = "Failed to check Defender status: $_"
}
}
else {
$AntivirusInfo."Definition Status" = if ($Antivirus.productState -
band 0x10000) { "Up to date" } else { "Out of date" }
$AntivirusInfo.Note = "Checked via WMI. May be inaccurate for
third-party products."
}
$AntivirusInfoArray += $AntivirusInfo
}
if ($AntivirusInfoArray) {
$global:Summary."Antivirus Name" = $AntivirusInfoArray[0]."Antivirus
Name"
$global:Summary."Definition Status" =
$AntivirusInfoArray[0]."Definition Status"
if ($Config.Compliance.RequireAntivirus -and
($AntivirusInfoArray[0]."Product State" -ne "Enabled" -or
$AntivirusInfoArray[0]."Definition Status" -ne "Up to date")) {
$global:Summary."Compliance Issues" += "Antivirus not enabled or
outdated; "
}
}
Add-ReportSection -Title "Antivirus Products" -Data $AntivirusInfoArray -
CsvFileName "Antivirus_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve antivirus info: $_"
$global:ErrorLog += "Failed to retrieve antivirus info: $_"
$ErrorInfo = [PSCustomObject]@{
"Antivirus Name" = "Unknown"
"Product State" = "Unknown"
"Definition Status" = "Unknown"
"Note" = "Failed to retrieve antivirus info: $_"
}
Add-ReportSection -Title "Antivirus Products" -Data $ErrorInfo -CsvFileName
"Antivirus_$env:COMPUTERNAME.csv"
}
}
# 8. Network Configuration
if ($Config.Sections -contains "Network") {
try {
$NetConfig = Get-NetIPConfiguration -ErrorAction Stop | Where-Object
{$_.IPv4Address}
$FirewallProfiles = Get-NetFirewallProfile -ErrorAction Stop
$NetInfo = [PSCustomObject]@{
"IPAddress" = Redact-Data -Value ($NetConfig.IPv4Address.IPAddress -
join ", ") -FieldName "IPAddress"
"Subnet Mask" = $NetConfig.IPv4Address.PrefixLength
"Default Gateway" = $NetConfig.IPv4DefaultGateway.NextHop
"DNS Servers" = $NetConfig.DNSServer.ServerAddresses -join ", "
"Firewall Profile" = ($FirewallProfiles | Where-Object {$_.Enabled} |
Select-Object -ExpandProperty Name) -join ", "
}
Add-ReportSection -Title "Network Configuration" -Data $NetInfo -
CsvFileName "Network_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve network info: $_"
$global:ErrorLog += "Failed to retrieve network info: $_"
}
}
# 9. Installed Software
if ($Config.Sections -contains "Software") {
try {
$Software = Get-Package -Provider Programs -ErrorAction Stop | Select-
Object -First 10
$SoftwareInfo = $Software | ForEach-Object {
[PSCustomObject]@{
"Name" = $_.Name
"Version" = $_.Version
"Publisher" = $_.Publisher
}
}
Add-ReportSection -Title "Installed Software" -Data $SoftwareInfo -
CsvFileName "Software_$env:COMPUTERNAME.csv"
}
catch {
Write-Warning "Failed to retrieve software info: $_"
$global:ErrorLog += "Failed to retrieve software info: $_"
}
}
# Finalize reports
$global:HtmlContent += "</body></html>"
$global:HtmlContent | Out-File -FilePath $HtmlReport
$global:Summary | Export-Csv -Path $SummaryCsv -NoTypeInformation -Force
# Sample Config.json
<#
{
"HotfixLimit": 5,
"AntivirusUpdateDays": 2,
"Sections": ["OS", "CPU", "RAM", "Disk", "PhysicalDisk", "Antivirus",
"Hotfixes", "Network", "Software", "Performance", "Battery", "MissingUpdates",
"Firewall", "UserAccounts", "Encryption"],
"RedactFields": ["IPAddress", "UserName"],
"Compliance": {
"MinWindowsVersion": "10.0.19044",
"RequireAntivirus": true,
"RequireBitLocker": true
}
}
#>