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

Hi, I'm trying to figure out how to make a perl script logon to a a list of windows machines and run a command.

I made this with scriptomaitc but it doesnt quite work and i dont know perl well enough to know why.

use strict; use Win32::OLE('in'); use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; my $LOG_FILE = "D:\\dops\\st.log"; open (LOG, "+>>$LOG_FILE"); my @computers = ("OHDATASTAGESF","OHSTG2SF"); foreach my $computer (@computers) { print "\n"; print "==========================================\n"; print "Computer: $computer\n"; print "==========================================\n"; my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\$computer") + or die "WMI connection failed.\n"; my $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_Logon +Session", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly); print LOG `schtasks /query /v`; foreach my $objItem (in $colItems) { print "AuthenticationPackage: $objItem->{AuthenticationPackage}\ +n"; print "Caption: $objItem->{Caption}\n"; print "Description: $objItem->{Description}\n"; print "InstallDate: $objItem->{InstallDate}\n"; print "LogonId: $objItem->{LogonId}\n"; print "LogonType: $objItem->{LogonType}\n"; print "Name: $objItem->{Name}\n"; print "StartTime: $objItem->{StartTime}\n"; print "Status: $objItem->{Status}\n"; print "\n"; } }sub WMIDateStringToDate(strDate) { return "blah ER"; }

Replies are listed 'Best First'.
Re: Loging into a series of windows machines
by roboticus (Chancellor) on Dec 15, 2007 at 00:17 UTC
Re: Loging into a series of windows machines
by shmem (Chancellor) on Dec 15, 2007 at 00:51 UTC
    Looks like scriptomatic (whatever that is) is seriously b0rken (or at least doesn't work well with perl).

    For instance, that's VBScript-ish:

    foreach my $objItem (in $colItems) {

    That should be either (@$colItems) or (keys %$colItems) or such depending on what type of reference $colItems is. Use e.g. Data::Dumper to see what that is.

    This doesn't parse properly, either:

    sub WMIDateStringToDate(strDate) { return "blah ER"; }

    This construct looks like JavaScript. In perl, you may put subroutine prototypes in parens after a subroutine identifier, but you generally don't want to, because you don't need that. And you don't name your parameters to a sub.

    So that is

    sub WMIDateStringToDate { return "blah ER"; }
    which, given its usefulness and no being called anywhere, you could just drop entirely.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      For instance, that's VBScript-ish: foreach my $objItem (in $colItems) { That should be either (@$colItems) or (keys %$colItems) or such depending on what type of reference $colItems is.

      Actually not. $colItems is an Win32::OLE object and in is a function exported by that module for iterating collections.

      So ease up on the OP cos Win32::OLE is one of the hardest modules around to get to grips with even when you are fully conversant with Perl.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Loging into a series of windows machines
by Marza (Vicar) on Dec 15, 2007 at 00:52 UTC

    That needs some work and at the moment; I don't have the time to debug it. Maybe one of the others might.

    I would suggest taking a look at our tutorials as they are very good for beginners and take a look at the Code Caverns. Click the Code link on the above left. Take a look at the NT Admin scripts and you will see a couple examples that deal with computers and checking things. I even have an old one dealing with disk space checks. You might see something you can use.

    Oh and welcome! :)