Create user (Perl)

This code can be found in Chapter 23 of Active Directory, 3rd Edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Active Directory, Third Edition" 
# ISBN: 0-596-10173-2

use Win32::OLE;

use strict;
my ($objDomain, $objUser);
# Creating a user in a Windows NT domain

$objDomain = Win32::OLE->GetObject('WinNT://MYDOMAIN');
$objUser = $objDomain->Create('user', 'vlaunders');
$objUser->SetInfo();

# Creating a local user on a computer or member server
# Valid for Windows NT/2000/2003
$objComputer = Win32::OLE->GetObject('WinNT://MYCOMPUTER,Computer');
$objUser = $objComputer->Create('user', 'vlaunders');
$objUser->SetInfo();

# Creating a user in Active Directory
$objDomain = Win32::OLE->GetObject('LDAP://cn=Users,dc=mycorp,dc=com');
$objUser = $objDomain->Create('user', 'cn=vlaunders');
$objUser->Put('sAMAccountName', 'vlaunders');
$objUser->Put('userPrincipalName', 'vlaunders@mycorp.com');
$objUser->SetInfo();

This code has been viewed 2024 times.

New from the creators of TechTasks.com: StatSheet.com