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)
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)