diff --git a/Codex-Scripts/Get-SystemHealthReport.ps1 b/Codex-Scripts/Get-SystemHealthReport.ps1 new file mode 100644 index 0000000..116a6fa --- /dev/null +++ b/Codex-Scripts/Get-SystemHealthReport.ps1 @@ -0,0 +1,68 @@ +<#! +.SYNOPSIS + Generates a point-in-time report that summarizes CPU, memory, process, and disk usage. +.DESCRIPTION + Get-SystemHealthReport gathers system metrics that are typically reviewed during health checks. + The script can optionally persist the results to disk in JSON format for later analysis. +.PARAMETER TopProcessCount + Indicates how many of the busiest processes (based on CPU time) to include in the report. +.PARAMETER OutputPath + When provided, saves the health report as a JSON file to the specified path. +.EXAMPLE + .\\Get-SystemHealthReport.ps1 -TopProcessCount 10 +.EXAMPLE + .\\Get-SystemHealthReport.ps1 -OutputPath C:\\Temp\\system-health.json +.NOTES + Author: ChatGPT Automation +#> +[CmdletBinding()] +param( + [Parameter()] + [ValidateRange(1,50)] + [int]$TopProcessCount = 5, + + [Parameter()] + [string]$OutputPath +) + +function Get-DiskUsage { + [CmdletBinding()] + param() + Get-PSDrive -PSProvider 'FileSystem' | + Where-Object { $_.Free -ne $null } | + Select-Object Name, @{n='UsedGB';e={[math]::Round(($_.Used/1GB),2)}}, @{n='FreeGB';e={[math]::Round(($_.Free/1GB),2)}}, + @{n='FreePercent';e={[math]::Round(($_.Free/($_.Used+$_.Free))*100,2)}} +} + +function Get-TopProcesses { + [CmdletBinding()] + param( + [int]$Count + ) + Get-Process | + Sort-Object -Property CPU -Descending | + Select-Object -First $Count -Property ProcessName, CPU, Id, PM, WS +} + +$report = [pscustomobject]@{ + GeneratedOn = Get-Date + ComputerName = $env:COMPUTERNAME + Uptime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime + CpuLoadPercent = (Get-Counter '\\Processor(_Total)\\% Processor Time').CounterSamples.CookedValue + MemoryStatus = Get-CimInstance -ClassName Win32_OperatingSystem | + Select-Object @{n='TotalVisibleMemoryGB';e={[math]::Round($_.TotalVisibleMemorySize/1MB,2)}}, + @{n='FreePhysicalMemoryGB';e={[math]::Round($_.FreePhysicalMemory/1MB,2)}} + TopProcesses = Get-TopProcesses -Count $TopProcessCount + DiskUsage = Get-DiskUsage +} + +if ($OutputPath) { + $OutputDirectory = Split-Path -Path $OutputPath -Parent + if ($OutputDirectory -and -not (Test-Path -Path $OutputDirectory)) { + New-Item -Path $OutputDirectory -ItemType Directory -Force | Out-Null + } + $report | ConvertTo-Json -Depth 4 | Set-Content -Path $OutputPath + Write-Verbose "Health report saved to $OutputPath" +} else { + $report +} diff --git a/Codex-Scripts/Monitor-EventLogs.ps1 b/Codex-Scripts/Monitor-EventLogs.ps1 new file mode 100644 index 0000000..dcf4944 --- /dev/null +++ b/Codex-Scripts/Monitor-EventLogs.ps1 @@ -0,0 +1,61 @@ +<#! +.SYNOPSIS + Retrieves recent events from Windows event logs with filtering options. +.DESCRIPTION + Monitor-EventLogs queries one or more Windows event logs and emits the newest entries + within a user-specified time window. Use the parameters to scope the level and ID filters. +.PARAMETER LogName + One or more log names to query. Defaults to the Application and System logs. +.PARAMETER HoursBack + Limits the event search to entries newer than the provided number of hours. +.PARAMETER Level + Filters by event severity level. Accepts Information, Warning, or Error. +.PARAMETER Id + Optionally filters on specific event IDs. +.EXAMPLE + .\\Monitor-EventLogs.ps1 -LogName Security -HoursBack 1 -Level Error +.EXAMPLE + .\\Monitor-EventLogs.ps1 -Id 1000,1001 +.NOTES + Author: ChatGPT Automation +#> +[CmdletBinding()] +param( + [Parameter()] + [ValidateNotNullOrEmpty()] + [string[]]$LogName = @('Application','System'), + + [Parameter()] + [ValidateRange(1,168)] + [int]$HoursBack = 24, + + [Parameter()] + [ValidateSet('Information','Warning','Error')] + [string[]]$Level, + + [Parameter()] + [int[]]$Id +) + +$filter = @{ + LogName = $LogName + StartTime = (Get-Date).AddHours(-1 * $HoursBack) +} + +if ($Level) { + $filter['Level'] = foreach ($lvl in $Level) { + switch ($lvl) { + 'Information' { 4 } + 'Warning' { 3 } + 'Error' { 2 } + } + } +} + +if ($Id) { + $filter['Id'] = $Id +} + +Get-WinEvent -FilterHashtable $filter | + Sort-Object TimeCreated | + Select-Object TimeCreated, LogName, LevelDisplayName, Id, ProviderName, Message diff --git a/Codex-Scripts/Test-NetworkConnectivity.ps1 b/Codex-Scripts/Test-NetworkConnectivity.ps1 new file mode 100644 index 0000000..4d953a5 --- /dev/null +++ b/Codex-Scripts/Test-NetworkConnectivity.ps1 @@ -0,0 +1,71 @@ +<#! +.SYNOPSIS + Performs a lightweight network diagnostic test against one or more hosts. +.DESCRIPTION + Test-NetworkConnectivity validates DNS resolution, ICMP reachability, and TCP port checks for + each host that you specify. The cmdlet produces a summary object with pass/fail indicators. +.PARAMETER ComputerName + One or more hostnames or IP addresses to test. +.PARAMETER TcpPort + Optional list of TCP ports (per host) to validate via Test-NetConnection. +.PARAMETER ThrottleLimit + Controls how many hosts are tested concurrently. +.EXAMPLE + .\\Test-NetworkConnectivity.ps1 -ComputerName fileserver,print01 +.EXAMPLE + .\\Test-NetworkConnectivity.ps1 -ComputerName contoso.com -TcpPort 80,443 +.NOTES + Author: ChatGPT Automation +#> +[CmdletBinding()] +param( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [string[]]$ComputerName, + + [Parameter()] + [ValidateRange(1,65535)] + [int[]]$TcpPort, + + [Parameter()] + [ValidateRange(1,32)] + [int]$ThrottleLimit = 4 +) + +$scriptBlock = { + param($HostName, $Ports) + $result = [pscustomobject]@{ + ComputerName = $HostName + DnsResolved = $false + PingSucceeded= $false + TcpResults = @() + ErrorMessage = $null + } + + try { + $null = Resolve-DnsName -Name $HostName -ErrorAction Stop + $result.DnsResolved = $true + + $ping = Test-Connection -ComputerName $HostName -Count 2 -Quiet -ErrorAction Stop + $result.PingSucceeded = [bool]$ping + + if ($Ports) { + $tcpTests = foreach ($port in $Ports) { + $tcpResult = Test-NetConnection -ComputerName $HostName -Port $port -WarningAction SilentlyContinue + [pscustomobject]@{ + Port = $port + TcpSucceeded= [bool]$tcpResult.TcpTestSucceeded + LatencyMS = $tcpResult.PingReplyDetails.RoundtripTime + } + } + $result.TcpResults = $tcpTests + } + } catch { + $result.ErrorMessage = $_.Exception.Message + } + + return $result +} + +$ComputerName | + ForEach-Object -Parallel $scriptBlock -ThrottleLimit $ThrottleLimit -ArgumentList $TcpPort