需求:
我用自己的电脑 安装了k8s集群做测试,
每次都手动启动,有点麻烦, 所以就写了一个脚本,批量启动,停止。
在批处理(batch)脚本中,我们可以使用 REM 或 :: 来添加注释。
1- 注释
for 循环中 必须用REM 来注释, 否则出错
2- 获取变量值
%num%
3- 定义全局变量:
::定义变量
@echo off
set var1=D:\VM\5nodesk8s\10.0.0.108\10.0.0.108-master1.vmx
set var2=D:\VM\5nodesk8s\10.0.0.109\10.0.0.109-master2.vmx
脚本文件
(1)---for 循环的 (比较推荐)-------------------------
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
::定义变量
@echo off
set var1=D:\VM\5nodesk8s\10.0.0.108\10.0.0.108-master1.vmx
set var2=D:\VM\5nodesk8s\10.0.0.109\10.0.0.109-master2.vmx
set var3=D:\VM\5nodesk8s\10.0.0.110\10.0.0.110-master3.vmx
set var4=D:\VM\5nodesk8s\10.0.0.111-worker1\10.0.0.111-worker1.vmx
set var5=D:\VM\5nodesk8s\10.0.0.112-worker2\10.0.0.112-worker2.vmx
:: 定义 循环次数
set num=5
echo %var1%, %var2%, %var3%
@echo off
:start
echo 1(启动 k8s集群)
echo 2 (关闭 k8s集群)
set /P var="请选择你的设置:"
if %var%==1 goto start
if %var%==2 goto stop
:start
cls
::-------------------start 开始-------------------------------------by baimeidashu.com ------
@echo off
setlocal enabledelayedexpansion
for /L %%i in (1,1,%num%) do (
echo start !var%%i!
REM "D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" -x !var%%i!
"D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" -x !var%%i!
)
endlocal
::-------------------start 结束----------------------------------by baimeidashu---------
echo ** 启动完成 **
echo ------------------------------------------
pause
exit
:stop
cls
::-------------------stop 开始--------------------------------------by baimeidashu-----
@echo off
setlocal enabledelayedexpansion
for /L %%i in (1,1,%num%) do (
echo stop !var%%i!
"D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" stop "!var%%i!"
)
endlocal
::-------------------stop 结束----------------------------------------by baimeidashu---
echo ** stop 完成 **
echo ------------------------------------------
pause
exit
(2)--未用 for 循环-----------------
脚本文件:
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
@echo off
:start
echo 1(启动 k8s集群)
echo 2 (关闭 k8s集群)
set /P var="请选择你的设置:"
if %var%==1 goto start
if %var%==2 goto stop
:start
cls
ECHO "start 白眉大叔-master231"
"D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" -x "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.231-master231\白眉大叔-master231.vmx"
ECHO "start 白眉大叔-master232.vmx"
"D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" -x "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.232-master232\白眉大叔-master232.vmx"
ECHO "start 白眉大叔-master233.vmx"
"D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" -x "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.233-master233\白眉大叔-master233.vmx"
echo ** 启动完成 **
echo ------------------------------------------
pause
exit
:stop
cls
ECHO "stop 白眉大叔-master231"
"D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" stop "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.231-master231\白眉大叔-master231.vmx"
ECHO "stop 白眉大叔-master232.vmx"
"D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" stop "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.232-master232\白眉大叔-master232.vmx"
ECHO "stop 白眉大叔-master233.vmx"
"D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" stop "D:\3baimeidashu\1linuxVM\4k8s\baimei-10.0.0.233-master233\白眉大叔-master233.vmx"
echo ** 关闭完成 stop **
echo ------------------------------------------
pause
exit
双击运行程序:
选择 1 启动
参考:
欢迎来撩 : 汇总all