#  SetLogon.pl
#  Example 3.15:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script sets the AutoLogon feature for Win2k/WinNT.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Getopt::Long;
use Win32::Registry;

$PATH = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon";
Configure( \%Config );
if( $Config{help} )
{
  Syntax();
  exit();
}

my $Root = $HKEY_LOCAL_MACHINE;
my $Key;

if( "" ne $Config{machine} )
{
  print "Connecting to $Config{machine}...\n";
  if( ! $Root->Connect( $Config{machine}, $Root ) )
  {
    print "Can not connect to $Machine\n";
    exit();
  }
}

if( $Root->Create( $PATH, $Key ) )
{
  foreach my $Param ( keys( %{$Config{params}} ) )
  {
    my $Result = $Key->SetValueEx( $Param, 
                                   0, 
                                   REG_SZ, 
                                   $Config{params}->{$Param} );
    print "Setting $Param to '$Config{params}->{$Param}': ", 
          ( $Result )? "success":"failure", "\n";
  }
  $Key->Close();
}
else
{
  print "Unable to open the $PATH key.\n";
}

if( "" ne $Config{machine} )
{
  $Root->Close();
}

sub Configure
{
  my( $Config ) = @_;
  my $Result;
  my $Params = {
    DefaultDomainName => "",
    DefaultUserName => "",
    DefaultPassword => "",
    AutoAdminLogon => 0,
  };

  Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
  $Result = GetOptions( $Config, 
                        qw(
                          machine|m=s
                          clear|c
                          help|?|h
                        )
  );
  $Config->{help} = 1 unless( $Result );
  
  if( ! $Config->{clear} )
  {
    $Params->{DefaultDomainName} = shift @ARGV;
    if( "" ne $Params->{DefaultDomainName} )
    {
      ( $Params->{DefaultDomainName}, 
        $Params->{DefaultUserName} ) = ( $Params->{DefaultDomainName} 
                                          =~ /((.*)\\)?(.*)$/ )[1..3];
    }
    $Params->{DefaultPassword} = shift @ARGV;
    $Params->{AutoAdminLogon} = 1;
  }
  else
  {
    $Params->{AutoAdminLogon} = 0;
  }
  if( ( "" eq $Params->{DefaultUserName} )
      && ( ! $Config->{clear} ) )
  {
    $Config->{help} = 1;
  }
  $Config->{params} = $Params;
  return;
}

sub Syntax
{
  my( $Script ) = ( $0 =~ m#([^\\/]+)$# );
  my $Line = "-" x length( $Script );
  my $WhiteSpace = " " x length( $Script );
  print << "EOT";

  $Script
  $Line
  This utility will set the default logon information such as 
  Userid, Domain, Password and whether or not the computer 
  automatically logs on the default user.
  
  Syntax: $Script [-m Machine] <Domain\\User|User> <Password>
          $WhiteSpace -clear [-m Machine]

  User............The userid to autologon as
  Domain..........The domain of the autologon userid
  Password........The autologon userid's password
  -m Machine......Specifies what machine to set/unset.
  -clear..........Remove the domain, username and password
  
EOT
}
