Monitor Windows Servers and Workstations Using PowerShell: Save Money on APM Tools

Introduction

In IT environments, keeping track of server health is critical to ensuring performance and avoiding downtime. Many organizations use Application Performance Monitoring (APM) tools such as SolarWinds, Datadog, or New Relic to monitor resources like CPU, RAM, and disk space usage.

However, these tools can be costly. If you are looking for a cost-effective alternative, you can use PowerShell scripts to monitor system resources on your Windows Servers or Windows 10 workstations.

This blog provides a PowerShell-based monitoring solution, eliminating the need for expensive software.


Prerequisites

Before running the script, make sure:

  • Your user account has administrative privileges on the target machines.
  • WinRM (Windows Remote Management) is enabled on the servers.
    • Run this on the target machines:powershellCopyEditEnable-PSRemoting -Force
  • PowerShell Execution Policy allows remote scripts.
    • Run this on your machine:powershellCopyEditSet-ExecutionPolicy RemoteSigned -Scope Process

PowerShell Script to Monitor Windows Server Health

This script collects CPU, RAM, and Disk usage from multiple servers and exports the data to a CSV file for reporting.

powershellCopyEdit# List of servers to monitor (Modify as needed)
$Servers = @("Server1", "Server2", "Server3")

# Get current date for report file
$Date = Get-Date -Format "yyyy-MM-dd"

# Output file path
$OutputFile = "C:\ServerReports\ServerHealthReport_$Date.csv"

# Initialize an array to store results
$Results = @()

# Loop through each server
foreach ($Server in $Servers) {
    if (Test-Connection -ComputerName $Server -Count 2 -Quiet) {
        # Get CPU usage
        $CPU = Get-WmiObject Win32_Processor -ComputerName $Server | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average

        # Get RAM usage
        $RAM = Get-WmiObject Win32_OperatingSystem -ComputerName $Server
        $TotalRAM = [math]::Round($RAM.TotalVisibleMemorySize / 1MB, 2)
        $FreeRAM = [math]::Round($RAM.FreePhysicalMemory / 1MB, 2)
        $UsedRAM = $TotalRAM - $FreeRAM
        $RAMUsage = [math]::Round(($UsedRAM / $TotalRAM) * 100, 2)

        # Get Disk usage
        $Disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='C:'"
        $TotalDisk = [math]::Round($Disk.Size / 1GB, 2)
        $FreeDisk = [math]::Round($Disk.FreeSpace / 1GB, 2)
        $UsedDisk = $TotalDisk - $FreeDisk
        $DiskUsage = [math]::Round(($UsedDisk / $TotalDisk) * 100, 2)

        # Store results in an object
        $Result = [PSCustomObject]@{
            ServerName  = $Server
            CPU_Usage   = "$CPU%"
            Total_RAM   = "$TotalRAM GB"
            Used_RAM    = "$UsedRAM GB"
            RAM_Usage   = "$RAMUsage%"
            Total_Disk  = "$TotalDisk GB"
            Used_Disk   = "$UsedDisk GB"
            Disk_Usage  = "$DiskUsage%"
        }
        
        # Add result to array
        $Results += $Result
    } else {
        Write-Host "$Server is unreachable." -ForegroundColor Red
    }
}

# Export results to CSV
$Results | Export-Csv -Path $OutputFile -NoTypeInformation

Write-Host "Server health report generated: $OutputFile" -ForegroundColor Green

How This Script Works

  • Loops through the list of servers
  • Checks connectivity before querying each machine
  • Collects CPU, RAM, and Disk space usage
  • Formats the data for easy reading
  • Exports the results to a CSV file for reporting

Automating the Script

To run this script daily, use Task Scheduler:

  1. Open Task Scheduler and create a new task.
  2. Set the Trigger to run Daily at a specified time.
  3. In the Action tab, select:
    • Program/script: powershell.exe
    • Arguments:powershellCopyEdit-ExecutionPolicy Bypass -File "C:\Scripts\MonitorServers.ps1"
  4. Save the task.

Conclusion

  • Avoid expensive APM software by using this PowerShell-based solution.
  • Schedule the script to run automatically and generate daily reports.
  • Works on Windows Servers and Windows 10 workstations.
  • Customizable: Add more resources (e.g., network usage, event logs) as needed.

๐Ÿš€ Want to improve this? Let me know in the comments!


Stay tuned for more PowerShell automation guides!

๐Ÿš€ Follow for more IT solutions, automation scripts, and best practices!

error: Content is protected !!