
################################################################################
#####   GetUsers_All.pl -- Gets a list of Domain Users and Descriptions    #####
#####                 and outputs the result to a .csv file                #####
#####         by tojo2000 with thanks to perl-ntadmins@topica.com          #####
################################################################################

#####  Get the username of the user

print "\n\n\nThis script will retrieve the attributes of all domain users.\n";
print "\nProcessing";

#####  Prepare output file

open(OUTFILE,">GetUsers_All.csv");

#####  Print headers

print OUTFILE "USER,";
print OUTFILE "FULL NAME,";
print OUTFILE "COMMENT,";
print OUTFILE "PWD EXPIRES,";
print OUTFILE "PRIVELEGE,";
print OUTFILE "STATUS,";
print OUTFILE "SCRIPT,";
print OUTFILE "LAST LOGON,";
print OUTFILE "LAST LOGOFF\n";

#####  Declare modules and retrieve user list

use Win32::NetAdmin;
use Win32API::Net;
Win32::NetAdmin::GetDomainController("",$domain,$server);
Win32::NetAdmin::GetUsers($server,"",\@users);

@sort_users = sort @users; ##Sort result
foreach $user(@sort_users){
	if(!($user =~ /\$$/)){
		push @domainusers,$user;
	}
}

#####  Retrieve raw user data and populate attribute array

foreach $user(@domainusers){ ##Code from here to end of script iterates once for each user

Win32API::Net::UserGetInfo($server,$user,3,\%user);
print ".";

$attrib[0] = $user;

$user{fullName} =~ s/,/./g; ##Remove commas
$attrib[1] = $user{fullName};

$attrib[2] = $user{passwordAge}; ##Convert seconds to hours

$attrib[3] = $user{priv};

$user{comment} =~ s/,/./g; ##Remove commas
$attrib[4] = $user{comment};

$attrib[5] = $user{flags};
$attrib[6] = $user{scriptPath};

#####  Format Date so Excel won't consider it a string value

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($user{lastLogon});
$attrib[7] = ($mon + 1)."-".$mday."-".($year + 1900);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($user{lastLogoff});
$attrib[8] = ($mon + 1)."-".$mday."-".($year + 1900);

if($flags < 65535 && ($user{flags} - UF_DONT_EXPIRE_PASSWD) < 0 && $attrib[2] != 0){ ##(Unless password does not expire)
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime((120*86400 - $attrib[2]) + time);
$attrib[2] = ($mon + 1)."-".$mday."-".($year + 1900);
}elsif($attrib[2] == 0){
	$attrib[2] = "EXPIRED";
	}else{
		$attrib[2] = "n/a";
	}



#####  Print processed data to file.

print OUTFILE "$attrib[0],";
print OUTFILE "$attrib[1],";
print OUTFILE "$attrib[4],";
print OUTFILE "$attrib[2],";

#####  Check Privilege level

if($attrib[3]==USER_PRIV_USER){
	print OUTFILE "USER,";
	}elsif($attrib[3]==USER_PRIV_ADMIN){
		print OUTFILE "ADMIN,";
		}elsif($attrib[3]==USER_PRIV_GUEST){
			print OUTFILE "GUEST,";
		}
		
	#####  Check for flags
		
	$total=$user{flags};
		
	if(($total - UF_DONT_EXPIRE_PASSWD) >= 0){
		print OUTFILE "NO PWD EXPIRE.";
		$total = $total - UF_DONT_EXPIRE_PASSWD;
	}
if(($total - UF_SERVER_TRUST_ACCOUNT) >= 0){
	$total = $total - UF_SERVER_TRUST_ACCOUNT;
}
if(($total - UF_WORKSTATION_TRUST_ACCOUNT) >= 0){
	$total = $total - UF_WORKSTATION_TRUST_ACCOUNT;
}
if(($total - UF_INTERDOMAIN_TRUST_ACCOUNT) >= 0){
	$total = $total - UF_INTERDOMAIN_TRUST_ACCOUNT;
}
if(($total - UF_NORMAL_ACCOUNT) >= 0){
	$total = $total - UF_NORMAL_ACCOUNT;
}
if(($total - UF_TEMP_DUPLICATE_ACCOUNT) >= 0){
	$total = $total - UF_TEMP_DUPLICATE_ACCOUNT;
}
if(($total - UF_PASSWD_CANT_CHANGE) >= 0){
	print OUTFILE "CAN'T CHNG PWD.";
	$total = $total - UF_PASSWD_CANT_CHANGE;
}
if(($total - UF_PASSWD_NOTREQD) >= 0){
	print OUTFILE "BLANK PWD OK.";
	$total = $total - UF_PASSWD_NOTREQD;
}
if(($total - UF_LOCKOUT) >= 0){
	print OUTFILE "LOCKED OUT.";
	$total = $total - UF_LOCKOUT;
}
if(($total - UF_HOMEDIR_REQUIRED) >= 0){
	print OUTFILE "HOMEDIR REQUIRED.";
	$total = $total - UF_HOMEDIR_REQUIRED;
}
if(($total - UF_ACCOUNTDISABLE) >= 0){
	print OUTFILE "DISABLED.";
	$total = $total - UF_ACCOUNTDISABLE;
}
if (($total - UF_SCRIPT) >= 0){
	$total = $total - UF_SCRIPT;
}
if($user{passwordExpired} == 1){
	print OUTFILE "MUST CHNG PWD.";
}
print OUTFILE ",";

#####  Finish attributes

print OUTFILE "$attrib[6],";

if($attrib[7] ne "12\/31\/1969"){
	print OUTFILE "$attrib[7],";
	}else{
		print OUTFILE "n\/a,";
	}
if($attrib[8] ne "12\/31\/1969"){
	print OUTFILE "$attrib[8],";
	}else{
		print OUTFILE "n\/a,";
	}
print OUTFILE "\n";
}
