Your question, your audience. Choose who sees your identity—and your question—with question security.
=====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=====
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
' ************************************************
' 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
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.