Windows Perl Scripting Forums » Perl

script help please, i almost have it

(2 posts)
  • Started 11 years ago by TSgt_Mad
  • Latest reply from TSgt_Mad

Tags:


  1. TSgt_Mad
    Member

    Still pretty new at this PERL language, can someone take a look at this and tell me what is wrong?


    use Win32::OLE('in');
    use constant wbemFlagReturnImmediately => 0x10;
    use constant wbemFlagForwardOnly => 0x20;
    #use Win32::AdminMisc;

    #Read from your selected file, make sure the path is right; you must use \\ or UNC \\\\
    $computer="c:\\list\\hosts.txt \n";
    open(DATA,$computer) or die "Could not open File!, check the path! \n";
    @raw_list=<DATA>;
    print @raw_list;
    close(DATA)



    foreach($computer, @raw_list);
    $objWMIService = Win32::OLE->GetObject
        ("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection failed.\n";
    $colItems = $objWMIService->ExecQuery
        ("SELECT * FROM Win32_OperatingSystem","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);

    foreach my $objItem (in $colItems)
    {
          print "Caption: $objItem->{Caption}\n";
          print "CSD Version: $objItem->{CSDVersion}\n";
          print "CS Name: $objItem->{CSName}\n";
          print "Name: $objItem->{Name}\n";
          print "Service Pack Major Version: $objItem->{ServicePackMajorVersion}\n";
          print "System Directory: $objItem->{SystemDirectory}\n";
          print "Windows Directory: $objItem->{WindowsDirectory}\n";
          print "\n";
    }



    it is erroring on foreach($computer,@raw_list)

    it will read the file because it does print it out, but it does not like the WMI connection. I am running this with my admin account so i do have WMI access to read these properties.

    thanks

    JM

    EDIT: ok, i ran the script with perl -w and this is what i received

    Odd number of elements in hash assignment at retina.pl line 21, <DATA> line 3.
    Win32::OLE(0.1707) error 0x80041021
    after character 0 in "winmgmts:\\c:\list\hosts.txt
    \root\CIMV2" at retina.pl line 30
    eval {...} called at retina.pl line 30
    WMI connection failed.

    so basically, what i gather from this is that it may not even be connecting to the WMI service to begin with? And for some reason it is reading what is supposed to be a hostname as the literal filename and path. Am i on the right track here?

    thank you
    Posted 11 years ago #
  2. TSgt_Mad
    Member

    TSgt_Mad Escribió:

    Still pretty new at this PERL language, can someone take a look at this and tell me what is wrong?


    use Win32::OLE('in');
    use constant wbemFlagReturnImmediately => 0x10;
    use constant wbemFlagForwardOnly => 0x20;
    #use Win32::AdminMisc;

    #Read from your selected file, make sure the path is right; you must use \\ or UNC \\\\
    $computer="c:\\list\\hosts.txt \n";
    open(DATA,$computer) or die "Could not open File!, check the path! \n";
    @raw_list=<DATA>;
    print @raw_list;
    close(DATA)



    foreach($computer, @raw_list);
    $objWMIService = Win32::OLE->GetObject
        ("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection failed.\n";
    $colItems = $objWMIService->ExecQuery
        ("SELECT * FROM Win32_OperatingSystem","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);

    foreach my $objItem (in $colItems)
    {
          print "Caption: $objItem->{Caption}\n";
          print "CSD Version: $objItem->{CSDVersion}\n";
          print "CS Name: $objItem->{CSName}\n";
          print "Name: $objItem->{Name}\n";
          print "Service Pack Major Version: $objItem->{ServicePackMajorVersion}\n";
          print "System Directory: $objItem->{SystemDirectory}\n";
          print "Windows Directory: $objItem->{WindowsDirectory}\n";
          print "\n";
    }



    it is erroring on foreach($computer,@raw_list)

    it will read the file because it does print it out, but it does not like the WMI connection. I am running this with my admin account so i do have WMI access to read these properties.

    thanks

    JM

    EDIT: ok, i ran the script with perl -w and this is what i received

    Odd number of elements in hash assignment at retina.pl line 21, <DATA> line 3.
    Win32::OLE(0.1707) error 0x80041021
    after character 0 in "winmgmts:\\c:\list\hosts.txt
    \root\CIMV2" at retina.pl line 30
    eval {...} called at retina.pl line 30
    WMI connection failed.

    so basically, what i gather from this is that it may not even be connecting to the WMI service to begin with? And for some reason it is reading what is supposed to be a hostname as the literal filename and path. Am i on the right track here?

    thank you



    So i changed it slightly, and i get the <DATA> to read in, but now i get an RPC error. Do i need to call another Win32? Maybe NetAdmin?

    use Win32::OLE;
    use Win32::OLE('in');
    use constant wbemFlagReturnImmediately => 0x10;
    use constant wbemFlagForwardOnly => 0x20;
    #use Win32::AdminMisc;

    #Read from your selected file, make sure the path is right; you must use \\ or UNC \\\\
    #$computer="gws-999xxx1337";
    open(DATA,"<hosts.txt \n") || die "Could not open File!, check the path! \n";

    while( my $computer = <DATA>)
    {
    print $computer;
    foreach my $computer(in(<DATA>))
    {
    $objWMIService = Win32::OLE->GetObject
        ("winmgmts:\\\\$computer\\root\\CIMV2") || die "WMI connection failed.\n";
    $colItems = $objWMIService->ExecQuery
        ("SELECT * FROM Win32_OperatingSystem","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);

    foreach my $objItem (in $colItems)
    {
          print "Caption: $objItem->{Caption}\n";
          print "CSD Version: $objItem->{CSDVersion}\n";
          print "CS Name: $objItem->{CSName}\n";
          print "Name: $objItem->{Name}\n";
          print "Service Pack Major Version: $objItem->{ServicePackMajorVersion}\n";
          print "System Directory: $objItem->{SystemDirectory}\n";
          print "Windows Directory: $objItem->{WindowsDirectory}\n";
          print "\n";
    }
    }
    }
    close(DATA)



    i know this is really long, but thanks for the help.
    Posted 11 years ago #

RSS feed for this topic

Reply

You must log in to post.