PowerShell Windows XenDesktop

Get running processes including CPU and memory usage

One of my customers needed a PowerShell script to get all running processes with their corresponding CPU load. This was script should be triggered by their monitoring system if the system total CPU usage exceeds a configured threshold.

They’re running a mid-sized Citrix XenDesktop farm with Server-OS (formerly known as XenApp a.k.a. terminal services) and sometimes applications are using a high CPU amount.

This is the small script I wrote for them:

#####################################################################
##
## (C) 2015 Michael Miklis (michaelmiklis.de)
##
##
## Filename:      Get-Tasks.ps1
##
## Version:       1.0
##
## Release:       Final
##
## Requirements:  -none-
##
## Description:   PowerShell Tasklist with CPU usage and memory
##                usage
##
## This script is provided 'AS-IS'.  The author does not provide
## any guarantee or warranty, stated or implied.  Use at your own
## risk. You are free to reproduce, copy & modify the code, but
## please give the author credit.
##
####################################################################

<#
.SYNOPSIS
Lists all running task including cpu and memory usage

.DESCRIPTION
The Get-Tasks function uses Windows Management Instrumentation (WMI) to retrieve process Name, ProcessId, SessionId,
VirtualSizeMB, Handles, Owner, PercentProcessorTime and ThreadCount

.PARAMETER computerName
Computername or IP Adress of the computer to query

.PARAMETER credential
Credentials to query computer as System.Management.Automation.PSCredential

.EXAMPLE
Get-Tasks
Get-Tasks -computerName "server.domain.com" -credential $credential

.NOTES
You need to run this CMDlet with elevated permissions
#>

function Get-Tasks {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [string]$computername,

        [parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [System.Management.Automation.PSCredential]$credential      
    )

    PROCESS {
        $colProcs = Get-wmiobject win32_process -computername $computername  -Credential $credential | select *,@{Name=”Owner”;Expression={($_.GetOwner()).User}}
        $colPerfs = Get-wmiobject win32_perfformatteddata_perfproc_process -computername $computername  -Credential $credential 
        $colTasklist = @()

        foreach ($proc in $colProcs) {
            $process = New-Object System.Object

            $perf = $colPerfs | Where-Object { $_.IDProcess -eq $proc.ProcessId }

            $process | Add-Member -type NoteProperty -name "Name" -value $proc.Name       
            $process | Add-Member -type NoteProperty -name "ProcessId" -value $proc.ProcessId
            $process | Add-Member -type NoteProperty -name "SessionId" -value $proc.SessionId
            $process | Add-Member -type NoteProperty -name "VirtualSizeMB" -value ([math]::Round(($proc.VirtualSize / 1024 /1024), 2))
            $process | Add-Member -type NoteProperty -name "Handles" -value $proc.Handles
            $process | Add-Member -type NoteProperty -name "Owner" -value $proc.Owner
            $process | Add-Member -type NoteProperty -name "PercentProcessorTime" -value $perf.PercentProcessorTime
            $process | Add-Member -type NoteProperty -name "ThreadCount" -value $perf.ThreadCount

            $colTasklist += $process
        }

        $colTasklist | Sort-Object PercentProcessorTime -Desc

        return $colTasklist
    }
}
You may also like
Using Windows certificate store in Mozilla Firefox
Disable IPv6 in Windows for a specific network connection only
Script-Gallery: Fix-Printer.ps1

Leave Your Comment

Your Comment*

Your Name*
Your Webpage