We help IT Professionals succeed at work.

Help with Script File to Rename Computer on Active Directory and on Local Host

410 Views
Last Modified: 2010-04-21
I found this script online but I am unable to run it. Can someone test and let me know what I may be doing wrong?
I have this script in a file named RNcomputer.vbs on my Desktop (for testing) and call it by Start | Run
CSCRIPT "%userprofile%\desktop\RNcomputer.vbs" which opens a cmd window but then closes shortly afterwards without completing the script. When I run it with WSCRIPT then I get an error on line 23, char. 1, "The RPC server is unavailable."

Here is the Code:


' This VBScript code renames a computer in AD and on the host itself.
 
' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------
 
' ------ SCRIPT CONFIGURATION ------
                                         ' These examples weren't commented properly in the book:
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
'###########################
set objWMILocator = CreateObject("WbemScripting.SWbemLocator")
objWMILocator.Security_.AuthenticationLevel = 6
set objWMIComputer = objWMILocator.ConnectServer(strComputer,  _
           		                         "root\cimv2", _
                                                  strLocalUser, _
                                                  strLocalPasswd)
set objWMIComputerSystem = objWMIComputer.Get( _
                               "Win32_ComputerSystem.Name='" & _
                               strComputer & "'")
'###########################
' Rename Computer
'###########################
rc = objWMIComputerSystem.Rename(strNewComputer, _
                                 strDomainPasswd, _
                                 strDomainUser)
if rc <> 0 then
    WScript.Echo "Rename failed with error: " & rc
else
    WScript.Echo "Successfully renamed " & strComputer & " to " & _
                 strNewComputer
end if
 
WScript.Echo "Rebooting..."
set objWSHShell = WScript.CreateObject("WScript.Shell")
 
' This line was in the book:
'   objWSHShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2"
' When it should be this:
objWSHShell.Run "shutdown.exe /r"
' The SHExitWindowsEx entry point is only available on Win 95/98

Open in new window

Comment
Watch Question

Wow... there are several things you could be running into here.

Windows Firewall issues, DCom permissions, WMI functionality, maybe even issues with Vista UAC, if this is a Vista PC.

Here's an article link that should help you narrow it down.  If you can't get it solved from there, let me know what the specific problem you've narrowed down is and hopefully I can help you through it.

http://msdn.microsoft.com/en-us/library/aa389290(VS.85).aspx
Leon FesterSenior Solutions Architect

Commented:
Have a look at lines 10 - 15.

You need to replace all the items inbetween '<' and '>' with the correct details for your machine, domain  and login credentials.

Author

Commented:
Thanks I had completely overlooked that for some reason. Now how about getting the script to prompt for these via questions?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Phil, did you get things to work now?

Author

Commented:
It works like a charm. Thanks for your help as well as the reference link for further info!
Leon FesterSenior Solutions Architect

Commented:
The accepted answer doesn't answer the original question asked.
But I suppose those are you points to give away.

Author

Commented:
Yeah, I am new to this and only wanted to award partial points, but had not recieved an answer for the original question. I could still use a Simplified script or batch to perform the computer rename task. I don't know if I can setup a new question or repost or not.

Author

Commented:
Can I some how re-open this Question?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.