Rename 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 changes the name of a computer.  It does NOT modify 
# the computer's account in the domain if one exists.
# ---------------------------------------------------------------
# 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 = '<CurrentServerName>';
$strNewName = '<NewServerName>';
# ------ END CONFIGURATION ---------
use constant HKLM => 0x80000002;
$strKeyPath = 'System\\CurrentControlSet\\Control\\ComputerName\\ComputerName';
$objReg = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\default:StdRegProv');
$intRC = $objReg->SetStringValue(HKLM, $strKeyPath, 'ComputerName', $strNewName);
if ($intRC != 0) {
    print 'Error setting ComputerName value: ' . $intRC, "\n";
}
else {
    print "Successfully set ComputerName value to $strNewName\n";
}

$strKeyPath = 'System\\CurrentControlSet\\Services\\Tcpip\\Parameters';
$intRC = $objReg->SetStringValue(HKLM, $strKeyPath, 'NV Hostname', $strNewName);
if ($intRC != 0) {
    print 'Error setting NV Hostname value: ' . $intRC, "\n";
}
else {
    print "Successfully set NV Hostname value to $strNewName\n";
}

print "Rebooting system...\n";
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
foreach my $objOS (@{$objWMI->InstancesOf('Win32_OperatingSystem')}) {
    $objOS->Reboot();
}

This code has been viewed 1063 times.

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