Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Using foreach?? maybe..


Hi!

Im trying to change the following Ive worked on into using a list of
machines in a file called machine.txt
At the moment, Ive used an array of computer names im testing on. This
works, but Id like some direction on how to reference a file instead of
using an array. Any help greatly appreciated. Thank you.
I believe I might have to use a foreach statement and possibly a split
function on the machine.txt file, but not sure where to start with this.

The format of the machine.txt file is:

dell101
dell102
dell103
etc

#use diagnostics;
#use strict;
use Win32::OLE('in');

# Use the Win32_Process
my @requests = ('Win32_Process', );

# Computer list
my @computers = qw/dell101 dell102 dell103/;     # having trouble
here....instead of listing, id like to reference a file called machine.txt

# Connect to each system s CimV2 repository in turn.
for my $computer( @computers ){
my $wmi_repos =
Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\$compu ter\\root\\CIMV2")
or die "WMI connection failed: ".Win32::OLE->LastError()."\n";

for my $request( @requests ){
my $wmi_collection = $wmi_repos->ExecQuery("SELECT * FROM $request", "WQL",
48 )
or die "WQL query failed: ".Win32::OLE->LastError()."\n";

for my $item (in $wmi_collection){
my @methods = join (", ", map {$_->{Name}}( in $item->{Methods_} ) );
my %properties = map { $_->{Name} => $_->{Value} }( in
$item->{Properties_} );

# Print results
print "\n$computer :: $request :: $properties{Name}\n", "\tMethods:
@methods\n\tProperties:\n";
for my $property( sort keys %properties ){
( $properties{$property} )
? print "\t\t$property = $properties{$property}\n"
: print "\t\t$property =\n";

} } } }

##################################################
SimonH wrote:

...

> Im trying to change the following Ive worked on into using a list of
> machines in a file called machine.txt
> At the moment, Ive used an array of computer names im testing on. This
> works, but Id like some direction on how to reference a file instead of
> using an array. Any help greatly appreciated. Thank you.
> I believe I might have to use a foreach statement and possibly a split
> function on the machine.txt file, but not sure where to start with this.

> The format of the machine.txt file is:

> dell101
> dell102
> dell103
> etc

Try something like:

    use warnings;
    use strict;
    my $fh;
    my @computers;
    open $fh,"<","machine.txt"
       or die "Oops, couldn't open machine.txt for input, $!";
    while(<$fh>){
       chomp;
       push @computers,$_;
    }
    close($fh);
    #...

...

> # Computer list
> my @computers = qw/dell101 dell102 dell103/;     # having trouble
> here....instead of listing, id like to reference a file called machine.txt

...
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
On Jun 3, 2:10 pm, Bob Walton <see.@rochester.rr.com> wrote:

Or a little more succinctly,

open my $fh,"<","machine.txt"
   or die "Oops, couldn't open machine.txt for input, $!";
my @computers = <$fh>;
chomp @computers;
close($fh);

However, that slurps the whole file, which it looks like
you don't need.  Instead, you could:

open my $fh,"<","machine.txt"
   or die "Oops, couldn't open machine.txt for input, $!";

while( defined( my $computer = <$fh> ) ) {
    chomp $computer;

    # do all your other stuff with $computer

}

close($fh);

# or more succinctly ...

open $fh,"<","machine.txt"
   or die "Oops, couldn't open machine.txt for input, $!";

while( <$fh> ) {
    chomp;

    # do all your other stuff with $_

}

close($fh);

And please improve your indentation.  :-)

--
Brad

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc