All pastes #811556 Raw Edit

setup_user.pl

public text v1 · immutable
·published 2007-12-11 01:04 UTC
rendered paste body
use strict;
use File::Path;

print "User Setup Script - Coded by Justin.\n",
      "What would you like to do?\n  -restore everything\t[e]",
      "\n  -restore network\t[n]\n  -restore settings\t[s]\n",
      "Your choice [e/n/s]: ";

do_network(do_settings(in()));
      
sub do_settings {
  return shift unless $_[0] =~ /^(e|s)$/i;
  print "\n   +-----------------+\n   | BIG FAT WARNING |",
        "\n   +-----------------+\n\nThis will remove all:\n",
        "  - custom start menu shortcuts & directories\n",
        "  - custom desktop shortcuts\n",
        "  - custom firefox extentions & settings\n",
        "  - custom quick launch shortcuts\n\n",
        "Continue? [y/n]: ";
  exit 0 unless in() =~ /^\s*y(es)?/i;

  gohome();

# 1st clear start menu & desktop
  clean_out('Start Menu','Programs');
  clean_out('Start Menu/Programs','Accessories|Startup');
  clean_out('Start Menu/Programs/Accessories','\/');
  clean_out('Start Menu/Programs/Startup','\/');
  clean_out('Desktop','\/');

#new quick launch
  replace('Quick Launch',
          'Application Data/Microsoft/Internet Explorer/Quick Launch',
          'C:\Documents and Settings\All Users\Quick Launch',
          'Application Data\Microsoft\Internet Explorer\Quick Launch',
          '/Q /V /H',
  );

#new firefox
  replace('Firefox Data',
          'Application Data/Mozilla',
          'C:\Documents and Settings\All Users\Application Data\Mozilla',
          'Application Data\Mozilla',
          '/Q /V /I /E /H',
  );
  print "did stuff\n";
  return shift;
}

sub do_network {
  return shift unless $_[0] =~ /^(e|n)$/i;
  use Win32::NetAdmin qw(LocalGroupIsMember);

  my $me = $ENV{USERNAME};
  my %groups;

  chdir('C:/Documents and Settings/All Users/Documents/');
  open(GROUPS,"groups.dat") or die "$!";
  while (<GROUPS>) {
    chomp;
    my ($drive,$host,$share,$group) = split(/\s*,\s*/,$_,4);
    $group = 0 if $group eq '!null';
    $groups{$group}->{$drive} = [$host,$share] if $group;
  }
  close(GROUPS) or print "Couldn't close groups.dat....\n";

  foreach my $key (keys %groups) {
    next if $key eq 'users';
    if (LocalGroupIsMember('',$key,$me)) {
      print "\u$me is a member of $key, mapping drives:";
#      print join "\n  ", map {
#        "\u$_: -> \\\\" . join('\\',@{$groups{$key}->{$_}});
#      } keys %{$groups{$key}};
      foreach my $drive (keys %{$groups{$key}}) {
	my $path = '\\\\' . join('\\',@{$groups{$key}->{$drive}});
	print "\n  \u$drive: -> $path";
	system(qq(net use $drive: /DELETE));
	system(qq(net use $drive: $path /PERSISTENT:YES));
      }
      print "\r\n";
    } else {
      print "\u$me is not a member of $key, skipping drives.\n";
    }
  }
  
  print "Adding default mappings:\n  ";
  foreach my $drive (keys %{$groups{'users'}}) {
    my $path = '\\\\' . join('\\',@{$groups{'users'}->{$drive}});
    print "\n  \u$drive: -> $path \n";
    system(qq(net use $drive: /DELETE));
    system(qq(net use $drive: $path /PERSISTENT:YES));
  }
}

# finish
print "\nWork complete. (Enter to quit.) ";
in();

#subs

sub in {
  chomp(my $in = <STDIN>);
  return $in or 0;
}

sub gohome {
  chdir('c:/Documents and Settings/' . $ENV{USERNAME}) or
    die "Can't access user's homedir.";
}

sub replace {
  print 'Trying to sync ', shift, "...\n";
  my ($clear,$from,$to,$opt) =
     (shift,shift,shift,shift);

  clean_out($clear,'\/');
  system(qq(XCOPY "$from" "$to" $opt));
}

sub clean_out {
  print "Entering $_[0].\n";
  chdir shift or die "$!";
  my $gex = shift;
  my $sgex = $gex;
  $sgex = 'N/A' if $gex eq '\/';

  print "Removing all shortcuts.\n";
  unlink(glob('*.lnk'));

  my $glob;
  print "Removing all un-listed directories.\n",
    'Directory save list: ',
    join(', ',split(/\|/,$sgex)), "\n";
  while($glob = glob('*')) {
    print "Removed $glob directory.\n" and
    rmtree($glob) or
    die "$!" unless $glob =~ /$gex/i;
  }
  gohome();
}