views:

241

answers:

1

Currently with WMI's Win32_Process, I seem unable to detect if a batch file is currently running

all it lists is CommandLine of cmd.exe, without telling me which batch file that particular cmd.exe is running. Anybody have any insights there?

code snippet (ruby-wmi)

many_args = 'batch_file.bat'
procs = WMI::Win32_Process.find(:all)
procs.each{|proc|
  if (proc.CommandLine.contain?(many_args)) || proc.Name.include?(many_args) # never succeeds

      ...

   end

  }
+1  A: 
require 'win32ole'

many_args = "test.cmd"

wmi = WIN32OLE.connect("winmgmts://")

processes = wmi.ExecQuery("select * from win32_process")

processes.each do |process|
  if process.CommandLine != nil && process.CommandLine.include?(many_args) then
    puts process.inspect
    puts "Name: #{process.Name}"
    puts "CommandLine: #{process.CommandLine}"
    puts "CreationDate: #{process.CreationDate}"
    puts "WorkingSetSize: #{process.WorkingSetSize}"
  end
end

see more at this link: http://rubyonwindows.blogspot.com/2007/07/using-ruby-wmi-to-get-win32-process.html

The following is my output while the test.cmd is running:

#<WIN32OLE:0x2b8f360>
Name: cmd.exe
CommandLine: cmd /c ""C:\wmi test\test.cmd" "
CreationDate: 20100108083948.497052-300
WorkingSetSize: 1593344
Beanish
I really like the code--does this work for you on your box if you have test.cmd running in another windowex:contentsbash -c 'sleep 1000'or what not?-r
rogerdpack
all i did was make test.cmd with pause as the only command. It worked for me on my machine. Windows xp - ruby 1.8.6. Does it not work for you?
Beanish
Interesting. It does work for .cmd but not for .bat [?]Sweet--at least I have a work around of naming all my .bat files .cmd :)-r
rogerdpack