VMware_EXSI
EXSI
ESXi6.0中将虚拟机从厚置备转换为精简置备
VMware Workstation
Windows下VMware虚机运行状态自动启动脚本
本文档使用 MrDoc 发布
-
+
home page
Windows下VMware虚机运行状态自动启动脚本
# 作用 通过在windows宿主机上添加定时任务计划,实现定时检查指定虚机的运行状态,如果没有在运行状态就直接启动,无论状态是在关机状态或者暂停状态 # 脚本源码 ```bat @echo off setlocal :: Set the virtual machine's path set VM_PATH="D:\Vhost\CentOS7-1\Template.vmx" :: Set the interval (in seconds) for checking the virtual machine status set INTERVAL=60 :: Set the lock file path set LOCK_FILE=%TEMP%\vm_status_lock.txt for /f "tokens=1-2 delims= " %%a in ('date /t') do (set mydate=%%a) for /f "tokens=1-3 delims=:." %%a in ("%TIME%") do (set mytime=%%a:%%b:%%c) :: Check if the lock file exists, indicating that the script is already running if exist %LOCK_FILE% ( echo [%mydate% %mytime%] Script is already running. >> "%PARENT_DIR%StartupLog.txt" exit ) :: Create a lock file to prevent multiple instances of the script echo vmware_vhost_running_check_script > %LOCK_FILE% echo [%mydate% %mytime%] Creating lock file... >> "%PARENT_DIR%StartupLog.txt" :LOOP :: Get the current date and time for /f "tokens=1-2 delims= " %%a in ('date /t') do (set mydate=%%a) for /f "tokens=1-3 delims=:." %%a in ("%TIME%") do (set mytime=%%a:%%b:%%c) :: Get the parent directory of the virtual machine path for %%A in (%VM_PATH%) do set PARENT_DIR=%%~dpA cd /d "C:\Program Files (x86)\VMware\VMware Workstation" :: Check the virtual machine status vmrun.exe -T ws list > "%PARENT_DIR%temp.txt" :: Check if the status output file contains the virtual machine path findstr /i /C:%VM_PATH% "%PARENT_DIR%temp.txt" > nul :: If the virtual machine is not running, start it and log the action if errorlevel 1 ( echo [%mydate% %mytime%] Virtual machine is not running: %VM_PATH% >> "%PARENT_DIR%StartupLog.txt" echo [%mydate% %mytime%] Starting the virtual machine... >> "%PARENT_DIR%StartupLog.txt" vmrun.exe -T ws start %VM_PATH% nogui :: Log the action to the virtual machine's working directory echo [%mydate% %mytime%] Virtual machine startup success >> "%PARENT_DIR%StartupLog.txt" ) else ( echo [%mydate% %mytime%] Virtual machine is already running, no need to start. >> "%PARENT_DIR%StartupLog.txt" ) :: Clean up temporary files :: del "%PARENT_DIR%temp.txt" :: Wait for the specified interval before checking again timeout /t %INTERVAL% > nul :: Go back to the loop goto LOOP :end :: Delete the lock file to release the lock if exist %LOCK_FILE% ( echo [%mydate% %mytime%] Deleting lock file... >> "%PARENT_DIR%StartupLog.txt" del %LOCK_FILE% ) endlocal exit ```
Nathan
Nov. 9, 2023, 12:36 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
Markdown文件
PDF文件
Docx文件
share
link
type
password
Update password