vCenter alarm: List all processes

This PowerShell script obtains a list of processes running on the target machine and returns the top 5.

	$smtpServer = “smtp-server”
	$vmId= $env:VMWARE_ALARM_TARGET_NAME
	$alarmName = $env:VMWARE_ALARM_NAME
	$alarmDesc = $env:VMWARE_ALARM_EVENTDESCRIPTION
	$credFile = Get-Content -Path C:\Scripts\Credential | ConvertTo-SecureString
	$userName = “domain\user”
	$pscreds = New-Object System.Management.Automation.PSCredential ($userName, $credFile)
	$to = “email to”
	$from = “email from”
	$Subject = “[ALARM: ] Triggered on ” + $vmId
	$BodyStart =”Alarm has been triggered what follows is a list of the top 5 processes for each catagory<br><br>”

	function AddOwner($ProcessList){
	#Loop through the proccess list and attach owner information to each string
	$retarry = @()
	foreach ($proc in $ProcessList){
	if ($proc.IDProcess -eq 0){
	continue
	}
	$procinfo = gwmi -Class win32_process -Credential $pscreds -computername $vmId | select ProcessId, Name, ExecutablePath ,
	@{Name=”Owner”; Expression={$_.getowner().domain+”\”+ $_.getowner().user}} | where-object {$_.ProcessId -EQ $proc.IDProcess}
	$outArry = New-Object psobject -Property @{
	Name = $procinfo.Name
	ExecutablePath = $procinfo.ExecutablePath
	Owner = $procinfo.Owner
	IORSec = $proc.IOReadBytesPerSec
	IOWSec = $proc.IOWriteBytesPerSec
	ProcPer = $proc.PercentProcessorTime
	}
	$retarry += $outArry
	}
	return $retarry
	}
	#throwaway data, needed to initialize for some reason
	$junk = gwmi -Class Win32_PerfFormattedData_PerfProc_Process -computername $vmId
	$processesCPU = AddOwner(gwmi -Class Win32_PerfFormattedData_PerfProc_Process -Credential $pscreds -computername $vmId | sort PercentProcessorTime -des | select IDProcess ,IOWriteBytesPerSec ,IOReadBytesPerSec,PercentProcessorTime -First 5)
	$processesIOR = AddOwner(gwmi -Class Win32_PerfFormattedData_PerfProc_Process -Credential $pscreds -computername $vmId | sort IOReadBytesPerSec -des | select IDProcess,PercentProcessorTime,IOWriteBytesPerSec ,IOReadBytesPerSec -First 5)
	$processesIOW = AddOwner(gwmi -Class Win32_PerfFormattedData_PerfProc_Process -Credential $pscreds -computername $vmId | sort IOWriteBytesPerSec -des | select IDProcess,PercentProcessorTime,IOReadBytesPerSec ,IOWriteBytesPerSec -First 5)

	$diskInfo = get-counter -computername $vmId “LogicalDisk(_Total)\Avg. Disk Queue Length” -SampleInterval 3
	$BodyprocInfo = $diskInfo.Readings + ” This value should be 2 or lower, a high number indicates that the system is waiting on the drive to respond<br>”

	$BodyCpu = $processesCPU| format-table| out-string -Width 160
	$BodyCpu = “<br>Percent Processor Time <br>” + $BodyCpu
	$BodyIOR = $processesIOR| format-table| out-string -Width 160
	$BodyIOR = “<br>IO Read Bytes Per Second <br>” + $BodyIOR
	$BodyIOW = $processesIOW| format-table| out-string -Width 160
	$BodyIOW = “<br>IO Write Bytes Per Second <br>” + $BodyIOW
	$body = $BodyStart + $BodyprocInfo + $BodyCpu + $BodyIOR + $BodyIOW
	Write-Output $body
	Send-MailMessage -to $to -SmtpServer $smtpServer -From $from –Subject $Subject -Body $body -BodyAsHtml

Jeremy Tirrell

Read more posts by this author.