#TEST THE USE OF NETADMIN AND LANMAN MODULES TO ADD AN
#ACCOUNT TO THE LOCAL ADMINISTRATORS GROUP OF A PC WITH
#A HUNGARIAN OPERATING SYSTEM
#
#THIS SCRIPT WORKS CORRECTLY FOR OTHER OS LANGUAGES:
#   ENGLISH - WORKS (Administrators)
#   SPANISH - WORKS (Administratadores)
#   GERMAN - WORKS (Administratoren)
#   HUNGARIAN - FAILS (Rendszergazdák) WITH 2220 (group name could not be found)
#----------------------------------------------------------------------------
use strict;
use Win32::NetAdmin;
use Win32::Lanman;

#Define variables
#----------------
my %RIDs=('ADMINISTRATORS' => 544,
        'USERS' => 545,
        'GUESTS' => 546,
        'POWER USERS' => 547,
        'ADMINISTRATOR' => 500,
        'GUEST' => 501);
my ($LocalGrp,$RC);
my $GrpToManage = 'ADMINISTRATORS';
my $LogFile = 'C:\test.log';

my $PCName = 'MYPCNAME';
my $AcctForAction = 'MYDOMAIN\MYACCOUNT';

#Get the local group name via the RID
#------------------------------------
Win32::NetAdmin::GetAliasFromRID($PCName, $RIDs{$GrpToManage}, $LocalGrp);
LogText($LogFile, "The local group name is: $LocalGrp");
   #--------------------------------------------------------------------------------
   #NOTE:
   #IF I PUT A BREAKPOINT AT THIS POINT AND LOOK AT THE DEBUG INFORMATION
   #IN KOMODO, I SEE THE CORRECT VALUE (THE HUNGARIAN WORD FOR ADMINISTRATORS)
   #IN THE $LOCALGRP VARIABLE: "Rendszergazdák".
   #
   #ALSO PRINTS CORRECTLY TO THE OUTPUT SCREEN IN KOMODO: "Rendszergazdák".
   #
   #HOWEVER, IN THE LOG FILE IT COMES OUT AS: "RendszergazdÃ¡k".
   #--------------------------------------------------------------------------------

#Add the acctforaction into the localgrp using netadmin module
#-------------------------------------------------------------
if (Win32::NetAdmin::LocalGroupAddUsers($PCName, $LocalGrp, $AcctForAction)){
   LogText($LogFile, "ADD $AcctForAction to $LocalGrp: Successful\n");
}else{
   $^E = $RC = Win32::NetAdmin::GetError();
   if ($RC == 1378){
      LogText($LogFile, "ADD $AcctForAction to $LocalGrp: Successful - $^E\n");
   }
   else{
      my $ErrorText = "ADD $AcctForAction to $LocalGrp: Failed: $RC $^E\n";
      LogText($LogFile, $ErrorText);
   }
}

#Add the acctforaction into the localgrp using lanman module
#-------------------------------------------------------------
if (Win32::Lanman::NetLocalGroupAddMembers($PCName, $LocalGrp, [$AcctForAction])){
   LogText($LogFile, "ADD $AcctForAction to $LocalGrp: Successful\n");
}else{
   $^E = $RC = Win32::Lanman::GetLastError();
   if ($RC == 1378){
      LogText($LogFile, "ADD $AcctForAction to $LocalGrp: Successful - $^E\n");
   }
   else{
      my $ErrorText = "ADD $AcctForAction to $LocalGrp: Failed: $RC $^E\n";
      LogText($LogFile, $ErrorText);
   }
}



#--------------------------
sub LogText{
	my $logfile = shift;
	my $text = shift;
	open (LOG, ">>$logfile") || warn "Could not open $logfile!";
	print LOG "$text\n";
   print "$text\n";
	close LOG;
}
