Create user - WinNT (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;

# **********************************************************************
# Flag constants. See the later sidebar on "Boolean Arithmetic with
# Hexadecimal Values."
# **********************************************************************
use constant UF_SCRIPT => 0x1;
use constant UF_ACCOUNTDISABLE => 0x2;
use constant UF_LOCKOUT => 0x10;
use constant UF_PASSWD_NOTREQD => 0x20;
use constant UF_PASSWORD_CANT_CHANGE => 0x40;
use constant UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED => 0x80;
use constant UF_DONT_EXPIRE_PASSWD => 0x10000;

my ($objDomain, $objUser, $fso, $intUserFlags, $intNewUserFlags);
my ($fldUserHomedir, $wshShell);

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

# **********************************************************************
# Write the newly created object out from the property cache and read
# all the properties for the object, including the ones set by the
# system on creation
# **********************************************************************
$objUser->SetInfo();
$objUser->GetInfo();

# **********************************************************************
# Set the properties
# **********************************************************************
$objUser->{AccountDisabled} = 0;
$objUser->{AccountExpirationDate} = '02/05/04';
$objUser->{Description} = 'My description goes here!';
$objUser->{FullName} = 'Victoria Launders';
$objUser->{IsAccountLocked} = 0;
$objUser->{LoginScript} = 'login.vbs';
$objUser->{PasswordRequired} = 1;

# **********************************************************************
# Set all the properties for the user and read back the data, including
# any default so that you can set the flags
# **********************************************************************
$objUser->SetInfo();
$objUser->GetInfo();

# **********************************************************************
# Make sure the password never expires and the user can't change it
# **********************************************************************
$intUserFlags = $objUser->Get('userFlags');
$intNewUserFlags = $intUserFlags || UF_DONT_EXPIRE_PASSWD;
$intNewUserFlags = $intNewUserFlags || UF_PASSWORD_CANT_CHANGE;
$objUser->Put('userFlags', $intNewUserFlags);
$objUser->SetInfo();

# **********************************************************************
# Set the password
# **********************************************************************
$objUser->SetPassword('thepassword');

This code has been viewed 1244 times.

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