VBS script to change the computer name

Asked

Viewed 109 times

2

I have this code that checks the computer name and changes it, but it’s not changing, informs me:

Line 29 error, character 5, Required object:

Dim objShell
Do
    Choice = InputBox("ENTER THE CORRESPONDING NUMBER. . ." & vbCrlf & vbCrlf & _
    " 1 -  Check PC Name" & vbCrlf & _
    " 2 -  Change PC Name" & vbCrlf & _
    " 3 -  EXIT", _
      "MENU")
    
    If (Choice <> vbNull) and (Choice <> "") Then
        Select Case Choice
           Case 1
               Rem Gets the name of the Computer
               set WshNetwork=Wscript.CreateObject("Wscript.Network")
               strComputerName=WshNetwork.ComputerName
               Wscript.Echo strComputerName
           Case 2
               Dim Input
               Input = InputBox ("Enter Name", "New Name")
           If Input = "" Then
               MsgBox "Change Canceled!"
           Else
               'MsgBox ("You typed :" & Input)
               set Objshel=WScript.CreateObject("Wscript.shell")
               ObjShell.run"wmic computersystem where name=""%computername%"" call rename name=""& Input"""
               Set objshel=Nothing
           End If
           Case 3
               WScript.Quit
           Case Else
                'Wscript.Echo "bye"
        End Select
    End If
Loop Until Choice = ""

1 answer

0

Dim objShell
Do
Choice = InputBox( _
    " ENTER THE CORRESPONDING NUMBER. . ." & vbCrlf & vbCrlf & _
    " 1 -  Check PC Name" & vbCrlf & _
    " 2 -  Change PC Name" & vbCrlf & _
    " 3 -  EXIT", "MENU")

Rem Gets the name of the Computer
strComputerName=Wscript.CreateObject("Wscript.Network").ComputerName

If (Choice <> vbNull) and (Choice <> "") Then
    Select Case Choice
    Case 1
        Wscript.Echo strComputerName
    Case 2
        Dim Input
        Input = InputBox ("Enter Name", "New Name")
        If Input = "" Then
            MsgBox "Change Canceled!"
        Else
            WScript.CreateObject("Wscript.shell").Run _
            "wmic computersystem where name="&chr(39)&strComputerName&chr(39)&" call rename name="&Input&"",0,false
        End If
    Case 3
        WScript.Quit
        Case Else
        'Wscript.Echo "bye"
    End Select
End If
Loop Until Choice = ""

ObjShell.run"wmic computersystem where name=""%computername%"" call rename name=""& Input"""
  • To expand the variable %ComputerName% in the code, you can use:

Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings( "%ComputerName%")
wshShell = Nothing
        linked with Rob van der Woude’s Sript Pages - Environment Variables

  • In a row without creating variable for when it is used once in the whole script
WScript.Echo (CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ComputerName%"))
  • In a row saving in variable and used more than once in script
strComputerName = (CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ComputerName%"))
WScript.Echo strComputerName

Then note that in your code, there is no expanded value of the variable ComputerName being passed to the .Run WMIC use, try:

Get the name of the computer before and regardless of the Case who is chosen. Use before entering the if() else() Menu, you will use them both to Case 1 as Case 2:

    If (Choice  vbNull) and (Choice  "") Then
         Select Case Choice
           ⇉  Case 1
                 Rem Gets the name of the Computer
                 set WshNetwork=Wscript.CreateObject("Wscript.Network")
                 strComputerName=WshNetwork.ComputerName
                                      
                 Wscript.Echo strComputerName
           ⇉  Case 2
                 Dim Input
                 Input = InputBox ("Enter Name", "New Name")
                 If Input = "" Then
                     MsgBox "Change Canceled!"
                 Else
                     'MsgBox ("You typed :" & Input)
                     set Objshel=WScript.CreateObject("Wscript.shell")    
                   ObjShell.run "wmic computersystem where name=""%computername%"" call rename name=""& Input"""
                     Set objshel=Nothing
                 End If
  • Staying....
 Rem Gets the name of the Computer
 set WshNetwork=Wscript.CreateObject("Wscript.Network")
 strComputerName=WshNetwork.ComputerName

    If (Choice  vbNull) and (Choice  "") Then
         Select Case Choice
           ⇉  Case 1                  
                 Wscript.Echo strComputerName
           ⇉  Case 2
                 Dim Input
                 Input = InputBox ("Enter Name", "New Name")
                 If Input = "" Then
                     MsgBox "Change Canceled!"
                 Else
                     'MsgBox ("You typed :" & Input)
                     set Objshel=WScript.CreateObject("Wscript.shell")   
                   ObjShell.run "wmic computersystem where name=""ComputerName"" call rename name=""& Input"""
                     Set objshel=Nothing
                 End If

Note: 1. The lack of space between ObjShell.run+ESPAÇO+"wmic...", I’m beginning to understand how mere editing/typing error when posting your question.

            
ObjShell.run"wmic computersystem where name=""%computername%"" call rename name=""& Input"""

Note: 2. The wmic accepts the use of half quotes in nome='str...', and I have popr preference to use it in double quotes substitution, and it may be better to align the escapings in a command string that will also use double quotes, just by using the chr(), as I used with the ' == chr(29)

Note: 4. The variable %ComputerName% will only take on the new value after restarting, when you use the sequence below, the current name will not update to the name:

  • When using: Case 1 displays current name
  • When using: Case 2 change the name
  • When using: Case 1 after the Case 2 will display current name, not new!

Note: 5. You need to run your script as an administrator to change the name of the computer, a possible (untested) solution to elevate the execution, would be to add the lines below:

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName, _
  """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If

Dim objShell

Do
    Choice = InputBox( _
        " ENTER THE CORRESPONDING NUMBER. . ." & vbCrlf & vbCrlf & _
        " 1 -  Check PC Name" & vbCrlf & _
        " 2 -  Change PC Name" & vbCrlf & _
        " 3 -  EXIT", "MENU")

   Rem Gets the name of the Computer
   strComputerName=Wscript.CreateObject("Wscript.Network").ComputerName

   If (Choice <> vbNull) and (Choice <> "") Then

       Select Case Choice
       Case 1
           Wscript.Echo strComputerName
       Case 2
           Dim Input
           Input = InputBox ("Enter Name", "New Name")
           If Input = "" Then
               MsgBox "Change Canceled!"
           Else
               WScript.CreateObject("Wscript.shell").Run _
               "wmic computersystem where name="&chr(39)&strComputerName&chr(39)&" call rename name="&Input&"",0,false
           End If
       Case 3
           WScript.Quit
       Case Else
            Wscript.Echo "bye"
            WScript.Quit
       End Select

   End If

Loop Until Choice = ""

Browser other questions tagged

You are not signed in. Login or sign up in order to post.