Critique my code - need a bit of help (Perl for Win32) 
Author Message
 Critique my code - need a bit of help (Perl for Win32)

I have to rollout a shortcut update to several dozen WinNT/Win2k
workstations. We are going through a server migration and are looking
to replace all desktop shortcuts (*.lnk) that have targets that are in
the INC convention:

IE:

Desktop Path         \Administrator\Desktop  
LinkName             \file.lnk
Arguments            <none>
Target               \\server\directoryA\directoryB\File.doc
Working Directory    \\server\directoryA\directoryB\
Icon File            C:\somefile.exe
Icon Index           12

We are using the WinNT resource kit tool Shortcut.exe to perform this
maneuovre.

What I want help with is the setup of our script...I want to be able
to swap out the various link names and targets for other link names
and targets as we need to so that we can push many different NT
shortcuts as needed.

The following script takes a text file, named "computers.txt" and
parses every line for the workstation name (hostname) and from there
we get an output if needed (shortcut attribute dumped to a local file
for instance). It has been modified to UPDATE a given shorcut
("file.lnk") file by name only, but it's not working. this is where my
lack of knowledge is getting me stuck. Does anyone have any general
suggestions as to where I go from here, or some help with fixing this?

Thanks in advance everyone - I'm getting my feet wet here so I hope I
haven't asked too much of the roving help here.

Jason B

=========start================

#!C:\Perl\bin\perl.exe
################################################################################
#                                                                    
        #
#        makeshortcut.pl                                              
            #
#                                                                    
        #
################################################################################

#   UDS.pl - ver1.03.2002
#   ----------------------
#   This script extracts computer configurations from either machines
by a given computer list text file
#
#   Syntax:
#       perl makeshortcut.pl   [ Computer List File ]
#
#   Examples:
#       perl makeshortcut.pl   computers.txt
#
#

#======================================#
#       Module to be imported          #
#======================================#
use Win32;
use win32::Registry;

use Win32::TieRegistry( Delimiter=>"/" );
use Win32::NetAdmin;
use POSIX;

#==============================================================================#
#       Programs - Only works for Class C subnet or individual destop
        #
#==============================================================================#

{
        print<<"eof";
##############################################################################
#                                                                    
      #
#    Perl Script for scanning workstations over network.              
      #
#                                                                    
      #
#    Syntax:  perl makeshortcut.pl  [ Computer List File ]            
      #
#                                                                    
      #
#    Examples:                                                        
      #
#    perl uds.pl computers     Retrieve all workstations in
computers.txt    #
#                                                                    
      #
##############################################################################
eof
        die 'Syntax Incorrect. Goodbye';

Quote:
}


{

        my $HostName=$ARGV[0];

        open FH_NAME, "$_.txt";

        close FH_NAME;

        $Delimiter = "\t";

        open FH_REPORT, ">C:\\Temp\\$_-Shortcut.txt";

        print FH_REPORT "Workstation\tLinkName\tArguments\tTarget\tWorking
Directory\tIcon File\tIcon Index\tUser Profile Desktop Path\n";
        # Go through Each workstation by first testing existence of the
workstation,
        # and connect to its registry if found


        $Current = 0;


        {
                my ($LM, $hkey);

                $Current ++;

                chomp($Machine);
                $Machine =~ tr/a-z/A-Z/;

                print "\n$Current\/$Total\tWorking on '$Machine' ... ";




                {
                        print STDERR    "\t! Failed to PING it.\n";
                        print FH_REPORT "\? $Machine\t! Failed to PING it.\n";
                        next; # Skip this Machine since failed to ping it
                }

                my ($hkey, $DataType, $Ver, $ProfileDir);

                if (!$HKEY_LOCAL_MACHINE->Connect($Machine, $LM))
                {
                        print STDERR    "\t! Failed to connect to the registry.\n";
                        print FH_REPORT "\? $Machine\t! Failed to connect to the
registry.\n";
                        next; # Skip this IP Address since the workstation's registry can't
be accessed
                }

                # -- Check out the Windows NT version to determine the profile
directory --
                $RegistryKey = "SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion\\ProfileList";

                $LM->Open($RegistryKey, $hkey) || die "Error: ProfileList";


                {
                        next if (!grep {/S-1-5-21/} $SID);

                        $RegistryKey .= "\\";
                        $RegistryKey .= $SID;
        #print "$RegistryKey\n";
                        $LM->Open($RegistryKey, $hkey) || die;
                        $hkey->QueryValueEx('ProfileImagePath', $DataType, $ProfilePath) ||
die;
                        $ProfileDir = "\\\\$Machine\\C\$\\Winnt\\Profiles" if (grep
{/Profiles/} $ProfilePath);
                        $ProfileDir = "\\\\$Machine\\C\$\\Docume~1"        if (grep
{/Documents/} $ProfilePath);
                        last;
                }
                #$LM->close();
                #$hkey->close();

                $Path_Desktop = $ProfileDir."\\ALLUSE~1\\Desktop\\";
                #print "$Path_Desktop\n";
                my $LinkName = $Path_Desktop."IBMS.lnk";
                `shortcut.exe -f -t "c:\\ibms\\csdc\\amanda4.1\\iprod.bat" -n
"$LinkName" -i "c:\\ibms\\csdc\\amanda4.1\\ibmsprod.ico" -d
"c:\\ibms\\csdc\\amanda4.1"`;

                $Path_Link        =$Path_Desktop."*.LNK";


                {
                        chomp($LinkName);
        #print "$LinkName\n";
                        $LinkName_Path = $Path_Desktop.$LinkName;
        #print "$LinkName_Path\n";

                        print FH_REPORT $Machine.$Delimiter;


                        {
                                chomp($ShortcutLine);
                                print FH_REPORT $ShortcutLine;
                                print FH_REPORT $Delimiter;
                        }
                        print FH_REPORT $Path_Desktop;
                        print FH_REPORT "\n";
                }
                print STDERR "OK";

        }
        close FH_REPORT;

Quote:
}



Tue, 22 Nov 2005 23:20:43 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:

> The following script takes ... but it's not working. this is where
> my lack of knowledge is getting me stuck. Does anyone have any
> general suggestions as to where I go from here, or some help with
> fixing this?

The obvious general suggestion would be to ask Perl to tell you why
"it's not working" by using

     use strict;
     use warnings;

and study the resulting messages in the error log.

/ Gunnar

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



Tue, 22 Nov 2005 23:32:34 GMT  
 Critique my code - need a bit of help (Perl for Win32)
Thanks kindly Gunnar,

I'm certain the best advice will be the most obvious to the veteran coders
here.

I appreciate every bit of advice,

Jason Bunston


Quote:


>> The following script takes ... but it's not working. this is where
>> my lack of knowledge is getting me stuck. Does anyone have any
>> general suggestions as to where I go from here, or some help with
>> fixing this?

> The obvious general suggestion would be to ask Perl to tell you why
> "it's not working" by using

>    use strict;
>    use warnings;

> and study the resulting messages in the error log.

> / Gunnar



Wed, 23 Nov 2005 00:51:07 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:


>> The obvious general suggestion would be to ask Perl to tell you
>> why "it's not working" by using

>>     use strict;
>>     use warnings;

>> and study the resulting messages in the error log.

> I'm certain the best advice will be the most obvious to the veteran
> coders here.

> I appreciate every bit of advice,

Uhm.. Maybe I'm misunderstanding you now, Jason, but are you saying
that using strictures and warnings is just for veteran coders? It's
not. It's even more important for beginners like you and me.

/ Gunnar

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



Wed, 23 Nov 2005 01:18:06 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:

> I'm certain the best advice will be the most obvious to the veteran coders
> here.

You do not need to be a veteran, you just need to have seen the
Posting Guidelines that are posted here twice each week.

Quote:
> I appreciate every bit of advice,

Here's a whole bunch more:

   http://mail.augustmail.com/~tadmc/clpmisc.shtml

[ snip TOFU ]

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 23 Nov 2005 02:38:05 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:

>    {
>            my ($LM, $hkey);

[snip]

Quote:
>            my ($hkey, $DataType, $Ver, $ProfileDir);

[snip]

Quote:
>    }

You should always enable warnings when developing Perl code.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 23 Nov 2005 03:26:08 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:

> it's not working.

Hmmph.

Quote:

> {

>    my $HostName=$ARGV[0];

Your code never refers to that variable after this point. Why not?

Quote:
>    open FH_NAME, "$_.txt";

You should always, yes *always*, check the return value from open():

   open FH_NAME, "$_.txt" or die "could not open '$_.txt'  $!";

You don't need to read the entire file into memory, and you don't
need the temporary array variable:

   while ( $Machine = <FH_NAME> )

Quote:
>            $Machine =~ tr/a-z/A-Z/;

That does not respect locales.

Use the operator for replacing chars when you want to replace chars.

Use the operator for changing case when you want to change case.  :-)

   $Machine = uc $Machine;

"unless" is the same as "if not", and you don't need the block:


(just a point of style, but excessive punctuation gets in the way of
 clear communication, so I try to avoid punctuation.
)

Quote:
>                    next if (!grep {/S-1-5-21/} $SID);

Why are you grep()ing a one-element list?

   next unless $SID =~ /S-1-5-21/;

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 23 Nov 2005 03:23:42 GMT  
 Critique my code - need a bit of help (Perl for Win32)

Quote:



> "unless" is the same as "if not", and you don't need the block:

But it does aid readability.

Quote:

> (just a point of style, but excessive punctuation gets in the way of
>  clear communication, so I try to avoid punctuation.
> )

Just a point of style, but excessive avoidance of punctuation also
gets in the way of clear communication, so I try to avoid saving a few
puctionation marks at the expense of clear communication.


The first argument to grep is code.  Even if you use the syntax that
doesn't make this explicit, the first argument is still code to pass
to grep not an expression to be evaluated.  I like form to follow
function so I always use the explicit block syntax.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Sun, 27 Nov 2005 19:00:22 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Bit of help needed..

2. Shopping Cart Code Critique

3. Need a bit of help with LWP

4. code critique: loops, references, & usage()

5. Critique My Code

6. Shopping Cart Code Critique

7. Please critique this code

8. Critique My Code! (Please)

9. Shopping Cart Code Critique

10. Need help with perl code

11. Newbie needs help debugging code called from perl script

12. Need Help C code -> Perl

 

 
Powered by phpBB® Forum Software