Multiple Parameters

Hi guys hope you can help.
Id like to be able to do build the following script that does the following..

checkfile.pl -b <business> -s <scope>  -n <remote file> -f <filename>
where:
<business>, <scope>, <remote file> and <filename> are parameters that are entered in by the user.

Options available would be:

<business>:      -b
"L" --- (for liquor),   # any systems in <filename> that start with the letter "l"
"S" --- (for store),   # any systems in <filename> that start with the letter "s"
"O" --- (for other)   # any systems in <filename> that start with the letter "o"

<scope>:           -s
"A" --- (for all systems in <filename> as defined by the -b switch eg. if user puts in "L" for the -b switch, and "A" for the s switch, then all ("A") liquor systems as defined in <filename> (as entered at the command line for the -f switch), would be processed),  
"S" --- (for systems ending in "NT001" in <filename>),
"N" --- (for systems that DONT end in "NT001" in <filename>)

<remote file>:      -n
a supplied full LOCAL path and file to the file you wish to check the existence for.

<filename>:        -f
a supplied name of a file that has a list of computers in it, 1 computer per line.
This file will be processed based on the criterion of the aforementioned switches, in order for filtering of machines in <filename>.

So, an example of user input to run the script would be:
checkfile.pl -b L -s A -n "d:\bin\testfile.txt" -f systems.txt

What the above would do is:
1) Read the systems.txt file (as defined by -f <filename>
2) Search for the business "L" - any systems that start with "L" (as defined by -b <business)
3) Grab ALL systems that start with "L" - all because of the -s A (as defined by -s <scope>)
4) Check for the existence of "d:\bin\testfile.txt" on the remote system (as defined by -n <remote file>)
5) If the file exists, write to a file called "fileexists.txt" with an entry for the computer name.
6) If the file doesnt exist, write to a file called "filenotexists.txt" with an entry for the computer name.
7) The user should be able to miss some of the parameters, thus being able to do the following:

checkfile.pl -n "d:\bin\testfile.txt" -f systems.txt

In which case, the script would read the systems.txt file, process every system in systems.txt, disregarding the -b and -s switches, and search for the existence of "d:\bin\testfile.txt" on the remote machine.

As the entry for the -n switch is inputted as local eg."d:\bin\testfile.txt", the d: has to be interpreted as a d\$
In other words, when a computer name is read from the <filename>, for example, LR2313NT001, when checking for the file existence, the path is \\\\$SystemName\\d\$\\bin\testfile.txt, if $SystemName is chosen as the variable name to set for the computers in <filename>.

I know how to do the code to connect to each machine, but the code for the multiple parameters is overwhelming, and any help appreciated.

This question will probably go over 2 questions, as I know there is a lot here.

Thanks guys.
LVL 1
Simon336697Asked:
Who is Participating?
 
Adam314Commented:
use Getopt::Long;

my ($Business, $Scope, $RemFileName, $FileName);
GetOptions ("b=s" => \$Business,
            "s=s" => \$Scope,
            "n=s" => \$RemFileName,
            "f=s" => \$FileName) or die "Invalid options\n";

die "Invalid -b option.  Valid options are L, S, and O.\n" unless (!defined($Business) or ($Business =~ /^(L|S|O)$/));
die "Invalid -s option.  Valid options are A, S, and N.\n" unless (!defined($Scope) or ($Scope =~ /^(A|S|N)$/));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTYES,">fileexists.txt") or die "Could not open exists: $!\n";
open(OUTNO,">filenotexists.txt") or die "Could not open notexists: $!\n";
while(<IN>) {
      chomp;
      if(defined($Business)){
            next unless /^$Business/;
      }
      if($Scope eq 'S') {
            next unless /NT001$/;
      }
      if($Scope eq 'N') {
            next if /NT001$/;
      }
      my $LookFor = "\\\\$_\\$RemFilePath";
      if(-e $LookFor){
            print OUTYES "$_\n";
      }
      else {
            print OUTNO "$_\n";
      }
}
close(IN);
0
 
Adam314Commented:
You can read parameters easily with the Getopt:Long module:
http://search.cpan.org/~jv/Getopt-Long-2.36/lib/Getopt/Long.pm


my ($Business, $Scope, $RemFileName, $FileName);
GetOptions ("b=s" => \$Business,
            "s=s" => \$Scope,
            "n=s" => \$RemFileName,
            "f=s" => \$FileName) or die "Invalid options\n";

die "Invalid -b option.  Valid options are L, S, and O.\n" unless $Business =~ /^(L|S|O)$/;
die "Invalid -s option.  Valid options are A, S, and N.\n" unless $Scope =~ /^(A|S|N)$/;
0
 
Adam314Commented:
what is the format of the systems.txt file?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Simon336697Author Commented:
Hi Adam (hope you are going well!).

Mate the format is as follows for systems.txt

LN5834NT001
LS9286NW001
LW2722NW131
SA1209NT001
AS2390NT001
OS7399NQ001
LV6391NW231
OQ2322NT001
OV3187NT001
AN2333NT001
SW1259NT001
0
 
Simon336697Author Commented:
In the following systems.txt

LN5834NT001
LS9286NW001
LW2722NW131
SA1209NT001
AS2390NT001
OS7399NQ001
LV6391NW231
OQ2322NT001
OV3187NT001
AN2333NT001
SW1259NT001

------------------------------
The first character of each computer name is the business code.
So,
if its "L", its liquor,
if its "S", its store,
anything else is "O" for other.
0
 
Simon336697Author Commented:
Example:

checkfile.pl -b <business> -s <scope>  -n <remote file> -f <filename>

checkfile.pl -b S -s S -n "c:\test.txt" -f systems.txt

would work on finding systems starting with S, ending with NT001, testing for existence of c:\test.txt, computer system list from systems.txt

computers to process would be:

SA1209NT001
SW1259NT001

From the list mentioned in my last comment.

0
 
Simon336697Author Commented:
Adam mate thank you for this.

I havent had time to properly test this, but it looks great!
0
 
Adam314Commented:
Let me know if there are any problems.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.