如何使用WMI发现正在运行的批处理(.bat)文件名?

时间:2010-01-07 19:08:23

标签: ruby wmi win32-process

目前使用WMI的Win32_Process, 我似乎无法检测批处理文件当前是否正在运行

所有列出的是cmd.exe的CommandLine,而没有告诉我哪个批处理文件正在运行特定的cmd.exe。 有人有任何见解吗?

代码段(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 个答案:

答案 0 :(得分:2)

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

在此链接中查看更多信息: http://rubyonwindows.blogspot.com/2007/07/using-ruby-wmi-to-get-win32-process.html

以下是test.cmd运行时的输出:

#<WIN32OLE:0x2b8f360>
Name: cmd.exe
CommandLine: cmd /c ""C:\wmi test\test.cmd" "
CreationDate: 20100108083948.497052-300
WorkingSetSize: 1593344