在一个变量中对多个值进行分组

在一个变量中对多个值进行分组

我公司有多个部门。我需要使用不同的 Windows 密钥来激活其 Windows。我怎样才能使用一个密钥将各个部门分组到一个变量中,使用另一个密钥将其他部门分组到一个变量中,以便我的批处理脚本可以更容易执行。

REM ComputerName Group
HR-16
DD-89
CC-05
AB-50
XY-23
LM-46
NO-15
RB-09
CF-05

set group1=HR-16  DD-89  CC-05
set group2=CF-05  RB-09  NO-15
set group3=AB-50  XY-23  LM-46

set key1=AAAAA.BBBBB.CCCCC.DDDDD.EEEE
set key2=BBBBB.CCCCC.DDDDD.EEEE.11111
set key3=BBBBB.CCCCC.DDDDD.EEEE.22222


if %computername:~0,5%==%group1% if %computername:~-2%==-X (
cscript %windir%\system32\slmgr.vbs /ipk %key1% 
) else (
if %computername:~0,5%==%group2% if %computername:~-2%==-X (
cscript %windir%\system32\slmgr.vbs /ipk %key2% 
) else (
if %computername:~0,5%==%group3% if %computername:~-2%==-X (
cscript %windir%\system32\slmgr.vbs /ipk %key3% 
)
)
)
cscript %windir%\system32\slmgr.vbs /ato

@exit /b

在此处输入图片描述

在此处输入图片描述

将两个选项合并到一个批处理文件中

@echo off

REM Windows Activation
echo ================================
echo Choices:
echo. [1] = Input Key
echo. [2] = Pre-Set Key
echo ================================

goto choice


:choice
Echo ComputerName : %computername%
Choice /n /c 12 /m "Activation Choice : "%1
if "%errorlevel%"=="1" goto input
If "%errorlevel%"=="2" goto preset


:input
echo %errorlevel%

Echo Activate Windows
echo. =================================
set /p key="Input Windows KEY : "
echo. =================================

echo. %key%
echo. Press ENTER to continue if correct
pause
cscript %windir%\system32\slmgr.vbs /ipk %key%
ping /n 3 localhost>nul 2>&1
cscript %windir%\system32\slmgr.vbs  /ato
ping /n 3 localhost>nul 2>&1
cscript %windir%\system32\slmgr.vbs /xpr
ping /n 3 localhost>nul 2>&1
goto end



:preset

set group1=HR-16,DD-89,CC-05
set key1=AAAAA.BBBBB.CCCCC.DDDDD.EEEE
call :ActivateWindows "%group1%" %key1%

set group2=CF-05,RB-09,DS-60
set key2=BBBBB.CCCCC.DDDDD.EEEE.11111
call :ActivateWindows "%group2%" %key2%

set group3=AB-50,XY-23,LM-46
set key3=BBBBB.CCCCC.DDDDD.EEEE.22222
call :ActivateWindows "%group3%" %key3%

cscript %windir%\system32\slmgr.vbs /ato

endlocal
exit /b %errorlevel%

:ActivateWindows [group] [key]
for %%G in (%~1) do for %%K in (%~2) do (
    if "%computername:~0,5%"=="%%G" if "%computername:~-2%"=="-X" (
        echo Activating Group: %%G and Key: %%K
        cscript //nologo %windir%\system32\slmgr.vbs /ipk %%K
    )
)
goto :eof

答案1

@echo off
setlocal

set group1=HR-16,DD-89,CC-05
set key1=AAAAA.BBBBB.CCCCC.DDDDD.EEEE
call :ActivateWindows %group1% %key1%

set group2=CF-05,RB-09,NO-15
set key2=BBBBB.CCCCC.DDDDD.EEEE.11111
call :ActivateWindows %group2% %key2%

set group3=AB-50,XY-23,LM-46
set key3=BBBBB.CCCCC.DDDDD.EEEE.22222
call :ActivateWindows %group3% %key3%

cscript %windir%\system32\slmgr.vbs /ato

endlocal
exit /b %errorlevel%

:ActivateWindows [group] [key]
for %%G in (%~1) do for %%K in (%~2) do (
    if "%computername:~0,5%"=="%%G" if "%computername:~-2%"=="-X" (
        echo Activating Group: %%G and Key: %%K
        cscript //nologo %windir%\system32\slmgr.vbs /ipk %%K
    )
)
goto :eof

相关内容