//
you're reading...
Tools

Monitor processes per user using WMI in VB.Net

I had problems trying to create a tool to log Internet Explorer memory usage to an output log file in a Citrix Environment, without pulling out all Internet Explorer processes for all users logged on. I then found that WMI could help with this to identify the user and match with the process Id to log in each users seperate log file. Below is the code to do this, which I placed within a timer:

To get this working, in Visual Studio you need to add a reference to your project (project properties > add reference…) to System.Maintenance

Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Imports System.Management

Dim user As String = Environment.UserName
Dim machine As String = Environment.MachineName
Dim ReturnObject As Management.ManagementObjectCollection
Dim SearchObject As Management.font color=”#51a8a8″>ManagementObjectSearcher
Dim mObject As Management.font color=”#51a8a8″>ManagementObject
Dim ProcessId, TotalRamUsage As Integer
Dim LogFile As String = “C:\logs\” + user + ” iexplore memory.log”
Dim Logwriter As System.IO.StreamWriter

Try
SearchObject = New Management.ManagementObjectSearcher(“Select * from Win32_Process WHERE Name = ‘iexplore.exe'”)
ReturnObject = SearchObject.Get
For Each mObject In ReturnObject
Dim arOwner(2) As String
mObject.InvokeMethod(“GetOwner”, arOwner)
strProcessId = Convert.ToInt32(mObject.Properties(“ProcessId”).Value.ToString)
If arOwner(0) = user Then
Dim Process As Process = Process.GetProcessById(ProcessId)
TotalRamUsage = TotalRamUsage + Process.WorkingSet64 / 1024 / 1024

If process.WorkingSet64 / 1024 > 100000 Then
If System.IO.File.Exists(LogFile) = True Then
Logwriter = File.AppendText(LogFile)
Logwriter.WriteLine(Date.Now.ToString + ” | User: “ + user + ” | Machine: “ + machine + “| WARNING: IE instance “ + ProcessId.ToString + ” is using more than 100 MB of RAM”)
Logwriter.Close()
‘Can try to kill the process if we want, uncomment to do so – needs modifying to use WMI terminate
‘Try
‘ Process.Kill()
‘Catch ex As Exception
‘ MessageBox.Show(ex.Message)
‘End Try
Else
System.IO.File.Create(LogFile).Dispose()
Logwriter = File.AppendText(LogFile)
Logwriter.WriteLine(Date.Now.ToString + ” | User: “ + user + ” | Machine: “ + machine + ” | WARNING: IE instance “ + ProcessId.ToString + ” is using more than 100 MB of RAM”)
Logwriter.Close()
End If
End If
End If
Next

Logwriter = File.AppendText(LogFile)
Logwriter.WriteLine(Date.Now.ToString + ” | User: “ + user + ” |      Machine: “ + machine + ” | Total RAM usage by IE is “ + TotalRamUsage.ToString + ” MB”)
Logwriter.WriteLine(“———————————————————————————————–“)
Logwriter.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: