2

I am very new to scripting/programming and WMI but I have been tasked with gathering information from a list of systems to see if the screensavers are active or not. I need to be able to then store the information to a file and have a cron job run every 10 minutes or so and append the results to said file.

There is noting in there as of yet to write to the file as I am stumped on just getting it to return the correct information in the first place.

Any pointers you guys can provide would be great.

Here is what I have so far:

#!/usr/bin/perl

use strict;

my $file = 'hosts.txt';
open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>)  { 
    my $user = "uid";
    my $password = "pwd";
    my $server = $line;
    my $result = `wmic -U domain/$user%"$password" //$server "select * from Win32_Process WHERE Name = 'LogonUI.exe'"`;
    my @results = split(/\n/,$result);
    my @tmparray = split(/\|/, @results[2]);
    my $isActive = 0;

    use POSIX qw/strftime/;

    if (@tmparray[0] eq 'LogonUI.exe') {
        $isActive = 1;
    }
    else {
        $isActive = 0;
    }

    print strftime("Date:%Y-%m-%d %H:%M:%S, ", localtime(time));
    print "Host:$server, Screensaver IsActive:$isActive,\n";
}
3
  • Out of pure curiosity, why do you need to monitor whether screensavers are on or not? Feb 13, 2014 at 18:08
  • Also, you might consider using DBD::WMI instead of calling wmic in backticks. I haven't used it but it should give you a cleaner interface; you wouldn't have to worry about nested quotes, among other things. Feb 13, 2014 at 18:23
  • I was asked to find a way to see if a user was actually using the system. This was about the best way I could think of. There might be better options in the future as we are in the middle of upgrading the management systems. I will look into the DBD::WMI as that sounds like it might take care of some issues I just ran into. I ended up writing another script to take care of the file read/write but the issue now is what to do if a system returns a value other than LogonUI.exe such as NTSTATUS: NT_STATUS_CONNECTION_REFUSED or just timed out. Now it sees it as being active.
    – Jeremiah
    Feb 13, 2014 at 22:22

1 Answer 1

0

Win32::OLE supports the creation of management objects. I've used it like so:

Win32::OLE->GetObject( "winmgmts:\\\\$host\\$path" );

And then with that object called the ExecQuery method on it:

$wmi->ExecQuery( 
      $query
     , 'WQL'
     , (  ( $flags // 0 ) 
       || wbemFlagReturnImmediately | wbemFlagForwardOnly
       )
     );

I've only used this on my local machine to change my IP configuration, automatically.

1
  • Thank you for the example. I will start working on something to see if that will do what we need.
    – Jeremiah
    Feb 13, 2014 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.