?
Solved

Script to change PC Name by querying list

Posted on 2009-05-13
4
Medium Priority
?
276 Views
Last Modified: 2012-05-06
Hi All,

Need to some help to create a script that can change the pc name automatically. I can currently do this with no problem but only if I put the new pc name in the script. I want to be able to have one main text (or access database) where all the pc names are listed for each site (grouped by default gateway), then when the script runs it looks at the pc's current default gateway then queries the master file (txt or db) and then picks the next pc name in the list according to the default gateway.

I know thats a lot there, and hope I explained it fine.

Please help.
Thanks
0
Comment
Question by:avineshp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24376210
Give this a try.

Change these variables to something accessible on the workstation. Could be server share.
Set NewCompList=C:\AT\NewComputerNames.txt
Set UsedCompList=C:\AT\UsedComputerNames.txt
Set ErrorLogging=C:\AT\Errors.txt

@ECHO OFF
Set NewCompList=C:\AT\NewComputerNames.txt
Set UsedCompList=C:\AT\UsedComputerNames.txt
Set ErrorLogging=C:\AT\Errors.txt
 
if exist "%UsedCompList%" (
    FIND /i "%ComputerName%" "%UsedCompList%" 1>NUL
    if not errorlevel 1 (
        echo %ComputerName% already exist in %UsedCompList% computer name has already been changed once before.
        echo %ComputerName% already exist in %UsedCompList% computer name has already been changed once before.>>"%ErrorLogging%"
        goto :EOF
    )
)
 
Set NewName=
Set MAC=
Set Gateway=
 
for /f "Tokens=11 delims=: " %%a in ('ipconfig /all ^| find /i "Physical Address"') do Set MAC=%%a
for /f "Tokens=12 delims=: " %%a in ('ipconfig /all ^| find /i "Default Gateway"') do Set Gateway=%%a
for /f "tokens=3 delims=]," %%a in ('type "%NewCompList%" ^| find "%Gateway%" ^| find /v /n "" ^| findstr /b /c:"[1]"') do Set NewName=%%a
 
if defined NewName (
    (type "%NewCompList%" | find /v "%NewName%")>"%NewCompList%.temp"
    move "%NewCompList%.temp" "%NewCompList%"
 
    if not exist echo Date,Time,Old Computer Name,Gateway,New Computer Name,MAC Address>>"%UsedCompList%"
    echo %date%,%time%,%ComputerName%,%Gateway%,%NewName%,%MAC%
    echo %date%,%time%,%ComputerName%,%Gateway%,%NewName%,%MAC%>>"%UsedCompList%"
 
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /t REG_SZ /f /d "%NewName%"
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters" /v "NV Hostname" /t REG_SZ /f /d "%NewName%"
    echo Computer name has been changed please reboot!!!
) else (
    echo A new computer name for %Gateway% is not found in %NewCompList%
    echo A new computer name for %Gateway% is not found in %NewCompList%>>"%ErrorLogging%"
)

Open in new window

NewComputerNames.txt
0
 
LVL 2

Author Comment

by:avineshp
ID: 24378118
Hi AmazingTech,

The script works nearly perfectly, Just one problem:
when I run it it just waits for a date to be entered manually (found this out when, I run it, it just stops at a point, then if I go to the UsedComputerNames.txt file I see the below:

The system cannot accept the date entered.
Enter the new date: (yy-mm-dd)

after i type in the date, it works perfectly, why would it do this?

Thanks again for the help, after this problem, this script will be perfect.
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 24379143
Oh. I see the problem.

OK. Here you go.
@ECHO OFF
Set NewCompList=C:\AT\NewComputerNames.txt
Set UsedCompList=C:\AT\UsedComputerNames.txt
Set ErrorLogging=C:\AT\Errors.txt
 
if exist "%UsedCompList%" (
    FIND /i "%ComputerName%" "%UsedCompList%" 1>NUL
    if not errorlevel 1 (
        echo %ComputerName% already exist in %UsedCompList% computer name has already been changed once before.
        echo %ComputerName% already exist in %UsedCompList% computer name has already been changed once before.>>"%ErrorLogging%"
        goto :EOF
    )
)
 
Set NewName=
Set MAC=
Set Gateway=
 
for /f "Tokens=11 delims=: " %%a in ('ipconfig /all ^| find /i "Physical Address"') do Set MAC=%%a
for /f "Tokens=12 delims=: " %%a in ('ipconfig /all ^| find /i "Default Gateway"') do Set Gateway=%%a
for /f "tokens=3 delims=]," %%a in ('type "%NewCompList%" ^| find "%Gateway%" ^| find /v /n "" ^| findstr /b /c:"[1]"') do Set NewName=%%a
 
if defined NewName (
    (type "%NewCompList%" | find /v "%NewName%")>"%NewCompList%.temp"
    move "%NewCompList%.temp" "%NewCompList%"
 
    if not exist "%UsedCompList%" echo Date,Time,Old Computer Name,Gateway,New Computer Name,MAC Address>>"%UsedCompList%"
    echo %date%,%time%,%ComputerName%,%Gateway%,%NewName%,%MAC%
    echo %date%,%time%,%ComputerName%,%Gateway%,%NewName%,%MAC%>>"%UsedCompList%"
 
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /t REG_SZ /f /d "%NewName%"
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters" /v "NV Hostname" /t REG_SZ /f /d "%NewName%"
    echo Computer name has been changed please reboot!!!
) else (
    echo A new computer name for %Gateway% is not found in %NewCompList%
    echo A new computer name for %Gateway% is not found in %NewCompList%>>"%ErrorLogging%"
)

Open in new window

0
 
LVL 2

Author Closing Comment

by:avineshp
ID: 31580889
Excellent script, thanks for your help again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question