Can someone help me change my program to use a recurssive subroutine? Basically the script takes a manger username as argument and find all directs and indirects reports under him?
my (@alldn,@alldn2,@alldn3,@alldn4); my $manager = $ARGV[0]; print "Employees reporting to '$manager' are:\n"; &getDep($manager); sub getDep { my $dmgr = $_[0]; my $RootDSE = Win32::OLE->GetObject("LDAP://RootDSE") || print "<1>ERROR</h1>";; $dc = $RootDSE->Get("DnsHostName"); query_ldap("<LDAP://" . $dc . ">;(&(objectclass=User)(Samaccountname=$dmgr*));displayname,distinguishedname;subtree",$objects); while (!$objects->{EOF}) { getdirectreports($dc,$objects->Fields("distinguishedname")->{Value}); $objects->MoveNext(); } sub query_ldap { my $ldap_query = $_[0]; my $error_num; my $error_name; my $RS; my $Conn = Win32::OLE->new("ADODB.Connection"); if (Win32::OLE->LastError() != 0) { print "Failed creating ADODB.Connection object (".Win32::OLE->LastError().")\n -> $ldap_query\n"; return 0; } $Conn->{'Provider'} = "ADsDSOObject"; if (Win32::OLE->LastError() != 0) { print "Failed setting ADODB.Command Provider (".Win32::OLE->LastError().")\n -> $ldap_query\n"; return 0; } $Conn->{Open} = "Perl Active Directory Query"; my $Cmd = Win32::OLE->new("ADODB.Command"); if (Win32::OLE->LastError() != 0) { print "Failed creating ADODB.Command object (".Win32::OLE->LastError().")\n -> $ldap_query\n"; return 0; } $Cmd->{CommandText} = $ldap_query; $Cmd->{ActiveConnection} = $Conn; $Cmd->{Properties}->{"Page Size"} = 99; $RS = $Cmd->Execute(); if (Win32::OLE->LastError() != 0) { print "Failed Executing ADODB Command object (".Win32::OLE->LastError().")\nExecuting ADODB Command -> $ldap_query\n"; return 0; } else { $_[1] = $RS; return 1; } } sub getdirectreports { my $dc = $_[0]; my $dn = $_[1]; my $adsuser = Win32::OLE->GetObject("LDAP://$dc/$dn") || die ("Can't find user: ".Win32::OLE->LastError()."\n"); foreach my $report ( @{$adsuser->{directReports}}) { push (@alldn,$report); } } &getChild($dc); } sub getChild { my $dc1 = $_[0]; foreach my $a ( @alldn ) { my $child = Win32::OLE->GetObject("LDAP://$dc1/$a") || die ("Can't find user: ".Win32::OLE->LastError()."\n"); print "$child->{DistinguishedName}\n"; foreach my $report ( @{$child->{directReports}}) { push (@alldn2,$report); } } &getGrandChild($dc1); } sub getGrandChild { my $dc2 = $_[0]; foreach $x ( @alldn2) { my $gchild = Win32::OLE->GetObject("LDAP://$dc2/$x") || die ("Can't find user: ".Win32::OLE->LastError()."\n"); print "$gchild->{DistinguishedName}\n"; foreach my $report ( @{$gchild->{directReports}}) { push (@alldn3,$report); } } &getGreatGrandChild($dc2); } sub getGreatGrandChild { my $dc3 = $_[0]; foreach $y ( @alldn3) { my $ggchild = Win32::OLE->GetObject("LDAP://$dc3/$y") || die ("Can't find user: ".Win32::OLE->LastError()."\n"); print "$ggchild->{DistinguishedName}\n"; foreach my $report ( @{$ggchild->{directReports}}) { push (@alldn4,$report); } } &getlastChild($dc3); } sub getlastChild { my $dc3 = $_[0]; foreach $y ( @alldn4) { my $lchild = Win32::OLE->GetObject("LDAP://$dc3/$y") || die ("Can't find user: ".Win32::OLE->LastError()."\n"); print "$lchild->{DistinguishedName}\n"; } }
_______________________________________________ Perl-Win32-Admin mailing list Perl-Win32-Admin@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs