Computing.Net > Forums > Programming > Perl Renaming Computer name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Perl Renaming Computer name

Reply to Message Icon

Name: mongar
Date: December 5, 2006 at 09:54:00 Pacific
OS: win2003
CPU/Ram: 1
Product: hp
Comment:

hi,
i am trying to rename a computer name on win2003server but faile with the error :
"
Rebooting system...
Not an ARRAY reference at C:\Documents and Settings\Administrator\Desktop\1.pl l
ine 53, <STDIN> line 1."
here is the script:

########################################


use Sys::Hostname;

$host = hostname();


#############
# Functions #
#############


sub change_hostname{

print"Enter new computer name:\n";
chomp ($hostname=<STDIN>);


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


$strComputer = $host;
$strNewName = $hostname;

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();
}


}#end of sub change_hostname

&change_hostname;


########################################

please advice
thanks



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: December 5, 2006 at 15:12:29 Pacific
Reply:

What is on line 53 of your script?

Use the strict pragma!

Load your modules at the beginning of the script instead of inside the subroutine. If you load them at the beginning (outside of a sub), they load at compile time instead of run time and any module loading errors will be caught during compilation.

You might find it easier to use the Win32::TieRegistry module instead of Win32::OLE.
http://search.cpan.org/~adamk/Win32...

The Windows Resource Kit has a reboot.exe (or it might be shutdown.exe) utility that will simplify the rebooting.


0

Response Number 2
Name: tonysathre
Date: December 5, 2006 at 16:34:49 Pacific
Reply:

Always start your script with the following lines:

use strict;
use warnings;

Also, when declaring variables use the 'my' keyword:

my $hostname=();

It's not required, but it's good Perl scripting practice.

"Computer security." — Oxymoron


0

Response Number 3
Name: mongar
Date: December 27, 2006 at 12:57:35 Pacific
Reply:

thanks guys for the tips
use strict;
use warnings; and my .

and sorry for the late answer
i have managed by my self to found a way
to do it i am using :
Win32::InitiateSystemShutdown($host,300,0,0,1);

thanks
anyway


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl Renaming Computer name

Change Computer Name www.computing.net/answers/programming/change-computer-name/13422.html

Batch file for renaming folders www.computing.net/answers/programming/batch-file-for-renaming-folders/15430.html

Perl Rename www.computing.net/answers/programming/perl-rename/15367.html