#! c:\perl\bin\perl.exe
#-----------------------------------------------------------
# wksdump.pl
# Dump a list of workstations from the PDC
# uses Win32::Lanman, and looks for workstation trust
# accounts; strips '$' from end of name
#
# Win32::Lanman module available from CPAN:
#   http://www.cpan.org/authors/id/J/JH/JHELBERG/
#
# usage: [perl] wksdump.pl [> wksdump.log]
#
# Redirect output to a file for use with other tools
#-----------------------------------------------------------
use strict;
use Win32::Lanman;

# Keep a count of systems
my $count = 0;
#-----------------------------------------------------------
# Get the PDC of the domain...runs command on the local machine, 
# using the currently logged in domain.
#-----------------------------------------------------------
my $pdc;
if(Win32::Lanman::NetGetDCName("\\\\".Win32::NodeName, Win32::DomainName, 
  \$pdc)) {
# if the command succeeds, you don't need to do anything except strip 
# leading "\\"'s.  This isn't a requirement...it's more of a programming 
# style issue.
	$pdc =~ s/\\//g;
	print "$pdc\n";	
}
else {
	print STDERR "Error in NetGetDCName: ".Win32::FormatMessage 
	  Win32::Lanman::GetLastError."\n";
	  exit 0;
}

#-----------------------------------------------------------
# Dump workstation accounts
#-----------------------------------------------------------
my @users;
if(Win32::Lanman::NetUserEnum("\\\\$pdc", &FILTER_WORKSTATION_TRUST_ACCOUNT, 
  \@users)) {
	foreach my $user (@users) {
		my $sys = ${$user}{'name'};
		$sys =~ s/\$//;
		print "$sys\n";
		$count++;
	}
}
else {
	print STDERR "Error in NetUserEnum: ".Win32::FormatMessage 
	  Win32::Lanman::GetLastError."\n";
	exit 0;
}

print STDERR "There are $count workstations.\n";

