批处理文件更改系统名称

时间:2014-12-02 13:51:57

标签: batch-file

先生,我想用系统名称更改我的系统名称+使用批处理文件更改IP地址的最后六位数(例如:如果我的IP是192.168.1.22,PC名称应该是PC-001-022)请帮帮我需要改变我大学的400多个系统

1 个答案:

答案 0 :(得分:1)

未经测试,但至少在理论上,这应该有效

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem Retrieve ip address.
    set "ip[4]="
    for /f "tokens=2 delims=[]" %%z in ('ping -n 1 -4 ""') do if not defined ip[4] for /f "tokens=1-4 delims=." %%a in ("%%z") do  (
        set /a "ip[1]=%%a", "ip[2]=%%b", "ip[3]=%%c", "ip[4]=%%d"
    )

    rem If no ip address available, end process
    if not defined ip[4] (
        echo Failed to get ip address
        goto :eof
    )

    rem Prepare the new PC name. Padding is needed
    set /a "n[1]=ip[3]+1000", "n[2]=ip[4]+1000"
    set "newName=PC-%n[1]:~-3%-%n[2]:~-3%"

    rem Chech if renaming is needed
    if "%computername%"=="%newName%" (
        echo Computer already renamed
        goto :eof
    )

    rem Are you sure ?
    echo(
    echo(WARNING : Computer will be renamed from [%computername%] to [%newName%]
    echo(
    echo(Press Ctrl-C to keep the old name or any other key to rename computer
    pause > nul

    rem OK - Do the rename and restart the computer
    wmic ComputerSystem where Name="%computername%" call Rename Name="%newName%"
    shutdown /r /c "Restarting to rename computer"