Renaming a Win32 Computer in Perl
I can’t take credit for the original write up of this snippet, but sadly it has also been so long that I can’t recall exactly where I got it (I know it was on the web). Basically what this code will do, assuming you accepted some type of input earlier in your script, is rename an XP computer (whether on the domain or not) and force a reboot.
I’ve only worked with it on XP Pro platforms, so YMMV.
# Connect to Computer
$objWMILocator = Win32::OLE->new(’WbemScripting.SWbemLocator’);
$objWMILocator->Security_->{AuthenticationLevel} = 6;
$objWMIComp = $objWMILocator->ConnectServer($strComputer, ‘root\\cimv2′);
my $objWMICompSys = $objWMIComp->Get(’Win32_ComputerSystem.Name=\” . $strComputer . ‘\”);# Rename Computer
$intRC = $objWMICompSys->Rename($strNewComputer, $strLocalPasswd, $strLocalUser);
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) {
Win32::InitiateSystemShutdown( ”, “\nAction Complete.\n\nSystem will now Reboot\!”, 10, 0, 1 );
}
Leave a comment
You must be logged in to post a comment.