#  ADSIDumpAccount.pl
#  Example 9.6:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script displays the configuration for a user, computer or group.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32;
use Win32::OLE;

foreach my $Account ( @ARGV )
{
  my( undef, 
    $fMachine, 
    $Domain, 
    $AccountName ) = ( $Account =~ /((\\\\)?(.*)\\)?(.*)?$/ );
  my $Class = ( "" eq $fMachine )? "domain" : "computer";
  $Domain = Win32::DomainName() if( "" eq $Domain );
  if( my $AD = Win32::OLE->GetObject( "WinNT://$Domain,$Class" ) )
  {
    my $User = $AD->GetObject( "", $AccountName ) || next;
    my $PropertyList = GetProperties( $User );
  
    print "$User->{Name}\n";
    foreach my $Property ( sort( @{$PropertyList} ) )
    {
      next if( "" eq $Property );
      print "\t $Property: $User->{$Property}\n";
    }
  }
}

sub GetProperties
{
  my( $Obj ) = @_;
  my @Properties;
  
  if( my $Schema = Win32::OLE->GetObject( $Obj->{Schema} ) )
  {
    foreach my $PropList ( $Schema->{MandatoryProperties},
                           $Schema->{OptionalProperties} )
    {
      push( @Properties, @{$PropList} ) if( defined $PropList );
    }
  }
  return( \@Properties );
}