Forum Discussion

suhailmagray's avatar
suhailmagray
Copper Contributor
Jun 08, 2022

SERVER MONITORING

Hi Brothers/Sisters,

 

Greetings of the Day

 

Hope everyone is doing well

 

I found this community after long & hectic browsing and it looks like someone will help me out here. Actually, I recently joined a new job and I have to monitor multiple servers for the parameters below:

CPU Usage

Memory (RAM) Usage

Free Disk Space

Free Disk Space (%age)

 

As of now, I am browsing through each server one by one and maintaining the above data in an excel manually, which takes a lot of time and is also hectic.

Can you help me please develop a Powershell script for this task? It is a request if possible for you.

 

So far after Google R&D I was able to develop the below script and it is working fine but a few things are missing in it.

------------------------------

$OldReports = (Get-Date).AddDays(-30)
Get-ChildItem E:\DiskUsage\DiskUsageReports\
Where-Object { $_.LastWriteTime -le $OldReports} | `
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
$LogDate = get-date -f dd_MM_yyyy
$File = Get-Content -Path E:\DiskUsage\ServersList\ServerList.txt
$DiskReport = ForEach ($Servernames in ($File))

{Get-WmiObject win32_logicaldisk <#-Credential $RunAccount#> `
-ComputerName $Servernames -Filter "Drivetype=3" `
-ErrorAction SilentlyContinue
}

$DiskReport |

Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |

Export-Csv -path "E:\DiskUsage\DiskUsageReports\Monitoring_$logDate.csv" –NoTypeInformation

-----------------------------------------------------

The above script exports the data in a CSV file like in the screenshot

 

MISSING THINGS3

1. Memory (RAM) & CPU Usage is missing.

2. Every Time I execute the script it creates a separate CSV file BUT IT SHOULD CREATE A SINGLE CSV WITH MULTIPLE WORKSHEETS (DATEWISE)

3. I need an alert system that will alert me if the Disk Space reaches a certain level and also the same with CPU usage.

 

Thanks, Brothers/Sisters

Hope someone will come forward and help me as I have to complete this Script Task by Saturday

1 Reply

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    suhailmagray 

     

    Hi.

     

    Point 1

    PowerShell isn't designed to work as a monitoring system (such as System Center Configuration Manager, Nagios, etc.) and won't adequately satisfy point 1. This is particularly true for memory and CPU where you really need to be taking data samples at small intervals, not a point-in-time snapshot from PowerShell.

     

    PowerShell can provide you with point-in-time memory and CPU statistics but not the kind of ongoing monitoring you're referring to. The best information for memory and CPU utilisation will come from the Get-Counter commandlet, which is more useful and better performing than WMI. Still, neither are a good substitution for a proper monitoring system.

     

    Get-Counter (Microsoft.PowerShell.Diagnostics) - PowerShell | Microsoft Docs

     

    You might be tempted to create scheduled tasks to automate the running of the reporting script. This would be best suited to monitoring disk space (which generally doesn't rapidly change), somewhat suited to monitoring memory, but completely useless for monitoring CPU since it's purely demand-driven. In short, scheduled tasks just aren't worth the effort.

     

    You might be better off looking at an event-driven model like performance alerts (linked below), though this doesn't scale very well as you look to monitor more and more servers. It's better suited to very small environments.

     

    Create performance counter alert and send email - Windows Server | Microsoft Docs

     

    Point 2

    Worksheets are from Excel (XLSX files). CSV files are nothing like XLSX files at all and do not support worksheets.

     

    There are a number of third-party PowerShell modules available (a sample list can be found below) that work with Excel files, and worksheets specifically, but as they are not part of the Windows operating system, you'd have to invest time into learning how to best make use of them.

     

    One command I have seen other people use before is Export-Excel, which comes from the module (listed below) named "ImportExcel" (ironic choice of name.) I don't personally use third-party modules but there are other people in this community who do and may be able to explain how to make the best use of the Export-Excel commandlet.

     

    Module nameAuthorVersion
    BitTitan.Runbooks.ExcelBitTitan0.1.6
    BitTitan.Runbooks.Excel.BetaBitTitan0.1.6
    ExcelBDDExcelBDD Team0.5.1
    ExcelCmdletsCData Software Inc.21.0.8137.1
    ExcelimoPrzemyslaw Klys0.0.4
    ExcelOnlineCmdletsCData Software Inc.21.0.8137.1
    ExcelPSLibPhilip Tomson0.6.9
    ExcelServicesCmdletsCData Software Inc.21.0.8137.1
    ExcelToolsBrent Denny0.1.0
    IIE-ExcelDavid Rikkoert0.1.0
    ImportExcelDouglas Finke7.5.3
    ProductivityTools.ImportExcelToSQLPawel Wujczyk0.0.5
    PSExcelWarren Frame1.0.2
    PSWriteExcelPrzemyslaw Klys0.1.14
    Read-ExcelFilePascal Rimark2.1

     

    Point 3

    Same comments as point 1. PowerShell just isn't the right tool for this job.

     

    Summary

    What you've achieved so far with disk space monitoring is about as much as you can achieve through PowerShell - unless you're prepared to spend months writing a system from the ground up (which you can do since PowerShell natively consumes .NET), but it's highly unlikely you'd be able to adequately address points 1 and 3 by Saturday.

     

    Cheers,

    Lain

Resources