Advertisement
kohijones

GetSysInfo.ps1

Feb 17th, 2022
1,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# 
  2.     .NOTES
  3.      Created on:    8/11/2020 7:29 PM
  4.      Created by:    kohijones
  5.      Organization:  xxx
  6.      Filename:      GetSysInfo.ps1
  7.     .DESCRIPTION
  8.         Get various systems information from local computer, and display in a log file.
  9. #>
  10.  
  11. [cmdletbinding(DefaultParameterSetName = "object")]
  12. param (
  13.    
  14.     [string]$Computername = $env:computername,
  15.     [ValidateScript({
  16.             $_ -ge 1
  17.         })]
  18.     [int]$Hours = 24,
  19.     [string]$LogPath = "$($env:TEMP)",
  20.     [string]$LogName = "SystemReport_$((Get-Date).ToString('MMddyyyyHHmmss')).txt"
  21. )
  22.  
  23. Import-Module CimCmdlets
  24.  
  25. $SysInfoProps = [System.Collections.Generic.List[PSObject]]@()
  26.  
  27. $versionMinimum = [Version]'5.1.99999.999'
  28.  
  29. if ($versionMinimum -lt $PSVersionTable.PSVersion) {
  30.     "This script cannot be run on PS v6 or greater."
  31.     "Running PowerShell $($PSVersionTable.PSVersion)."
  32.    
  33.     break
  34. }
  35.  
  36. if ($computername -eq $env:computername) {
  37.     $OK = $True
  38. }
  39. elseif (($computername -ne $env:computername) -and (Test-Connection -ComputerName $computername -Quiet -Count 2)) {
  40.     $OK = $True
  41. }
  42.  
  43. if ($OK) {
  44.     try {
  45.         $os = Get-WmiObject Win32_operatingSystem -ComputerName $computername -ErrorAction Stop
  46.         $wmi = $True
  47.     }
  48.     catch {
  49.         Write-Warning "WMI failed to connect to $($computername.ToUpper())"
  50.         break
  51.     }
  52.    
  53.     if ($wmi) {
  54.         New-Item -Path "$($Logpath)\$($LogName)" -ItemType "file" -Force | Out-Null
  55.        
  56.         Write-Host "Preparing report for $($os.CSname)" -ForegroundColor Cyan
  57.        
  58.         # OS Summary
  59.         Write-Host "...Operating System" -ForegroundColor Cyan
  60.         $osdata = ($os | Select-Object @{
  61.                 Name = "Computername"; Expression = {
  62.                     $_.CSName
  63.                 }
  64.             },
  65.                                        @{
  66.                 Name = "OS"; Expression = {
  67.                     $_.Caption
  68.                 }
  69.             },
  70.                                        @{
  71.                 Name = "ServicePack"; Expression = {
  72.                     $_.CSDVersion
  73.                 }
  74.             },
  75.                                        free*memory, totalv*, NumberOfProcesses,
  76.                                        @{
  77.                 Name = "LastBoot"; Expression = {
  78.                     $_.ConvertToDateTime($_.LastBootupTime)
  79.                 }
  80.             },
  81.                                        @{
  82.                 Name = "Uptime"; Expression = {
  83.                     (Get-Date) - ($_.ConvertToDateTime($_.LastBootupTime))
  84.                 }
  85.             } | Out-String).Trim()
  86.        
  87.         # Computer system
  88.         Write-Host "...Computer System" -ForegroundColor Cyan
  89.         $cs = Get-WmiObject -Class Win32_Computersystem -ComputerName $computername
  90.         $csdata = ($cs | Select-Object Status, Manufacturer, Model, SystemType, Number* | Out-String)
  91.        
  92.         # Services
  93.         Write-Host "...Services" -ForegroundColor Cyan
  94.         $wmiservices = Get-WmiObject -Class Win32_Service -ComputerName $computername
  95.         $services = $wmiservices | Group-Object State -AsHashTable
  96.        
  97.         # Get services set to auto start that are not running
  98.         $failedAutoStart = $wmiservices | Where-Object {
  99.             ($_.startmode -eq "Auto") -AND ($_.state -ne "Running")
  100.         } |
  101.         Select-Object Name, Displayname, StartMode, State | Format-Table -AutoSize | Out-String
  102.        
  103.         # Disk Utilization
  104.         Write-Host "...Logical Disks" -ForegroundColor Cyan
  105.         $disks = Get-WmiObject -Class Win32_logicaldisk -Filter "Drivetype=3" -ComputerName $computername
  106.         $diskData = ($disks | Select-Object DeviceID,
  107.                                             @{
  108.                 Name = "SizeGB"; Expression = {
  109.                     $_.size / 1GB -as [int]
  110.                 }
  111.             },
  112.                                             @{
  113.                 Name = "FreeGB"; Expression = {
  114.                     "{0:N2}" -f ($_.Freespace / 1GB)
  115.                 }
  116.             },
  117.                                             @{
  118.                 Name = "PercentFree"; Expression = {
  119.                     "{0:P2}" -f ($_.Freespace / $_.Size)
  120.                 }
  121.             } | Format-Table -AutoSize | Out-String)
  122.        
  123.         # CPU Utilization
  124.         Write-Host "...CPU" -ForegroundColor Cyan
  125.         $totalRam = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
  126.         $cpuTime = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
  127.         $availMem = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
  128.         $cpuUtil = ($cpuTime.ToString("#,0.000") + '%, Avail. Mem.: ' + $availMem.ToString("N0") + 'MB (' + (104857600 * $availMem / $totalRam).ToString("#,0.0") + '%)' | Out-String) + [environment]::NewLine
  129.        
  130.         # Top 10 CPU Utilization
  131.         Write-Host "...CPU TOP 10" -ForegroundColor Cyan
  132.         $cputop10 = (Get-WmiObject -ComputerName $Computername Win32_PerfFormattedData_PerfProc_Process | Sort-Object PercentProcessorTime -desc | Select-Object Name, PercentProcessorTime | Select-Object -First 10 | Format-Table -auto | Out-String) + [environment]::NewLine
  133.        
  134.         # Memory Utilization
  135.         Write-Host "...Memory" -ForegroundColor Cyan
  136.         $os = Get-CimInstance Win32_OperatingSystem
  137.         $pctFree = [math]::Round(($os.FreePhysicalMemory / $os.TotalVisibleMemorySize) * 100, 2)
  138.         # Top 10 Memory Utilization
  139.         Write-Host "...Memory TOP 10" -ForegroundColor Cyan
  140.         $memTop10 = (Get-WmiObject -ComputerName $Computername Win32_Process | Sort-Object WorkingSetSize -Descending | Select-Object Name, @{
  141.                 n = "Private Memory(mb)"; Expression = {
  142.                     [math]::round(($_.WorkingSetSize / 1mb), 2)
  143.                 }
  144.             } | Select-Object -First 10 | Format-Table -AutoSize | Out-String) + [environment]::NewLine
  145.        
  146.         if ($pctFree -ge 45) {
  147.             $Status = "OK"
  148.         }
  149.         elseif ($pctFree -ge 15) {
  150.             $Status = "Warning"
  151.         }
  152.         else {
  153.             $Status = "Critical"
  154.         }
  155.        
  156.         $memUtil = ($os | Select-Object @{
  157.                 Name = "Status"; Expression = {
  158.                     $Status
  159.                 }
  160.             },
  161.                                         @{
  162.                 Name = "PctFree"; Expression = {
  163.                     $pctFree
  164.                 }
  165.             },
  166.                                         @{
  167.                 Name = "FreeGB"; Expression = {
  168.                     [math]::Round($_.FreePhysicalMemory / 1mb, 2)
  169.                 }
  170.             },
  171.                                         @{
  172.                 Name = "TotalGB"; Expression = {
  173.                     [int]($_.TotalVisibleMemorySize / 1mb)
  174.                 }
  175.             } | Out-String)
  176.        
  177.         # Running Applications
  178.         Write-Host "...Running Applications" -ForegroundColor Cyan
  179.         $runningApps = (Get-Process | Group-Object -Property ProcessName |
  180.             Format-Table Name, @{
  181.                 n = 'Mem(KB)'; e = {
  182.                     '{0:N0}' -f (($_.Group | Measure-Object WorkingSet -Sum).Sum / 1KB)
  183.                 }; a = 'right'
  184.             } -AutoSize | Out-String)
  185.        
  186.         # NetworkAdapters
  187.         Write-Host "...Network Adapters" -ForegroundColor Cyan
  188.         # Get NICS that have a MAC address only
  189.         $nics = Get-WmiObject -Class Win32_NetworkAdapter -Filter "MACAddress Like '%'" -ComputerName $Computername
  190.         $nicdata = $nics | ForEach-Object {
  191.             $tempHash = @{
  192.                 Name = $_.Name; DeviceID = $_.DeviceID; AdapterType = $_.AdapterType; MACAddress = $_.MACAddress
  193.             }
  194.             # Get related configuation information
  195.             $config = $_.GetRelated() | Where-Object {
  196.                 $_.__CLASS -eq "Win32_NetworkadapterConfiguration"
  197.             }
  198.             # Add to temporary hash
  199.             $tempHash.Add("IPAddress", $config.IPAddress)
  200.             $tempHash.Add("IPSubnet", $config.IPSubnet)
  201.             $tempHash.Add("DefaultGateway", $config.DefaultIPGateway)
  202.             $tempHash.Add("DHCP", $config.DHCPEnabled)
  203.             # Convert lease information if found
  204.             if ($config.DHCPEnabled -AND $config.DHCPLeaseObtained) {
  205.                 $tempHash.Add("DHCPLeaseExpires", ($config.ConvertToDateTime($config.DHCPLeaseExpires)))
  206.                 $tempHash.Add("DHCPLeaseObtained", ($config.ConvertToDateTime($config.DHCPLeaseObtained)))
  207.                 $tempHash.Add("DHCPServer", $config.DHCPServer)
  208.             }
  209.             New-Object -TypeName PSObject -Property $tempHash
  210.         }
  211.         $nicdata = ($nicdata | Format-List | Out-String)
  212.        
  213.         $last = (Get-Date).AddHours(-$Hours)
  214.         # System Log
  215.         Write-Host "...System Event Log Error/Warning since $last" -ForegroundColor Cyan
  216.         $syslog = Get-WinEvent -ComputerName $Computername -FilterHashtable @{
  217.             logname = "system"; level = 2, 3; starttime = $last
  218.         } -ErrorAction 0
  219.         $syslogdata = ($syslog | Select-Object id, timecreated, message | Format-List | Out-String)
  220.         # Application Log
  221.         Write-Host "...Application Event Log Error/Warning since $last" -ForegroundColor Cyan
  222.         $applog = Get-WinEvent -ComputerName $Computername -FilterHashtable @{
  223.             logname = "application"; level = 2, 3; starttime = $last
  224.         } -ErrorAction 0
  225.         $applogdata = ($applog | Select-Object id, timecreated, message | Format-List | Out-String) + [environment]::NewLine
  226.     }
  227.    
  228.     $SysInfoProps.Add("System Summary" + [environment]::NewLine)
  229.     $SysInfoProps.Add($osdata)
  230.     $SysInfoProps.Add($csdata)
  231.     $SysInfoProps.Add("Disk Utilization")
  232.     $SysInfoProps.Add($diskdata)
  233.     $SysInfoProps.Add("CPU Utilization" + [environment]::NewLine)
  234.     $SysInfoProps.Add($cpuUtil)
  235.     $SysInfoProps.Add("CPU Top 10 Utilization")
  236.     $SysInfoProps.Add($cputop10)
  237.     $SysInfoProps.Add("Memory Utilization")
  238.     $SysInfoProps.Add($memUtil)
  239.     $SysInfoProps.Add("Memory Top 10 Utilization")
  240.     $SysInfoProps.Add($memTop10)
  241.     $SysInfoProps.Add("Running Applications")
  242.     $SysInfoProps.Add($runningApps)
  243.     $SysInfoProps.Add("Network Adapters" + [environment]::NewLine)
  244.     $SysInfoProps.Add($nicdata)
  245.     $SysInfoProps.Add("Failed Autostart Services")
  246.     $SysInfoProps.Add($failedAutoStart)
  247.     $SysInfoProps.Add("System Event Log Summary")
  248.     $SysInfoProps.Add($syslogdata)
  249.     $SysInfoProps.Add("Application Event Log Summary" + [environment]::NewLine)
  250.     $SysInfoProps.Add($applogdata)
  251.     $SysInfoProps.Add("Services")
  252.     $SysInfoProps.Add(($services.keys | ForEach-Object {
  253.                 $services.$_ | Select-Object Name, Displayname, StartMode, State
  254.             } | Format-List | Out-String).TrimEnd())
  255.     $SysInfoProps.Add(([environment]::NewLine))
  256.     $SysInfoProps.Add(([environment]::NewLine))
  257.     $SysInfoProps.Add(("Report run {0} by {1}\{2}" -f (Get-Date), $env:USERDOMAIN, $env:USERNAME))
  258.    
  259.     foreach ($obj in $SysInfoProps) {
  260.         Add-Content -Path "$($LogPath)\$($LogName)" -Value $obj -NoNewline
  261.     }
  262.     & notepad.exe "$($LogPath)\$($LogName)"
  263. }
  264. else {
  265.     Write-Warning "Failed to ping $computername"
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement