无法使用WMI和Perl连接到远程计算机

时间:2010-12-22 15:19:58

标签: perl wmi wmi-query win32ole

我正在尝试编写一个脚本,该脚本将使用win32 :: ole模块和WMI查询从远程Windows机器上获取事件日志信息。我可以ping机器,但无论我的WMI连接总是使用ConnectServer()方法失败。我很确定它不是防火墙相关的问题。这是我的代码:

use Win32::OLE qw(in);
use Net::Ping;

use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;

my $computer = "10.10.10.15";

my $user = "Administrator";
my $pwd = "pass";


$p = Net::Ping->new();
print "$computer is alive.\n" if $p->ping($host);
$p->close();

  my $locatorObj =Win32::OLE->new("WbemScripting.SWbemLocator") or die "ERROR CREATING OBJ";

  $locatorObj->{Security_}->{impersonationlevel} = 3;

  my $objWMIService = $locatorObj->ConnectServer($computer, "root\civm2", $user, $pwd) or die "WMI connection failed.\n";

  my $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_NTLogEvent", "WQL",
              wbemFlagReturnImmediately | wbemFlagForwardOnly);

   foreach my $objItem (in $colItems) {
      print "Category: $objItem->{Category}\n";
      print "CategoryString: $objItem->{CategoryString}\n";
      print "ComputerName: $objItem->{ComputerName}\n";
      print "Data: " . join(",", (in $objItem->{Data})) . "\n";
      print "EventCode: $objItem->{EventCode}\n";
      print "EventIdentifier: $objItem->{EventIdentifier}\n";
      print "EventType: $objItem->{EventType}\n";
      print "InsertionStrings: " . join(",", (in $objItem->{InsertionStrings})) . "\n";
      print "Logfile: $objItem->{Logfile}\n";
      print "Message: $objItem->{Message}\n";
      print "RecordNumber: $objItem->{RecordNumber}\n";
      print "SourceName: $objItem->{SourceName}\n";
      print "TimeGenerated: $objItem->{TimeGenerated}\n";
      print "TimeWritten: $objItem->{TimeWritten}\n";
      print "Type: $objItem->{Type}\n";
      print "User: $objItem->{User}\n";
      print "\n";
   }

我尝试连接的任何想法总是失败?谢谢:))

1 个答案:

答案 0 :(得分:1)

ConnectServer电话有几个潜在的问题:

  • 我认为它需要两个反斜杠。
  • 它有一个拼写错误:civm2 - > CIMV2

它可能会通过添加调用来检索错误信息来显示更多信息:

my $objWMIService = $locatorObj->ConnectServer($computer, "root\\cimv2", $user, $pwd)
        or die "WMI connection failed.\n", Win32::OLE->LastError;