softworkz has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I've been in the windoze prison for some time now and I'm looking to escape! I have been forced to create a VBscript that creates AD accounts and the quickest way I could find a solution was to use VBscript (hence the windoze prison). Is there a way to do the same thing but in Perl instead? I only included the code that actually creates the accounts (this is the piece I'd rather have in Perl), any examples would be helpful, thanks!!
'------------------------------------------------------ ' Add piece to create users here Const ADS_PROPERTY_UPDATE = 2 Const ADS_PROPERTY_APPEND = 3 'Bind to OU Where new User will be placed (domain users) '----------------------------- Set objOU = GetObject("LDAP://OU=Domain Users,DC=HEADQUARTERS,DC=YOURC +OMPANY,DC=com") 'Create New User and set specific information '-------------------------------------------------- Set objUser = objOU.Create("User","cn=" & strName) objUser.Put "sAMAccountName", strNetId objUser.SetInfo objUser.AccountDisabled = False objUser.SetInfo objUser.SetPassword strPass objUser.Put "mail", strNetId & "" objUser.Put "givenName",strFName objUser.Put "sn", strLName objUser.Put "displayName",strName objUser.Put "userPrincipalName",strNetId & "" objUser.SetInfo ' Add that user to their respective global security group Set objGroup = GetObject("LDAP://cn=" & strUserDir & ",OU=Domain Users +,DC=HEADQUARTERS,DC=YOURCOMPANY,DC=com") objGroup.PutEx ADS_PROPERTY_APPEND, "member", _ Array("cn=" & strName & ",ou=domain users,dc=HEADQUARTERS,dc=YOURC +OMPANY,dc=com") objGroup.SetInfo ' Add that user to general users group Set objGroup = GetObject("LDAP://cn=General,cn=users,DC=HEADQUARTERS,D +C=YOURCOMPANY,DC=com") objGroup.PutEx ADS_PROPERTY_APPEND, "member", _ Array("cn=" & strName & ",ou=Domain Users,dc=HEADQUARTERS,dc=YOURC +OMPANY,dc=com") objGroup.SetInfo '--------------------------------------------------

Replies are listed 'Best First'.
Re: convert VBscript to Perl
by davidrw (Prior) on Apr 22, 2005 at 12:57 UTC
    I haven't used it personally, but from reading your vbscript code it looks like you could replace it with Net::LDAP.
Re: convert VBscript to Perl
by NetWallah (Canon) on Apr 23, 2005 at 00:36 UTC
    I downloaded this code last year - can't remember where I got it, and I haven't used it but this may accomplish what you want:
    use strict; use Net::LDAP; #-----------------------------------------------# # Customize for your own environment, # connect and authenticate #-----------------------------------------------# my $dc = 'dc1'; my $user = ''; my $passwd = 'Adminpasswd'; my $dn = "cn=jdoe,cn=users,dc=mycorp,dc=com"; my $ldap = Net::LDAP->new($dc) or die "$@\n"; my $rc = $ldap->bind( $user, password => $passwd); die $rc->error if $rc->code; #----------------------------------------------# # Modify several attributes #---------------------------------------------# print "Setting givenname, sn and mail...\n"; $rc = $ldap->modify($dn, changes => [ add => [ givenname => "Johnny" ], add => [ sn => "Doh"], add => [ mail => ''], ]); die $rc->error if $rc->code; print "Changing givenname to John...\n"; $rc = $ldap->modify($dn, replace => { givenname => "John" }); die $rc->error if $rc->code; print "Deleting the mail attribute...\n"; $rc = $ldap->modify($dn, delete => [ 'mail' ]); die $rc->error if $rc->code; print "Setting the telephoneNumber and sn...\n"; $rc = $ldap->modify($dn, changes => [ add => [ telephoneNumber => '555-123-456 +7'], replace => [ sn => 'Doe'], ]); die $rc->error if $rc->code; print "\nModifications successful\n"; $ldap->unbind;

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein

      ++ for the help!

      I figured out how to do this via Perl, below is the code
      #!/usr/bin/perl -w use strict; use Win32::OLE; $Win32::OLE::Warn = 3; # Taken from $ADS_USER_FLAG_ENUM my $ADS_UF_NORMAL_ACCOUNT = 512; my $ADS_PROPERTY_APPEND = 3; my $NetId = "zeta28"; my $Name = "Zeta Jones"; my $FName = "Zeta"; my $LName = "Jones"; my $UserDir = "user12"; my $Pass = "S0mePass!"; my $objParent = Win32::OLE->GetObject("LDAP://OU=Domain Users, DC=HEAD +QUARTERS,DC=YOURCOMPANY,DC=com"); my $objUser = $objParent->Create("user", "cn=$Name"); $objUser->Put("sAMAccountName", "$NetId"); $objUser->SetInfo; $objUser->{AccountDisabled} = 0; $objUser->SetInfo; $objUser->SetPassword("$Pass"); $objUser->Put("mail", "$NetId".'@YOURCOMPANY'); $objUser->Put("givenName", "$FName"); $objUser->Put("sn", "$LName"); $objUser->Put("displayName", "$Name"); $objUser->Put("userPrincipalName", "$NetId".'@YOURCOMPANY'); $objUser->SetInfo; # add that user to the user12 global security group my $objGroup = Win32::OLE->GetObject("LDAP://CN=$UserDir, OU=Domain Us +ers, DC=HEADQUARTERS,DC=YOURCOMPANY,DC=com"); $objGroup->PutEx ($ADS_PROPERTY_APPEND, "member", ["cn=$Name,ou=domain + users, dc=HEADQUARTERS, dc=YOURCOMPANY, dc=com"]); $objGroup->SetInfo; # add that user to the general users group $objGroup = Win32::OLE->GetObject("LDAP://CN=General, CN=Users, DC=HEA +DQUARTERS,DC=YOURCOMPANY,DC=com"); $objGroup->PutEx ($ADS_PROPERTY_APPEND, "member", ["cn=$Name,ou=domain + users, dc=HEADQUARTERS, dc=YOURCOMPANY, dc=com"]); $objGroup->SetInfo;
Re: convert VBscript to Perl
by nimdokk (Vicar) on Apr 22, 2005 at 12:59 UTC
    Only conversion from VBscript to Perl I've done is from a script that used cdonts (or something) to send email. You might take a look at this. It doesn't look like its got an exact match for what you are looking for, but it might point you in the right direction. You might want to look at some of the Win32 modules.