Using foreach?? maybe..

S

SimonH

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 systems CimV2 repository in turn.
for my $computer( @computers ){
my $wmi_repos =
Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\$computer\\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";
} } } }
##################################################
 
B

Brad Baxter

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

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top