@echo off
:: 检测命令行参数1
if not %1#==# set JoinWorkgroup=%1
if not %Joinworkgroup%#==# goto _doit
:: 从当前目录下的sysprep.inf中获得
if not exist sysprep.inf goto _noGrpName
for /f "usebackq tokens=*" %%i in (`type sysprep.inf ^| find /i "JoinWorkgroup"`) do @set %%i
:: 如果以上都没有,默认设置为BDGROUP
:_noGrpName
if %JoinWorkgroup%#==# set JoinWorkgroup=administrators
:_doit
:: 先检查当前工作组设置,如果相同,则不修改
wmic computersystem get domain | findstr /r /i "^%JoinWorkGroup%$" > nul
if %errorlevel%==0 goto _quit
:: 检查环境变量COMPUTERNAME是否为空,如果为空,则从WMI数据库中提取
if not %COMPUTERNAME%#==# goto _change
for /f "usebackq skip=1 tokens=*" %%i in (`wmic computersystem get name`) do set CNAME=%%i
if %CNAME%#==# goto _quit
set COMPUTERNAME=%CNAME%
:_change
echo Changing Workgroup to "%JoinWorkgroup%" ...
echo.
wmic computersystem where Name="%COMPUTERNAME%" call JoinDomainOrWorkgroup Name="%JoinWorkgroup%"
if %errorlevel%==0 goto _success
goto _quit
:_success
echo Successfully Changed Workgroup to "%JoinWorkgroup%".
echo.
:_quit
for %%i in (Joinworkgroup cname) do set %%i=
批处理更改工作组为Administrator,布布扣,bubuko.com