DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Display Process In Ruby

09.27.2011
| 3482 views |
  • submit to reddit
        // description of your code here

require 'win32ole'

writeFile = File.open("./process.txt","w")
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from win32_process")

for process in processes do    
	for property in process.Properties_ do        
		writeFile.puts property.Name    
	end    
	break
end
writeFile.puts
writeFile.puts "***********************************************************************************"
writeFile.puts
for process in processes do
	writeFile.puts "Caption: #{process.Caption}"
	writeFile.puts "CommandLine: #{process.CommandLine}"
	writeFile.puts "CreationClassName: #{process.CreationClassName}"
	writeFile.puts "CreationDate: #{process.CreationDate}"
	writeFile.puts "CSCreationClassName: #{process.CSCreationClassName}"
	writeFile.puts "CSName: #{process.CSName}"
	writeFile.puts "Description: #{process.Description}"
	writeFile.puts "ExecutablePath: #{process.ExecutablePath}"
	writeFile.puts "ExecutionState: #{process.ExecutionState}"
	writeFile.puts "Handle: #{process.Handle}"
	writeFile.puts "HandleCount: #{process.HandleCount}"
	writeFile.puts "InstallDate: #{process.InstallDate}"
	writeFile.puts "KernelModeTime: #{process.KernelModeTime}"
	writeFile.puts "MaximumWorkingSetSize: #{process.MaximumWorkingSetSize}"
	writeFile.puts "MinimumWorkingSetSize: #{process.MinimumWorkingSetSize}"
	writeFile.puts "Name: #{process.Name}"
	writeFile.puts "OSCreationClassName: #{process.OSCreationClassName}"
	writeFile.puts "OsName: #{process.OSName}"
	writeFile.puts "OtherOperationCount: #{process.OtherOperationCount}"
	writeFile.puts "OtherTransferCount: #{process.OtherTransferCount}"
	writeFile.puts "PageFaults: #{process.PageFaults}"
	writeFile.puts "PageFileUsage: #{process.PageFileUsage}"
	writeFile.puts "ParentProcessId: #{process.ParentProcessId}"
	writeFile.puts "PeakPageFileUsage: #{process.PeakPageFileUsage}"
	writeFile.puts "PeakVirtualSize: #{process.PeakVirtualSize}"
	writeFile.puts "PeakWorkingSetSize: #{process.PeakWorkingSetSize}"
	writeFile.puts "Priority: #{process.Priority}"
	writeFile.puts "PrivatePageCount: #{process.PrivatePageCount}"
	writeFile.puts "ProcessId: #{process.ProcessId}"
	writeFile.puts "QuotaNonPagedPoolUsage: #{process.QuotaNonPagedPoolUsage}"
	writeFile.puts "QuotaPagedPoolUsage: #{process.QuotaPagedPoolUsage}"
	writeFile.puts "QuotaPeakNonPagedPoolUsage: #{process.QuotaPeakNonPagedPoolUsage}"
	writeFile.puts "QuotaPeakPagedPoolUsage: #{process.QuotaPeakPagedPoolUsage}"
	writeFile.puts "ReadOperationCount: #{process.ReadOperationCount}"
	writeFile.puts "ReadTransferCount: #{process.ReadTransferCount}"
	writeFile.puts "SessionId: #{process.SessionId}"
	writeFile.puts "Status: #{process.Status}"
	writeFile.puts "TerminationDate: #{process.TerminationDate}"
	writeFile.puts "ThreadCount: #{process.ThreadCount}"
	writeFile.puts "UserModeTime: #{process.UserModeTime}"
	writeFile.puts "VirtualSize: #{process.VirtualSize}"
	writeFile.puts "WindowsVersion: #{process.WindowsVersion}"
	writeFile.puts "WorkingSetSize: #{process.WorkingSetSize}"
	writeFile.puts "WriteOperationCount: #{process.WriteOperationCount}"
	writeFile.puts "WriteTransferCount: #{process.WriteTransferCount}"
	writeFile.puts
	writeFile.puts "*******************************************************************************"
	writeFile.puts
end
    

Comments

Snippets Manager replied on Fri, 2010/10/01 - 5:57am

you could use: if process.Name.include? "Excel.exe" then puts "Name: #{process.Name}" puts "Process ID: #{process.Pid}" puts process.Terminate end

Snippets Manager replied on Wed, 2009/09/30 - 11:44am

require 'win32ole' writeFile = STDOUT # get process list with properties on each wmi = WIN32OLE.connect("winmgmts://") processes = wmi.ExecQuery("select * from win32_process") # get propeties name lp=[] processes.each { |pr| pr.Properties_.each { |x| lp << x.name} ; break } # get process name(s) and make et regexp filter name_filter=Regexp.new('('+ARGV.join(')|(')+')','i') for process in processes do if process.Caption =~ name_filter writeFile.puts "=========================================================" writeFile.puts (">"+ process.CommandLine.strip+"<").center(80) writeFile.puts "=========================================================" lp.each_with_index { |pname,i| writeFile.puts "%2d %-20s %s"% [i,pname, process.send(pname)] } end end writeFile.puts "==========================================================" processes.each { |process| puts "%20s : %s" % [process.Caption,process.CommandLine.strip] if process.Caption =~ name_filter }