如何通过批处理文件更改电脑的名称,而无需事先查找名称

如何通过批处理文件更改电脑的名称,而无需事先查找名称
WMIC ComputerSystem where Name=COMPUTERNAME call Rename Name=NewName

我在这里找到的这段脚本告诉您如何更改名称,但首先您必须找到它,我需要的是批处理文件找到它并将其存储起来以便在该脚本中使用它以便稍后更改它。

如果我必须自己去寻找它,那么通过设备属性进行更改会更容易,因为批处理文件会包含其他内容,而上面的脚本只会妨碍我。该脚本已经包含关闭 UAC、禁用防火墙以及创建文件夹并共享它(我也知道如何将其设置为授予所有人完全控制权限,

我在这里看到了,但是它不起作用。)(不,这不是什么坏事、奇怪事或错误事,只是一家做事方式错误的坏公司)。

答案1

最简单的方法是使用%ComputerName%系统变量:

Wmic ComputerSystem where Caption='%ComputerName%' rename NewName

或者,使用Wmic获取当前名称并在循环中使用它来编写设置新名称的命令:

命令行:

@for /f skip^=1^usebackq %i in (`Wmic /node:. ComputerSystem get Caption ^|findstr .^|More`)do @<con: Wmic ComputerSystem where Caption='%~i' rename NewName >nul &&@echo\zOk||@echo\nOp!

批处理文件:

@echo off 

for /f skip^=1^usebackq %%i in (`Wmic /node:. ComputerSystem get Caption ^| findstr . ^| More
`)do <con: Wmic ComputerSystem where Caption='%%~i' rename NewName>nul && echo\zOk || echo\nOp!

观察:以管理员身份运行并重新启动以查看结果。

答案2

该批处理脚本可以以管理员权限运行并显示一个包含两个选项的菜单:

  • [1] 重命名计算机名称
  • [2] 恢复旧计算机名称

@echo off
Mode 85,10
setlocal enableDelayedExpansion
Title Rename PC with WMIC
If [%1] NEQ [Admin] Goto RunAsAdmin
Set "TmpFile=%AppData%\%~n0.txt"
Set "File_Old_PC_Name=%~dp0Old_PC_Name.BAK"
If Not Exist "%TmpFile%" (
    echo "%ComputerName%">"%TmpFile%"
) Else (
    echo "%ComputerName%">>"%TmpFile%"
)
Call :RemoveDuplicateEntry "%TmpFile%" "%File_Old_PC_Name%"
:menuLOOP
Mode 85,10
Title Rename PC with WMIC
Cls & color 0B
echo(
echo(         ================================Menu================================
echo(
@for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do (
    echo                            %%A  %%B
)
echo(
echo(         ====================================================================
set choice=
echo( & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo( & call :menu_[%choice%]
GOTO:menuLOOP
::----------------------------------------------------------------------------------------------------------
:menu_[1] Rename Computer Name
cls & Mode 120,10
echo(
echo( Please Type a New Name for this PC :
set /P "NewName="
(
    echo    Answ = MsgBox("Are you sur to rename your computer as ""%NewName%"" ?"_
    echo   ,VbYesNo+VbQuestion,"%NewName% ?"^)
    echo    If Answ = VbYes then 
    echo        wscript.Quit(0^)
    echo    Else
    echo        wscript.Quit(1^)
    echo    End If
)>"%tmp%\%~n0_.vbs"
Cscript /nologo "%tmp%\%~n0_.vbs"
If ["!errorlevel!"] EQU ["0"] (
    Call :RenamePC "%NewName%" && Call :Ask4Reboot
) Else (
    Goto :menu_[1]
)
Exit /B
::----------------------------------------------------------------------------------------------------------
:menu_[2] Restore Old Computer Name
cls
set /a Cnt=0
    @for /f "delims=" %%a in ('Type "%File_Old_PC_Name%"') do ( 
        set /a Cnt+=1
        Set OLD!Cnt!=%%a
    )
echo(
@for /l %%N in (1 1 %Cnt%) do echo %%N - !OLD%%N!
echo(
:get selection
set selection=
set /p "selection=Enter Old Computer Name number: "
echo you picked %selection% - !OLD%selection%!
Call :RenamePC !OLD%selection%! && Call :Ask4Reboot
Exit /B
::----------------------------------------------------------------------------------------------------------
:RenamePC
WMIC ComputerSystem where Name="%ComputerName%" call Rename Name="%~1"
Exit /B
::----------------------------------------------------------------------------------------------------------
:Ask4Reboot
(
    echo    Set Ws = CreateObject("wscript.shell"^)
    echo    Answ = MsgBox("Did you want to reboot your computer ?"_
    echo ,VbYesNo+VbQuestion,"Did you want to reboot your computer ?"^)
    echo    If Answ = VbYes then 
    echo        Return = Ws.Run("cmd /c shutdown -r -t 10 -c ""You need to reboot in 10 seconds."" -f",0,True^)
    echo    Else
    echo        wscript.Quit(1^)
    echo    End If
)>"%tmp%\%~n0.vbs"
Start "" "%tmp%\%~n0.vbs"
Exit /B
::-----------------------------------------------------------------------------------------------------------
:RunAsAdmin
cls & color 0E & Mode 90,5
echo( 
echo(            ==========================================================
echo(                  Please wait a while ... Running as Admin ....
echo(            ==========================================================
Powershell start -verb runas '%0' Admin & Exit
::-----------------------------------------------------------------------------------------------------------
:RemoveDuplicateEntry <InputFile> <OutPutFile>
Powershell  ^
$Contents=Get-Content '%1';  ^
$LowerContents=$Contents.ToUpper(^);  ^
$LowerContents ^| select -unique ^| Out-File '%2'
Exit /b
::-----------------------------------------------------------------------------------------------------------

相关内容