Dear All

I have this script that obtains the sizes of shares on remote servers. It works fine, however if it encounters an error the script bombs out!.

What I am doing is obtaining the share paths using Lanman by populating an array with the info the NetEnumShare command provides, then I go through every element in that Enumerated share array to size up the shares. If for some reason or another the script can’t access a share, the script fails and I have to start again. If was was working on a massive server with 80Gb shares then that means I have to restart the process again. My question is how can I get the script to continue and get the next element in the share array even if it ecountered an error.

Thanks for your help.

btw: the reason why I put the "opendir" so I don't miss out on any shares if the script can't open....I am thinking maybe I should get rid of it!But I am not sure, so I will wait for your valuable response...Cheers.

AUTOLOAD; $|++; use strict; use Win32; use Time::localtime; use Win32::Lanman; use Win32::NetAdmin; use Win32::OLE qw ( in with ); use Win32::OLE::Const 'Microsoft Excel'; my $app = 'Excel.Application'; my $xls = Win32::OLE -> GetActiveObject ( $app ) || Win32::OLE + -> new ( $app ); die "\nCannot start $app! : $!\n" unless $xls; $xls->{Visible} = 1; $xls -> {SheetInNewWorkbook} = 1; die "\nCannot start $app! : $!\n" unless $xls; my $xls_book = $xls -> Workbooks -> Add ( ); my $sheet = $xls-> Worksheets ( "Sheet1" ); $xls_book ->{Name} ="$ARGV[0]"; $xls -> ActiveWorkbook -> SaveAs( $ARGV[0] ); my $row =2; print "\nAccessing Server: " . "\\\\" . $ARGV[0]; if (Win32::Lanman::NetShareEnum("\\\\"."$ARGV[0]", \ my @shares)) { print " Successfully\n"; my $ShareCount = 0; my $fs = Win32::OLE->CreateObject('Scripting.FileSystemObject'); my $TotalSize=0; for my $Share (@shares) { my $col =1; next if ( ($Share->{path}) =~ /^c/i); next if ( ($Share->{netname})=~ /ipc\$|rpc\$|netlogon\$|admin\ +$|^CDROM|^\w\$/i); my $Target = "\\\\"."$ARGV[0]" . "\\" . $Share->{path}; $Target =~ s/:/\$/; if (opendir (DIR, "$Target")) { print ++$ShareCount . ") $Share->{path}, $Target, $Share +->{netname}\n"; &Prep_Xls; $sheet-> Cells ($row,$col++)-> {value}="$Target"; my $d = $fs->GetFolder("$Target"); my @folders = $fs->GetFolder("$Target" ); my $fCount =0; my $sCount =0; while( @folders ) { my $folder = pop @folders; $fCount += $folder->Files->Count; $sCount += $folder->SubFolders->Count; for my $subFolder ( in $folder->SubFolders ) { $fCount += $subFolder->Files->Count; push @folders, $_ for in $subFolder->SubFolders ; $sCount += $subFolder->SubFolders->Count; } } my $Size = $d->size(); print " ( Size : " . $Size. " Files : $fCount, folders : $ +sCount)\n"; $sheet-> Cells ($row,$col++)-> {value}=$Size; $sheet-> Cells ($row,$col++)-> {value}=$fCount; $sheet-> Cells ($row,$col++)-> {value}=$sCount; $sheet-> Cells ($row++,$col++)-> {value}="$Share->{remark} +"; close (DIR); } else { print "\nError : $Target\n"; } } print "\nTotal number of shares = $ShareCount\n"; $sheet-> Cells (++$row,1)-> {value}="Total number of shares = $Sha +reCount"; } $xls -> ActiveWorkbook -> SaveAs( $ARGV[0] ); undef $xls_book; undef $xls; sub Prep_Xls { $sheet-> Columns ("A:A")->{ColumnWidth} = "55"; $sheet-> Cells(1,1)->Font->{FontStyle}='bold'; $sheet-> Cells(1,1)->{value}='UNC share path'; $sheet-> Columns ("B:B")->{ColumnWidth} = "20"; $sheet-> Cells(1,2)->Font->{FontStyle}='bold'; $sheet-> Cells(1,2)->{value}='Size'; $sheet-> Columns ("C:C")->{ColumnWidth} = "20"; $sheet-> Cells(1,3)->Font->{FontStyle}='bold'; $sheet-> Cells(1,3)->{value}='Num of Files'; $sheet-> Columns ("D:D")->{ColumnWidth} = "20"; $sheet-> Cells(1,4)->Font->{FontStyle}='bold'; $sheet-> Cells(1,4)->{value}='Num of Folders'; $sheet-> Columns ("E:E")->{ColumnWidth} = "20"; $sheet-> Cells(1,5)->Font->{FontStyle}='bold'; $sheet-> Cells(1,5)->{value}='Remarks'; }

janitored by ybiC: Balanced <readmore> tags around long codeblock, as per Monastery convention


In reply to How can I get a Perl script to run even if it encounters an Error? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.