Announcement

Collapse
No announcement yet.

Script to Rename Computers

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to Rename Computers

    My company is in the process of renaming the systems and we have about 1200 units which need to be renamed. I am looking for a way/script that will rename the computers for me and still keep them on the domain. The current script I have requires me to enter the current system name and then the new system name. My current naming scheme is ABCD-1234-WXYZ and the new policy requires the name to be ABCDWX0YZ1234. (note I have added a 0 (zero) at the 7th character, this will need to be added in all names.) The current script I have is (it is a batch file;
    Code:
    @echo off
    
    cls
    color 70
    Echo "|---------------------------------------------------|
    echo "         This Computer is "%computername%" 
    echo "|---------------------------------------------------|
    echo "|                                                   |
    echo "|This Utility will Rename a computer on the Domain  |
    echo "|         it must be powered on and connected       |
    echo "|              to the network at the time           |
    echo "|                  ***system admin***              |
    echo "|---------------------------------------------------|
    REM cd /d ..\..
    
    :redo
    
    rem ********************the following will ask for the compuer name **************************
    
    echo "*** TO QUIT TYPE Q"
    echo "oldcomputer %oldcomputer%"
    
    SET /P oldcomputer=Please enter the Current Computer Name :
    
    Rem *****************************************************************
    
    if exist "\\%oldcomputer%\c$" goto goodname
    
    echo " %oldcomputer% "
    
    pause
    
    :NotFound
    color c0
    echo " you typed  %oldcomputer%"
    
    echo " 
    echo " 
    echo "THE COMPUTER WAS NOT FOUND!!! Make Sure it is powered on "
    echo "and on the network at this time"
    pause
    goto redo
    
    :goodname
    color 70
    
    echo "
    echo "
    echo "           The computer %oldcomputer% was found
    Echo " 
    echo "           Now we need the new computer name
    Echo "
    SET /P newcomputer=What is the new name of the new computer :
    echo "
    echo " Your Username :            %username%" 
    echo " The Current Computername:  %oldcomputer%  
    echo " The New Computer Name:     %newcomputer%
    echo " 
    echo " You will now be prompted to enter your PASSWORD Twice:
    echo "
    echo "
    
    
    netdom renamecomputer %oldcomputer% /newname:%newcomputer% /userD:NV\%username% /passwordd:* /usero:NV\%username% /passwordo:* /reboot:600
    
    rem netdom renamecomputer %oldcomputer% /newname:%newcomputer% /userD:nv\* /passwordd:* /usero:nv\* /passwordo:* /reboot:600 /securepasswordprompt
    
    :stopp
    goto redo
    
    SET /P anotherrun=Would you like to rename another?(Y/N) :
    
    PAUSE
    end
    Last edited by Rems; 4th May 2011, 09:27. Reason: wrapped code in CODE tags

  • #2
    Re: Script to Rename Computers

    Does the current script work?

    Am I right in thinking that the only change you want to make is to not be prompted for the old and new name?

    Just my opinion, but the old naming scheme seems to be far more readable...
    Gareth Howells

    BSc (Hons), MBCS, MCP, MCDST, ICCE

    Any advice is given in good faith and without warranty.

    Please give reputation points if somebody has helped you.

    "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

    "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

    Comment


    • #3
      Re: Script to Rename Computers

      Yes the old script does work.

      You are correct in your thinking. I am trying to automate the process.

      I agree with your opinion, the old name scheme is easier to read but the change came from corporate. They want all sites to have the same format.

      Comment


      • #4
        Re: Script to Rename Computers

        Originally posted by hodester View Post
        My company is in the process of renaming the systems and we have about 1200 units which need to be renamed.
        I am looking for a way/script that will rename the computers for me and still keep them on the domain.

        My current naming scheme is ABCD-1234-WXYZ and the new policy requires the name to be ABCDWX0YZ1234. (note I have added a 0 (zero) at the 7th character, this will need to be added in all names.)

        The current script I have requires me to enter the current system name and then the new system name.
        The batch below uses DSQUERY to find computer object which names matches a certain pattern (matching the old names scheme, alphabet letters dash numerical digits dash alphabet letters).
        BECAREFUL - test the batch on test computers in a test OU.

        The batch uses VBS code to create a popup box. The password entered in the box will be masked. (If the pop-up box for any reason
        does return errors you can also use the Set /p for entering a password but then the password will not be masked)
        Code:
        @echo off & cls
        
        color 70 & echo.&echo.
        Echo.   *---------------------------------------------------*
        echo.             This Computer is "%computername%"
        echo.   ^|---------------------------------------------------^|
        echo.   ^|                                                   ^|
        echo.   ^|   This Utility will try to Rename all computers   ^|
        echo.   ^|                 on the Domain                     ^|
        echo.   ^|        they must be powered on and connected      ^|
        echo.   ^|              to the network at the time           ^|
        echo.   ^|                  ***system admin***               ^|
        echo.   *---------------------------------------------------*
        REM cd /d ..\..
        
        echo.
        setlocal
        echo.Your Username                        : NV\%username%
        <nul: set /p =Enter your password in the pop-up box:
        call:PasswordBox %~0
        echo. ~ thank you ~
        echo.
        
        for /f "tokens=*" %%* in (
           'dsquery.exe computer -limit 0 "OU=TEST,dc=DOMAIN,dc=local" -o samid ^|Findstr /ric:"^.[a-Z]*-[0-9]*-[a-Z]*\$.$"') Do call:modcomputer %%~*
        goto:exitFor
        
        :modcomputer
        set oldName=%*
        set oldName=%oldName:~0,-1%
        
        echo.Computer...
        echo.oldName=%oldName%|Findstr /ric:"^oldName=[a-Z]*-[0-9]*-[a-Z]*$" &&(
           For /f "tokens=1,2,3 delims=-" %%^1 in (
             'echo.%oldname%'
             ) do call:flip %%^1 %%^2 %%^3
           )
           exit /b 0
           :flip
           set "m=%~3"
           set "newName=%~1%m:~0,2%0%m:~2%%~2
           set newName
           call:rename >"mod_%oldName%.log"
           echo.   - done -
        exit /b 0
        :exitFor
        endlocal
        
        pause
        :: _________________________________________________
        goto:EOF & goto:EOF & goto:EOF & goto:EOF & goto:EOF
        
        
        :rename (and write log) ---THIS IS (was) YOUR SCRIPT---
        set "go="
        
        echo.%date% - %time%
        
        echo " Domain Admin  : %username%
        echo " operating from: %computername%
        echo "
        echo " Target computer  : %oldName%
        echo " New Computer Name: %newName%
        echo "
        
        ping.exe -n 1 %oldName% &&(goto :goodname)
        if exist "\\%oldName%\c$" goto :goodname
        
        :NotFound
        echo " %oldName% was not connectable!!
        echo " Make Sure it is powered on and on the network
        echo.
        exit /b 0
        
        :goodname
        set "go=TRUE"
        echo.           The computer %oldName% was found
        echo.           Now we set the new computer name
        Echo.
        If defined go start /b netdom renamecomputer %oldName% /newname:%newName% /userD:NV\%username% /passwordd:"%Pwd%" /usero:NV\%username% /passwordo:"%Pwd%" /reboot:600
        rem If defined go start /b netdom renamecomputer %oldName% /newname:%newName% /userD:nv\* /passwordd:"%Pwd%" /usero:nv\* /passwordo:"%Pwd%" /reboot:600 /securepasswordprompt
        set "go="
        exit /b 0
        
        goto:EOF ----------- vbs code ---------------
        :PasswordBox
        call:tmpfile1 %* >"%temp%.\$.Pwd"
        For /f "tokens=*" %%* in ('cscript.exe /nologo /e:vbscript "%temp%.\$.Pwd"') do set "Pwd=%%*"
        Del "%temp%.\$.Pwd"
        exit /b 0
        :tmpfile1
        For /f "tokens=1,* delims=#" %%A in ('findstr /Ric:"\<\:\:WSH1#\>" "%*"') do echo.%%B
        exit /b
        
        ::WSH1# '---------- Main begins -------------
        ::WSH1# sWidth = 350
        ::WSH1# sHeight = 180
        ::WSH1#
        ::WSH1# with HTABox(sHeight, sWidth)
        ::WSH1#   .document.title = String(22, chr(160)) & "Credentials"
        ::WSH1#   .msg.innerHTML = "UserID: &nbsp; &nbsp; <input type=text " _
        ::WSH1#                  & "size=20 id=UID><br>Password: <input "_
        ::WSH1#                  & "type=password id=PW size=22><p><input " _
        ::WSH1#                  & "type=submit value=Submit onclick=" _
        ::WSH1#                  & "'done.value=true'>"
        ::WSH1#   .UID.value = createobject("wscript.network").username
        ::WSH1#   .PW.focus
        ::WSH1#   do until .done.value : wscript.sleep 50 : loop
        ::WSH1#   sUID = .UID.value : sPW = .PW.value
        ::WSH1#   .close
        ::WSH1# end with
        ::WSH1# CreateObject("WScript.Shell").SendKeys "^"
        ::WSH1# 
        ::WSH1# wscript.echo sPW
        ::WSH1# 
        ::WSH1# '--------- Main Ends ------------
        ::WSH1# ' Author: Tom Lavedas, June 2010
        ::WSH1# Function HTABox(h, w)
        ::WSH1#    Dim l, t, IE, sCmd 
        ::WSH1#    With createobject("internetexplorer.application")
        ::WSH1#      l = .width
        ::WSH1#      t = .height
        ::WSH1#      .quit
        ::WSH1#    End with
        ::WSH1# 
        ::WSH1#   Dim IHTA
        ::WSH1# 
        ::WSH1#   nRnd = hex(Timer*100)
        ::WSH1# 
        ::WSH1#   sCmd = "mshta.exe ""javascript:{new " _ 
        ::WSH1#        & "ActiveXObject(""InternetExplorer.Application"")" _ 
        ::WSH1#        & ".PutProperty('" & nRnd & "',window);window" _
        ::WSH1#        & ".resizeTo();window.moveTo(" & Round(l/2, 0) & "," _
        ::WSH1#        & Round(t/2, 0) & ")}"""
        ::WSH1# 
        ::WSH1#   with CreateObject("WScript.Shell") 
        ::WSH1#     .Run sCmd, 1, False
        ::WSH1#      do until .AppActivate("javascript:{new ") : wscript.sleep 10 : loop
        ::WSH1#   end with ' WSHShell 
        ::WSH1# 
        ::WSH1#   For Each IE In CreateObject("Shell.Application").windows 
        ::WSH1#     If IsObject(IE.GetProperty(nRnd)) Then 
        ::WSH1#       set HTABox = IE.GetProperty(nRnd)
        ::WSH1#       IE.quit
        ::WSH1#       HTABox.document.title = "HTABox" 
        ::WSH1#       HTABox.document.write _ 
        ::WSH1#                "<HTA:Application contextMenu=no border=thin " _ 
        ::WSH1#              & "minimizebutton=no maximizebutton=no sysmenu=no />" _ 
        ::WSH1#              & "<body scroll=no style=filter:progid:DXImageTrans" _
        ::WSH1#              & "form.Microsoft.Gradient(GradientType=0,StartColor" _
        ::WSH1#              & "Str='#86cceb',endColorStr='#5589ab');font:normal " _
        ::WSH1#              & "10pt Arial' onbeforeunload='vbscript:if not done." _ 
        ::WSH1#              & "value then window.event.cancelBubble=true:" _ 
        ::WSH1#              & "window.event.returnValue=false:" _ 
        ::WSH1#              & "done.value=true:end if'>" _ 
        ::WSH1#              & "<input type=hidden id=done value=false>" _ 
        ::WSH1#              & "<center><span id=msg>&nbsp;</span><center></body>"
        ::WSH1# 
        ::WSH1#       l = Round( (l-w)/2, 0)
        ::WSH1#       t = Round( (t-h)/2, 0)
        ::WSH1#       HTABox.moveTo l,t
        ::WSH1#       HTABox.resizeTo w,h
        ::WSH1#       Exit Function 
        ::WSH1#     End If 
        ::WSH1#   Next 
        ::WSH1# 
        ::WSH1# ' I can't imagine how this line can be reached, but just in case
        ::WSH1#   MsgBox "HTA window not found." :  wscript.quit
        ::WSH1# 
        ::WSH1# End Function
        ::WSH1# '---------- code ends -------------
        /Rems
        Last edited by Rems; 6th May 2011, 15:50.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Script to Rename Computers

          Upon reading your script I found a possible problem caused by my description. The correct naming convention I use is AAAA-BBBBB-CC## where the "B" can be either a number or letter. Other than that the 2nd and 3rd set still need to trade places with the dashes remove (AAAACC0##BBBBB).

          Sorry for the inconvenience.

          Comment


          • #6
            Re: Script to Rename Computers

            Originally posted by hodester View Post
            Upon reading your script I found a possible problem caused by my description. The correct naming convention I use is AAAA-BBBBB-CC## where the "B" can be either a number or letter. Other than that the 2nd and 3rd set still need to trade places with the dashes remove (AAAACC0##BBBBB).

            Sorry for the inconvenience.
            If I understood now the exact pattern for the old naming convention would be:
            begin|four letters, a dash, five characters (can be a random mixure of letters and numbers), a dash, two letters and two digits|end

            Or, would this simplified regular expression also be sufficient?:
            begin|more than one characters, dash, more than one characters, dash, more than one characters|end

            We have to find a pattern that is loose enough to mach the names of all the old computers but tight enough not to find existing computers who's name shouldn't be changed.


            The name conversion done by the batch will be:
            Use the dash as separator to separate the 3 sets.
            And rearange the sets in the folowing order:
            first the first set then, the first two characters of the 3rd set, add a zerro followed by the remaining characters of the third set, and at last all characters of the second set. In the new naming convention theres are no dashes.



            /Rems

            - - - EDIT - - -
            here's is a new name translating part of the script,
            Code:
            @echo off & cls & color 70
            setlocal
            
            call:modcomputer AAAA-B#B#B-CC##
            goto:exitFor
            
            :modcomputer
            set oldName=%*
            set oldName=%oldName:$=%
            
            echo.Computer...
            echo.oldName=%oldName%|Findstr /ric:"^oldName=[a-Z][a-Z][a-Z][a-Z]-.*-[a-Z][a-Z].*$" &&(
               For /f "tokens=1,2,3 delims=-" %%^1 in (
                 'echo.%oldname%'
                 ) do call:flip %%^1 %%^2 %%^3
               )
               exit /b 0
               :flip
               set "m=%~3"
               set "newName=%~1%m:~0,2%0%m:~2%%~2
               set newName
            rem   call:rename >"mod_%oldName%.log"
            rem   echo.   - done -
            exit /b 0
            
            :exitFor
            endlocal
            
            pause
            :: _________________________________________________
            goto:EOF & goto:EOF & goto:EOF & goto:EOF & goto:EOF
            you also need to edit the regular expression used with the dsquery statement.


            _
            Last edited by Rems; 6th May 2011, 15:55.

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment

            Working...
            X