I present to you a PowerShell script that uses can be useful for monitoring system resources and checking the current configuration. The script saves the data in a table and generates a diagnostic report, which is useful for administering larger environments or analyzing problems.
This script does:
- CPU and RAM monitoring
- Analysis of free disk space.
- Verification of accounts and accesses
- Scanning open network ports.
- Checking security policies
Treść skryptu znajduje się tutaj:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
#DiagnostykaSystemu.ps1 # Plik z raportem $RaportPath = "C:\Dell\RaportSystemu.html" # Tworzenie lokalizacji jeśli nieistnieje if (!(Test-Path -Path (Split-Path $RaportPath))) { New-Item -ItemType Directory -Path (Split-Path $RaportPath) -Force | Out-Null } # Przygoptowanie HTML $RaportContent = " <html> <head> <title>Raport diagnostyczny systemu</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } h1, h2 { color: #2E86C1; } table { border-collapse: collapse; width: 100%; margin-bottom: 20px; } table, th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h1>Raport diagnostyczny systemu</h1> <hr> " # Informacje o CPU function Get-CPUUsage { $cpuLoad = Get-WmiObject -Class Win32_Processor | Select-Object -Property LoadPercentage, Name $Table = "<h2>CPU Usage</h2><table><tr><th>Processor</th><th>Load Percentage</th></tr>" foreach ($cpu in $cpuLoad) { $Table += "<tr><td>$($cpu.Name)</td><td>$($cpu.LoadPercentage)%</td></tr>" } $Table += "</table>" $Table } # Informacje o pamięci RAM function Get-MemoryUsage { $mem = Get-WmiObject -Class Win32_OperatingSystem $TotalMemoryMB = [math]::round($mem.TotalVisibleMemorySize / 1KB, 2) $FreeMemoryMB = [math]::round($mem.FreePhysicalMemory / 1KB, 2) $UsedMemoryMB = [math]::round(($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / 1KB, 2) $UsagePercentage = [math]::round((($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / $mem.TotalVisibleMemorySize) * 100, 2) " <h2>Memory Usage</h2> <p>Total Memory: $TotalMemoryMB MB</p> <p>Used Memory: $UsedMemoryMB MB ($UsagePercentage%)</p> <p>Free Memory: $FreeMemoryMB MB</p> " } # Informacje o przestrzeni dyskowej function Get-DiskSpace { $disks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" $Table = "<h2>Disk Space</h2><table><tr><th>Drive</th><th>Free Space (GB)</th><th>Total Space (GB)</th><th>Usage (%)</th></tr>" foreach ($disk in $disks) { $FreeSpaceGB = [math]::round($disk.FreeSpace / 1GB, 2) $TotalSpaceGB = [math]::round($disk.Size / 1GB, 2) $UsagePercentage = [math]::round((($disk.Size - $disk.FreeSpace) / $disk.Size) * 100, 2) $Table += "<tr><td>$($disk.DeviceID)</td><td>$FreeSpaceGB</td><td>$TotalSpaceGB</td><td>$UsagePercentage%</td></tr>" } $Table += "</table>" $Table } # Informacje o użytkownikach function Get-UserSecurityStatus { $users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount=True" $Table = "<h2>User Account Security</h2><table><tr><th>User</th><th>Status</th><th>Account Disabled</th></tr>" foreach ($user in $users) { $Table += "<tr><td>$($user.Name)</td><td>$($user.Status)</td><td>$($user.Disabled)</td></tr>" } $Table += "</table>" $Table } # Skaner otwartych portów function Get-OpenPorts { $netstat = netstat -an | Select-String "LISTENING" "<h2>Open Ports</h2><pre>$netstat</pre>" } # Polityki bezpieczeństwa function Get-SecurityPolicy { $policy = Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System" " <h2>System Security Policies</h2> <p>Consent Prompt Behavior Admin: $($policy.ConsentPromptBehaviorAdmin)</p> <p>EnableLUA: $($policy.EnableLUA)</p> " } # Generowanie raportu $RaportContent += Get-CPUUsage $RaportContent += Get-MemoryUsage $RaportContent += Get-DiskSpace $RaportContent += Get-UserSecurityStatus $RaportContent += Get-OpenPorts $RaportContent += Get-SecurityPolicy # Zakończenie raportu HTML $RaportContent += "</body></html>" # Zapis raportu do pliku $RaportContent | Out-File -FilePath $RaportPath -Encoding utf8 Write-Output "Raport został zapisany: $RaportPath" start-process $RaportPath |