开发者

cant connect to remote machine using WMI and Perl

I'm trying to write a script that will get event log information off of a remote windows machine using the win32::ole module and a WMI query. I can ping the machine but no matter what my WMI connection always fails using the ConnectServer() method. I'm pretty sure its not a firewall related problem. Here is my code:

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 $开发者_StackOverflowlocatorObj =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";
   }

Any ideas why my attempt to connect always fails? Thanks :)


The ConnectServer call has a couple of potential issues:

  • I believe it needs two back slashes.
  • And It has a typo: civm2 -> cimv2

And it might reveal more information by adding a call to retrieve the error information:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜