SteveS832001 has asked for the wisdom of the Perl Monks concerning the following question:

This program does 4 things to remote computers, you can either create a text file will a list of computer names or enter the computer name yourself. I also have a error file that it is suppose to print to when something fails. It does this when you enter the computer name yourself but when you use the txt file it only does the first one.

use Tk; use Tk::Dialog; use Win32; use Win32::NetAdmin; use Win32::AdminMisc; my @details; my $NewGuestUser = "DisabledGuestAccount"; my $Guestuser = "Guest"; my $Adminuser = "Administrator"; my $NewAdminUser = "testadmin"; my $Guest; my $Admin; my $AdminPass; my $server; my @computers; my $mw = MainWindow->new; my $c = $mw->Canvas(-width => 570, -height => 450); $c->pack; sub trans { open (OUTPUT, ">Error.txt"); if ($server =~ m/txt/) { open(DAT, $server) || die("Could not open file!"); @computers=<DAT>; close(DAT); foreach $computer (@computers) { chop($computer); } } else { @computers = $server; print OUTPUT "$server\n"; } foreach $computer (@computers) { print OUTPUT "$computer\n"; $text->insert ("end", "$computer\n"); if ($CheckRenameAdmin) { AdminRename(); $text->insert ("end", "$comments"); } if ($CheckAdminPassword) { AdminPasswordSet(); $text->insert ("end", "$comments"); } if ($CheckRenameGuest) { GuestRename(); $text->insert ("end", "$comments"); } if ($CheckDisableGuest) { GuestDisable(); $text->insert ("end", "$comments"); } $comments = "==========================================\n"; print OUTPUT "==========================================\n"; $text->insert ("end", "$comments"); close(OUTPUT); } } ###################################################################### +###################################### sub quit { $d = $mw->Dialog( -text => "Are you sure you want to quit?", -buttons => ["Yes", "No"] ); $answer = $d->Show(); exit if ($answer eq "Yes") } ###################################################################### +###################################### sub GuestRename { $Guest = Win32::AdminMisc::RenameUser($server, $Guestuser, $NewGue +stUser); #print OUTPUT Win32::AdminMisc::GetError(); if ($Guest == 1) { $comments = "The user $Guestuser was changed to $NewGuestUser\ +n"; } else { $comments = "The user $Guestuser was not changed to $NewGuestU +ser\n"; print OUTPUT "The user $Guestuser was not changed to $NewGues +tUser\n"; } } ###################################################################### +###################################### sub GuestDisable { $DisableGuest1 = Win32::AdminMisc::UserSetMiscAttributes($server, $New +GuestUser, USER_FLAGS => UF_ACCOUNTDISABLE | UF_PASSWD_CANT_CHANGE | UF_DONT_ +EXPIRE_PASSWD); #print OUTPUT Win32::AdminMisc::GetError(); if ($DisableGuest1 == 1) { $comments = "The Guest has been disabled\n"; } else { $comments = "The Guest account was not disabled\n"; print OUTPUT "The Guest Acount was not disabled\n"; } } ###################################################################### +###################################### sub AdminRename { $Admin = Win32::AdminMisc::RenameUser($server, $Adminuser, $NewAdm +inUser); #print OUTPUT Win32::AdminMisc::GetError(); if ($Admin == 1) { $comments = "$Adminuser has been renamed to $NewAdminUser\n"; } else { $comments = "$Adminuser was not renamed to $NewAdminUser\n"; print OUTPUT "$Adminuser was not renamed to $NewAdminUser\n"; } } ###################################################################### +#################################### sub AdminPasswordSet { $AdminPassP = Win32::AdminMisc::SetPassword($server, $NewAdminUser +, $AdminPass); #print OUTPUT Win32::AdminMisc::GetError(); if ($AdminPassP == 1) { $comments = "Admin password has been set\n"; } else { $comments = "Admin password set has failed\n"; print OUTPUT "Admin password set has failed\n"; } } ###################################################################### +##################################### $mw->title ("Admin Tools"); $text = $mw->Scrolled('Text', -scrollbars => 'so'); $text->insert ("end", "Details\n"); $lab = $mw->Label( -text => 'Select your choices'); $quit = $mw->Button( -text => "Quit", -command => \&quit); $run = $mw->Button( -text => "Run", -command => \&trans); $adminrename = $mw->Checkbutton( -text => "Rename Admin Account", -variable => \$CheckRenameAdmin ); $adminSetPass = $mw->Checkbutton( -text => "Set Admin Password", -variable => \$CheckAdminPassword ); $Guestrename = $mw->Checkbutton( -text => "Rename Guest Account", -variable => \$CheckRenameGuest ); $GuestDisable = $mw->Checkbutton( -text => "Disable Guest Account", -variable => \$CheckDisableGuest ); $Servername = $mw->Entry( -textvariable => \$server ); $Adminpass = $mw->Entry( -textvariable => \$AdminPass ); $TextAdminPass = $mw->Label( -text => "New Admin Password:"); $TextServerName = $mw->Label( -text => "Enter server or .txt file"); $adminrename->place(-x =>0, -y => 15); $Guestrename->place(-x =>0, -y => 32); $adminSetPass->place(-x =>0, -y => 49); $GuestDisable->place(-x =>0, -y => 64); $text->place(-x => 4, -y => 89); $quit->place(-x => 540, -y => 47); $run->place(-x => 500, -y => 47); $lab->place(-x => 0, -y => 0); $Servername->place(-x => 180, -y => 55); $Adminpass->place(-x => 320, -y => 55); $TextAdminPass->place(-x => 327, -y => 32); $TextServerName->place(-x => 187, -y => 32); MainLoop;

2006-03-21 Retitled by planetscape, as per Monastery guidelines
Original title: 'Help'

Replies are listed 'Best First'.
Re: Help with error output
by GrandFather (Saint) on Mar 20, 2006 at 22:54 UTC

    Place:

    use strict; use warnings;

    at the start of your script. Clean up the errors. Come back when you have done that and if you still have problems.

    If you come back, show us 10 lines of code (that run as a complete script) that demonstrate the problem. Show us what you expect and what you get (use print for output to STDOUT).

    If you still have a problem after that process I owe you a chocolate bar, otherwise you owe me one. :)


    DWIM is Perl's answer to Gödel