Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Renaming of Computer's Name using VBScript

Posted on 2007-12-06
7
Medium Priority
?
19,202 Views
Last Modified: 2011-06-06
Hi Experts
Current, i got a script that enables me to view the current settings of the computer's name...
But upon display the name, i would like the user to have the option to change the existing computer name or to quit the script. may i know am is it going to be done? is it possible to merge this 2 scripts into one? e.g below

cheers
=====Script=====Displays Computer Name
Option Explicit 
 
Dim Text, Title
Dim WshNetwork         ' Object variable
Text = "Networking information" & vbCrLf & vbCrLf
Title = "Rename Hostname?"
 
' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf
 
MsgBox Text, vbYesNo + vbQuestion, Title
 
=====End=====
 
=====Script=== Prompting user to rename or not
 
strComputerName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
For Each objComputer in colComputers
      intErrorCode = objComputer.Rename(strComputerName)
      If intErrorCode <> 0 Then
            MsgBox "Error renaming computer. Error # " & intErrorCode
      Else
            Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
            objOutputFile.Close
            Set objOutputFile = Nothing
            MsgBox "Computer renamed. It will now reboot."
            objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
      End If
Next
 
=====Exit=====

Open in new window

0
Comment
Question by:Manfredtoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Accepted Solution

by:
biztopia earned 2000 total points
ID: 20418109
Try this code.

It worked in my testing.  Let me know if you have any problems with it.

Cheers
D.
Option Explicit 
 
Dim Text, Title
Dim WshNetwork         ' Object variable
Text = "Networking information" & vbCrLf & vbCrLf
Title = "Rename Hostname?"
 
' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf & vbCrLf
Text = Text & "Do you want to rename this computer?"
 
If MsgBox(Text, vbYesNo + vbQuestion, Title) = vbyes Then
	Dim strComputerName, objWMIService, objComputer, returnCode
 
	strComputerName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
 
	If strComputerName <> "" Then 
 
		Set objWMIService = GetObject("Winmgmts:root\cimv2") 
 
		' Call always gets only one Win32_ComputerSystem object. 
		For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
			ReturnCode = objComputer.Rename(strComputerName) 
		Next
		If ReturnCode <> 0 Then
			MsgBox "There was a problem changing the computer name.  System error code : " & ReturnCode
		Else
			If MsgBox("Changes made.  Do you wish to restart the computer?", vbYesNo + vbQuestion, Title) = vbyes Then 
				objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
			Else 
				MsgBox "You must restart computer for changes to take effect"
			End If 
		End If 
	End If 
End If 

Open in new window

0
 

Author Comment

by:Manfredtoo
ID: 20418673
biztopia: thanks man... it works great.=)
0
 
LVL 1

Expert Comment

by:017087594
ID: 24578972
can someone please help im getting "System error code : 5
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:varcom
ID: 24943766
Hello biztopia,

Copied the script to notepad and everything went smooth up unitl the below error........

LIne 33
Char 5
Error Variiable is undefined: 'objShell'
Code: 800A01F4
Source:  Microsoft VBScript runtime error


Thanks for the help.
0
 
LVL 1

Expert Comment

by:ranga_matta
ID: 25401349
can someone please help im getting "System error code : 5
0
 

Expert Comment

by:Net-tech1
ID: 35919046
Varcom,
Add
Dim objShell to line 2
0
 

Expert Comment

by:Net-tech1
ID: 35919080
You are getting the error code 5 because of the split token in windows 7. if you elevate the scripts privileges it should allow you to change the computers name.

This should work.
' ************************************************

' RENAME COMPUTER NAME

' ************************************************
Option Explicit 
 ElevateIfNeeded
 
Dim Text, Title
Dim WshNetwork
Dim objshell		' Object variable
Dim oShell         
Text = "Networking information" & vbCrLf & vbCrLf
Title = "Rename Hostname?"
 
' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf & vbCrLf
Text = Text & "Do you want to rename this computer?"
 
If MsgBox(Text, vbYesNo + vbQuestion, Title) = vbyes Then
	Dim strComputerName, objWMIService, objComputer, returnCode
 
	strComputerName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
 
	If strComputerName <> "" Then 
 
		Set objWMIService = GetObject("Winmgmts:root\cimv2") 
 
		' Call always gets only one Win32_ComputerSystem object. 
		For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
			ReturnCode = objComputer.Rename(strComputerName) 
		Next
		If ReturnCode <> 0 Then
			MsgBox "There was a problem changing the computer name.  System error code : " & ReturnCode
		Else
			If MsgBox("Changes made.  Do you wish to restart the computer?", vbYesNo + vbQuestion, Title) = vbyes Then 
				objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
			Else 
				MsgBox "You must restart computer for changes to take effect"
			End If 
		End If 
	End If 
End If 
Sub ElevateIfNeeded
	If GetOsVersionNumber > 6.0 Then 'Windows Vista or higher
		If NOT UserPerms("ELEVATED") Then
			Set oShell = CreateObject("Shell.Application")
			oShell.ShellExecute "wscript.exe", WScript.ScriptFullName, "", "runas", 1
			WScript.Quit 0
		End If
	End If 
End Sub

Function UserPerms (PermissionQuery)          
	UserPerms = False  ' False unless proven otherwise           
	Dim CheckFor, CmdToRun         

	Select Case Ucase(PermissionQuery)           
		'Setup aliases here           
		Case "ELEVATED"           
			CheckFor =  "S-1-16-12288"           
		Case "ADMIN"           
			CheckFor =  "S-1-5-32-544"           
		Case "ADMINISTRATOR"           
			CheckFor =  "S-1-5-32-544"           
		Case Else                  
			CheckFor = PermissionQuery                  
	End Select           

	CmdToRun = "%comspec% /c whoami /all | findstr /I /C:""" & CheckFor & """"  

	Dim oShell, returnValue        
	Set oShell = CreateObject("WScript.Shell")  
	returnValue = oShell.Run(CmdToRun, 0, true)     
	If returnValue = 0 Then UserPerms = True                   
End Function


Function GetOsVersionNumber()
	Dim oShell, sOStype, sOSversion
	Set oShell = CreateObject("Wscript.Shell")

	On Error Resume Next

	sOStype = oShell.RegRead(_
		"HKLM\SYSTEM\CurrentControlSet\Control\ProductOptions\ProductType")

	If Err.Number<>0 Then
		Err.Clear

		sOStype = oShell.RegRead(_
			"HKLM\SOFTWARE\Microsoft\Windows" & _
			"\CurrentVersion\VersionNumber")

	Else ' OS is NT based
		sOSversion = oShell.RegRead(_
			"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
		If Err.Number<>0 Then
			GetOsVersion = "Unknown NTx"
			Exit Function
		End If
	End If

	SetLocale "en-us"
	GetOsVersionNumber = CDbl(sOSversion)
End Function

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question