主机关闭或注销时自动快照或暂停

主机关闭或注销时自动快照或暂停

我的主要开发环境是运行在强大的 Windows 10 Pro 主机(64GB RAM、16 核 i9、1TB SSD 等)上的 Ubuntu VM 客户机。这是我迄今为止拥有的最好的开发设置,但有一个例外:Windows Update 会定期重启我的机器。这意味着我会丢失在 VM 中执行的任何操作的状态,这很糟糕,因为我可能会在调试会话中途离开工作。

如何配置 VMWare 以使其在(主机)操作系统指示 VMWare 关闭时自动拍摄快照?

我看到 VMWare 论坛上有各种脚本(似乎是 Powershell?),所以如果有人向我提供 API 或 VMWare Powershell SDK,我可能可以破解一些东西,但如果此功能已经内置,当然会更顺畅。我发现的唯一设置是处理 VMWare 在客户机关机时对快照的操作(选项 -> 快照),而不是主机关机时要做什么。

我认为将监视器挂接到 Windows 事件日志的系统部分就可以解决问题,但是听起来相当复杂......

相关脚本提示

答案1

经过反复考虑,我拼凑了一个解决方案并将其发布到 GitHub。安装是半自动化的:下载两个文件并右键单击一个 xml 文件。

脚本响应的 Windows 事件列表(从 Windows 事件日志中查看)如下:

Log:System
Source:User32
Event ID:1074
Log:  Microsoft-Windows-Winlogon/Operational
Source:Winlogon
Event ID:7002
Log:  Microsoft-Windows-Eventlog-ForwardingPlugin/Operational
Source:Eventlog-ForwardingPlugin
Event ID:6005
Log:  Microsoft-Windows-Eventlog-ForwardingPlugin/Operational
Source:Eventlog-ForwardingPlugin
Event ID:6006
Log:Security
Source:Microsoft Windows security auditing.
Event ID:4647

脚本如下所示(截至 2019 年 9 月):

@echo off
echo SuspendRunningVMs Command (x64)...
echo Ideas for improving this? Visit https://github.com/fatso83/vmware-auto-suspend

SETLOCAL
REM Specify where vmrun.exe can be located
SET WSPath="C:\Program Files (x86)\VMware\VMware Workstation"

REM Get the list of currently running VMs
%WSPath%\vmrun.exe list | FIND /V "Total running VMs:" > %temp%\vmlist.txt

REM Suspend all running VMs
FOR /F "delims=*" %%v IN (%temp%\vmlist.txt) DO CALL :SuspendVM "%%v"

:WaitLoop
echo Waiting for the VMs to suspend...
REM Pause until no more VMs are running
%WSPath%\vmrun.exe list | FIND "Total running VMs: 0"
IF NOT ERRORLEVEL 1 GOTO End
timeout /t 10 /nobreak
GOTO WaitLoop
 
:End
echo End of script; all VMs suspended.
ENDLOCAL
GOTO :EOF
 
REM Suspend a VM
:SuspendVM
REM Suspend any running VM.  Workaround a "vmrun list" quirk that outputs
REM a blank line, by not trying to suspend a blank VM
IF %1x==x GOTO :EOF
echo Suspending VM %1
%WSPath%\vmrun.exe suspend %1
REM Allow some time after suspend call (allow disk to write vmem).
echo Wait a little bit for the VM to commit...
timeout /t 15 /nobreak
GOTO :EOF
 
REM Resume a VM (not used now, but may have use in future)
:ResumeVM
REM Resume any suspended VM.  Workaround a "vmrun list" quirk that outputs
REM a blank line, by not trying to start a blank VM
IF %1x==x GOTO :EOF
echo Starting VM %1
%WSPath%\vmrun.exe start %1
GOTO :EOF
 
:EOF

编辑: 我删除了以下事件,因为它导致一些 问题

Log:Security
Source:Microsoft Windows security auditing.
Event ID:4634

相关内容