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

发布于 2024-10-09 02:40:23 字数 1934 浏览 9 评论 0原文

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

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";
   }

有什么想法为什么我的连接尝试总是失败吗?谢谢 :)

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 $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";
   }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-10-16 02:40:23

ConnectServer 调用有几个潜在的问题:

  • 我认为它需要两个反斜杠。
  • 它有一个错字:civm2 -> cimv2

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

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文