Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Computer Name VB Script 1

Status
Not open for further replies.

Tom__

Technical User
Oct 13, 2016
3
US
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)
 
Something like this:

Code:
NewName = INPUTBOX("NOTE: Spaces are not allowed ... etc
[highlight #FCE94F]If NewName = "" Then[/highlight] 
   [highlight #FCE94F]MsgBox "Computer was not renamed successfully. Please rename manually!"[/highlight]   
[highlight #FCE94F]Else[/highlight]

   ... rest of the code

[highlight #FCE94F]End If[/highlight]
 
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")
[COLOR=#EF2929]If NewName = " " Then
MsgBox "Computer was not renamed successfully. Please rename manually!"
Else
[/color]
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
[COLOR=#EF2929]End If[/color]
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)
 
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)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top