Blog

Rename Computer

Here is a script to rename the computer
Make sure you add /NewName:YourNewComputerName in the script parameters and obviously change it for your naming.
Also note that each computer requires a unique name on the network to prevent conflicts so ensure the new name is unique.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
strComputerName = WScript.Arguments.Named("NewName")
 
If strComputerName <> "" then
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
 
For Each objComputer in colComputers
    wscript.echo "Old Computer Name: " & objComputer.Name
    wscript.echo "New Computer Name: " & strNewName
 
    If objComputer.Name <> strNewName Then
 
       ' rename this computer

       intErrorCode = objComputer.Rename(strComputerName)
       ' now reboot
       Reboot
    End if
Next
Else
wscript.echo "Please define a computer name!"
End If
Sub Reboot()
 
dim strComputer, objWMIService, colOS, objOS
  strComputer = "."
 
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
  Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
 
  For Each objOS in colOS
                objOS.Reboot()
  Next
End Sub

Posted in: Scripts, Technical

Leave a Comment (2) ↓

2 Comments

  1. Doug Mortensen May 26, 2013

    The argument didn’t seem to work for me. I uploaded the script to my dashboard & assigned it to run on a PC. The output was as follows:

    Summary: Old Computer Name: WINDOWS-K31NQU9
    New Computer Name:

    (i.e. the new computer name argument apparently wasn’t processed…?)

    I passed the argument to the script from the dashboard as follows:
    /NewName:Impala13

    (I used no space after the colon. Was I supposed to? I guess I’ll try again & add the space. I just thought I’d see whether you had any tips.

    Also FYI we have seen in the past where the GFI MAX dashboard seems to interpret an argument even when none is present. We ended up having to modify our scripts to sanitize the argument received and if it was not of the format expected, just behave as though no argument is passed. As a result, I am a bit mistrusting of whether the dashboard could be at fault here, and not your script or me… 🙂

    Thanks so much!


    Doug

    reply
    • Jake May 28, 2013

      Hey Doug,

      That’s very strange, looks like you are doing it correctly.
      No Space should be required after the colon.

      Max always passes the /LogFile parameter to each script, which is why I’ve done it that way.
      Easiest way would be to test on a script that enumerates all the parameters passed to make sure its actually working.

      reply

Leave a Comment