×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Change Computer Name VB Script

Change Computer Name VB Script

Change Computer Name VB Script

(OP)
I have a script that renames a PC. The issue is, if I click OK or Cancel without entering any data. i get an error. I am not sure how to add error control for this. I would like it to just display the error that the PC was not renamed and close. Can anyone help?

If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
NewName = " "
NewName = INPUTBOX("NOTE: Spaces are not allowed!" & vbCrLf & "Please enter computer name using format:" & vbCrLf & "First Initial Last Name-Model" & vbCrLf & "e.g. SBennet-X1")
strUser = "xxx"
strPassword = "xxx"
Set objWMIservice = GetObject("winmgmts:\\.\root\cimv2")
set colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
For each objitem in colitems
Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
ErrCode = objComputer.Rename(NewName, strPassword, strUser)
If ErrCode = 0 Then
CreateObject("WScript.Shell").Popup "Computer renamed successfully to: "& Newname & "", 5, "Computer Rename Task"
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'restart, wait 0 seconds, force running apps to close
'oShell.Run "%comspec% /c shutdown /r /t 0 /f", , TRUE
End If
If ErrCode <> 0 Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
End If
Next
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)

RE: Change Computer Name VB Script

Something like this:

CODE

NewName = INPUTBOX("NOTE: Spaces are not allowed ... etc
If NewName = "" Then 
   MsgBox "Computer was not renamed successfully. Please rename manually!"   
Else

   ... rest of the code

End If 

RE: Change Computer Name VB Script

(OP)
Still get the same error

CODE

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
strUser = "xxx"
strPassword = "xxx"
NewName = INPUTBOX("NOTE: Spaces are not allowed!" & vbCrLf & "Please enter computer name using format:" & vbCrLf & "First Initial Last Name-Model" & vbCrLf & "e.g.  SBennet-X1")
If NewName = " " Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
Else

Set objWMIservice = GetObject("winmgmts:\\.\root\cimv2")
set colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
For each objitem in colitems
Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
     & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
ErrCode = objComputer.Rename(NewName, strPassword, strUser)
If ErrCode = 0 Then
CreateObject("WScript.Shell").Popup "Computer renamed successfully to: "& Newname & "", 5, "Computer Rename Task"
Dim oShell 
Set oShell = CreateObject("WScript.Shell")
'restart, wait 0 seconds, force running apps to close
'oShell.Run "%comspec% /c shutdown /r /t 0 /f", , TRUE
End If
If ErrCode <> 0 Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
End If
Next
End If
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript) 

RE: Change Computer Name VB Script

(OP)
I figured it out.... Thanks to the tip from guitarzan

CODE

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
strUser = "xxx"
strPassword = "xxx"
NewName = ""
NewName = INPUTBOX("NOTE: Spaces are not allowed!" & vbCrLf & "Please enter computer name using format:" & vbCrLf & "First Initial Last Name-Model" & vbCrLf & "e.g.  SBennet-X1")
If NewName = "" Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
Else

Set objWMIservice = GetObject("winmgmts:\\.\root\cimv2")
set colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
For each objitem in colitems
Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
     & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
ErrCode = objComputer.Rename(NewName, strPassword, strUser)
If ErrCode = 0 Then
CreateObject("WScript.Shell").Popup "Computer renamed successfully to: "& Newname & "", 5, "Computer Rename Task"
Dim oShell 
Set oShell = CreateObject("WScript.Shell")
'restart, wait 0 seconds, force running apps to close
'oShell.Run "%comspec% /c shutdown /r /t 0 /f", , TRUE
End If
If ErrCode <> 0 Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
End If
Next
End If
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close