Rename computer domain (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 renames a computer in its domain and on the computer itself.
# This script works only against Windows XP and Windows Server 2003 computers.
# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------

use Win32::OLE qw(in);
$Win32::OLE::Warn = 3;

# ------ SCRIPT CONFIGURATION ------
$strComputer = '<ComputerName>'; # e.g. joe-xp
$strNewComputer = '<NewComputerName>'; # e.g. joe-pc
$strDomainUser = '<DomainUserUPN>'; # e.g. administrator@rallencorp.com
$strDomainPasswd = '<DomainUserPasswd>';
$strLocalUser = '<ComputerAdminUser>'; # e.g. joe-xp\administrator
$strLocalPasswd = '<ComputerAdminPasswd>';
# ------ END CONFIGURATION ---------
# 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 . '\'');
# Rename Computer
$intRC = $objWMICompSys->Rename($strNewComputer, $strDomainPasswd, $strDomainUser);
if ($intRC != 0) {
    print 'Rename failed with error: ' . $intRC, "\n";
}
else {
    print "Successfully renamed $strComputer to $strNewComputer\n";
}

print "Rebooting system...\n";
$colOS = $objWMIComp->InstancesOf('Win32_OperatingSystem');
foreach my $objOS (in $colOS) {
    $objOS->Reboot();
}

This code has been viewed 1021 times.

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