Nar has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to port over some VBS WMI calls over to perl. I have been able to pull the user logon time thus far but am still having trouble pulling the user name. Also, dumping the $ev variable hasnt provided me with any information to point me in the right place.
use DBI; my $computer = "."; my $dbh_connect = DBI->connect("dbi:WMI:$computer"); my $wmi_call = $dbh_connect->prepare(<<WQL); Select * from Win32_LogonSession Where LogonType = 2 OR LogonType += 10 WQL $wmi_call->execute(); while (defined (my $row = $wmi_call->fetchrow_arrayref())) { my $ev = $row->[0]; $time = $ev->{StartTime}; $user = $ev->{Name}; } print "User: $user LOGON TIME: $time;";
From reviewing the VBS it appears that the problem is where I am calling the $wmi_call->execute(); When it was done in VBS the call was made like this
Set colList = objWMI.ExecQuery("Associators of " _ & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " +_ & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
But thus far I have been unable to port that execute over to perl

Replies are listed 'Best First'.
Re: PERL WMI call
by planetscape (Chancellor) on Sep 17, 2007 at 15:42 UTC
Re: PERL WMI call
by moritz (Cardinal) on Sep 17, 2007 at 14:37 UTC
    What exactly is your error message?

    Always use strict; in your code.

    Also, you are retrieving an array ref and use it like a array ref to a hash ref.

    Try to use Data::Dumper; and print Dumper $row; to see if it contains what you think it should.

Re: PERL WMI call
by Muggins (Pilgrim) on Sep 18, 2007 at 10:27 UTC
    Hi,

    I don't know which is best but you can also do WMI calls using the Win32::OLE module, see particularly the code in Win32::Process::Info(::WMI)

    Almost immediate UPDATE:

    The following (based on the first example in Planetscape's "Single line of code" link) works for me. The $WMI->Execute() call returns an object or list of objects which Data::Dumper displays here, should give you the info

    use strict; use warnings; use Data::Dumper; use Win32::OLE; use Win32::OLE::Const; use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; my $computer = "localhost"; my $WMI = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2") + or die "WMI connection failed.\n"; my @items = $WMI->ExecQuery("SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly); print Dumper($_) for (@items);