Join computer (Perl)

This code can be found in Chapter 2 of Windows Server Cookbook

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.

# This code joins a server to a domain.
# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------

use Win32::OLE;
$Win32::OLE::Warn = 3;

# ------ SCRIPT CONFIGURATION ------
$strComputer = '<ComputerName>';# e.g. joe-xp
$strDomain = '<DomainName>';# e.g. rallencorp.com
$strDomainUser = '<DomainUserUPN>';# e.g. administrator@rallencorp.com
$strDomainPasswd = '<DomainUserPasswd>';
$strLocalUser = '<ComputerAdminUser>';# e.g. administrator
$strLocalPasswd = '<ComputerUserPasswd>';
# ------ END CONFIGURATION ---------
# Constants
use constant JOIN_DOMAIN => 1;
use constant ACCT_CREATE => 2;
use constant ACCT_DELETE => 4;
use constant WIN9X_UPGRADE => 16;
use constant DOMAIN_JOIN_IF_JOINED => 32;
use constant JOIN_UNSECURE => 64;
use constant MACHINE_PASSWORD_PASSED => 128;
use constant DEFERRED_SPN_SET => 256;
use constant INSTALL_INVOCATION => 262144;

# Connect to Computer
$objWMILocator = Win32::OLE->new('WbemScripting.SWbemLocator');
$objWMILocator->Security_->{AuthenticationLevel} = 6;
$objWMIComp = $objWMILocator->ConnectServer($strComputer, 'root\\cimv2', $strLocalUser, $strLocalPasswd);
$objWMICompSys = $objWMIComp->Get('Win32_ComputerSystem.Name=\'' . $strComputer . '\'');
# Join Computer
$intRC = $objWMICompSys->JoinDomainOrWorkGroup($strDomain, $strDomainPasswd, $strDomainUser, "", JOIN_DOMAIN);
if ($intRC != 0) {
    print 'Join failed with error: ' . $rc, "\n";
}
else {
    print "Successfully joined $strComputer to $strDomain\n";
}

This code has been viewed 890 times.

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