Public/Get-AdobeCPUUsageReport.ps1


<#
.SYNOPSIS
    Generates a report on specified process CPU usage on specified computers and sends an email if usage is significant.
 
.DESCRIPTION
    This function connects to specified computers, checks for running specified processes (default Adobe Reader),
    calculates CPU usage, collects data about each instance, exports it to a CSV file, and sends an email with this report attached.
 
.PARAMETER Computers
    The list of computer names to check for the process CPU usage.
 
.PARAMETER ProcessName
    The name of the process to check. Default is "AcroRd32" for Adobe Reader.
 
.PARAMETER CPULimit
    The CPU usage threshold as a percentage. Only processes exceeding this threshold will be reported.
 
.PARAMETER ExportToCSV
    Specifies whether to export the results to a CSV file.
 
.EXAMPLE
    Get-AdobeCPUUsageReport -Computers "Server1", "Server2" -ProcessName "AcroRd32" -CPULimit 20 -ExportToCSV
    This example checks the Adobe Reader CPU usage on Server1 and Server2, exports the results to a CSV file, and emails a report if usage exceeds 20%.
 
.NOTES
    Requires administrative permissions on the target computers for process monitoring and WMI access.
#>


function Get-AdobeCPUUsageReport {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$Computers,

        [Parameter(Mandatory = $false)]
        [string]$ProcessName = "AcroRd32",

        [Parameter(Mandatory = $false)]
        [int]$CPULimit = 20,

        [Parameter(Mandatory = $false)]
        [switch]$ExportToCSV
    )

    process {
        $hour = Get-Date -Format "dd_MM_yy_HH"
        $csvPath = "C:\temp\biovia_$hour.csv"

        $results = Invoke-Command -ComputerName $Computers -ScriptBlock {
            param($ProcessName, $CPULimit)
            $procs = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue -IncludeUserName
            foreach ($proc in $procs) {
                Start-Sleep -Seconds 5
                $procRefreshed = Get-Process -Id $proc.Id -ErrorAction SilentlyContinue
                if ($procRefreshed) {
                    $cpuFinal = $procRefreshed.CPU
                    $cpuCores = (Get-WmiObject Win32_ComputerSystem).NumberOfLogicalProcessors
                    $CPUAveragePerformance = [Math]::Round((($cpuFinal - $proc.CPU) / (5 * $cpuCores)) * 100)

                    if ($CPUAveragePerformance -gt $CPULimit) {
                        $userName = $proc.UserName.Split('\')[1]
                        $memoryUsage = [math]::Round($proc.WS / 1MB, 1)

                        [PSCustomObject]@{
                            Date        = Get-Date -Format "dd/MM/yy HH:mm:ss"
                            ComputerName = $env:COMPUTERNAME
                            UserName     = $userName
                            ProcessName  = $ProcessName
                            CPU          = $CPUAveragePerformance
                            MemoryMB     = $memoryUsage
                        }
                    }
                }
            }
        } -ArgumentList $ProcessName, $CPULimit

        if ($results) {
            if ($ExportToCSV) {
                $results | Export-Csv -Path $csvPath -NoTypeInformation -Append
                Write-Host "Results have been exported to $csvPath"
            } else {
                $results | Format-Table -AutoSize
            }
        }
    }
}