快速启动 VMware 虚拟机

借助 vmrun.exe


vmrun.exe 基础

命令行中进入 VMware Workstation Pro 安装目录,不带参数运行 vmrun.exe

(为了不影响阅读,该命令帮助已移动到了文章末尾部分)

也可以在命令行中运行 vmware.exe ,其命令选项及使用实例可以于在线文档中看到,但是该 .exe 可接收参数有限,其能力远不如 vmrun.exe


提出需求

  • 需求一
  • 我有一个 Windows 7 虚拟机,安装了一些常用的但是自认为不够单纯的软件,比如 xx、xxxx等。不求快速启动能直达应用本身,但需要有这么一个图标,可放在桌面以双击或者放在快速启动栏以通过 Win + num 唤起,从而打开 Windows 7
  • 需求二
  • 有一个 Ubuntu Server 虚拟机,提供有纯粹的 Linux 环境,使用时通过 ssh 远程连接。一般情况下,打开该虚拟机需要首先打开 VMware Workstation Pro ,而后才能打开该虚机,最后当虚机打开后,为了使其在后台运行(这样没有图形界面(CLI),会少些 消耗),还需要关闭 VMware Workstation Pro (注意:需要在 编辑 -> 首选项 里勾选 -> Workstation 关闭后保持虚拟机运行)。现在需要有这么一个图标,通过其便可在后台启动该虚机。不过关闭或挂起,还是需要手动进行的。

解决思路

编写 .bat 脚本以运行 vmrun.exe 命令来实现相应动作。但是运行 .bat 脚本会打开一个cmd黑框,看起来很不舒服。这个问题可以借助.vbs脚本来解决:在 .vbs脚本中静默(后台)调用.bat 脚本。
当脚本准备好后,创建脚本的快捷方式,修改其名称及图标即可。
随后便可以将该快捷方式放到桌面、“开始”菜单等处(暂时不知道如何固定在快速启动栏)。


具体实现

以需求二的为例,该需求实现步骤及效果如下:

步骤

  • 确定相关路径:
  • vmrun.exe 路径:D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe
  • 虚拟机配置文件(.vmx)路径:D:\Virtual Machines\usvr_free\usvr_free.vmx
  • 参照 vmrun.exe 基础 编写 start_nogui.bat 脚本如下:
"D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" -T ws start "D:\Virtual Machines\usvr_free\usvr_free.vmx" nogui

如果路径带空格,则路径整体需要用双引号包括,否则无法正常识别。

  • 编写 start_nogui.vbs 脚本如下:
set ws=wscript.createobject("wscript.shell")
ws.run "start_nogui.bat /start",0
  • 在 .vbs脚本文件之上右键,后点击“创建快捷方式”,或按住 Alt 键并拖动以创建快捷方式。
  • 从互联网上搜索 ubuntu 的图标(.ico格式)
  • 选中快捷方式,Alt Enter 以打开其属性对话框,修改其图标。
  • 将快捷方式复制到桌面。

效果

虚拟机如何启动Xmodem接收软件_虚拟机如何启动Xmodem接收软件

 

后记

更好的实现

对于需求二的更好的实现:双击快捷方式以执行脚本,当虚机打开时,将其挂起;否则将其打开。同时将操作及时间记录到一文件中。

效果

虚拟机如何启动Xmodem接收软件_ubuntu_02

步骤

首先将 .bat 文件中内容修改为:

set VMRUN="D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
set Machine="D:\Virtual Machines\usvr_free\usvr_free.vmx"
set LOG_FILE=".\operationLog.txt"

%VMRUN% list | find %Machine%

if errorlevel 1 (
	echo %date% %time% __start__ %Machine% >> %LOG_FILE%
	%VMRUN% -T ws start %Machine% nogui
) else (
	echo %date% %time% _suspend_ %Machine% >> %LOG_FILE%
	%VMRUN% -T ws suspend %Machine%
)

然后修改文件名,最后对应地,修改 .vbs 中调用的文件名,以及快捷方式指向的文件名。

 

vmrun 的命令帮助

vmrun version 1.17.0 build-14665864

Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]



AUTHENTICATION-FLAGS
--------------------
These must appear before the command and any command parameters.

   -T <hostType> (ws|fusion||player)
   -vp <password for encrypted virtual machine>
   -gu <userName in guest OS>
   -gp <password in guest OS>



POWER COMMANDS           PARAMETERS           DESCRIPTION
--------------           ----------           -----------
start                    Path to vmx file     Start a VM or Team
                         [gui|nogui]

stop                     Path to vmx file     Stop a VM or Team
                         [hard|soft]

reset                    Path to vmx file     Reset a VM or Team
                         [hard|soft]

suspend                  Path to vmx file     Suspend a VM or Team
                         [hard|soft]

pause                    Path to vmx file     Pause a VM

unpause                  Path to vmx file     Unpause a VM



SNAPSHOT COMMANDS        PARAMETERS           DESCRIPTION
-----------------        ----------           -----------
listSnapshots            Path to vmx file     List all snapshots in a VM
                         [showTree]

snapshot                 Path to vmx file     Create a snapshot of a VM
                         Snapshot name

deleteSnapshot           Path to vmx file     Remove a snapshot from a VM
                         Snapshot name
                         [andDeleteChildren]

revertToSnapshot         Path to vmx file     Set VM state to a snapshot
                         Snapshot name



GUEST OS COMMANDS        PARAMETERS           DESCRIPTION
-----------------        ----------           -----------
runProgramInGuest        Path to vmx file     Run a program in Guest OS
                         [-noWait]
                         [-activeWindow]
                         [-interactive]
                         Complete-Path-To-Program
                         [Program arguments]

fileExistsInGuest        Path to vmx file     Check if a file exists in Guest OS
                         Path to file in guest

directoryExistsInGuest   Path to vmx file     Check if a directory exists in Guest OS
                         Path to directory in guest

setSharedFolderState     Path to vmx file     Modify a Host-Guest shared folder
                         Share name
                         Host path
                         writable | readonly

addSharedFolder          Path to vmx file     Add a Host-Guest shared folder
                         Share name
                         New host path

removeSharedFolder       Path to vmx file     Remove a Host-Guest shared folder
                         Share name

enableSharedFolders      Path to vmx file     Enable shared folders in Guest
                         [runtime]

disableSharedFolders     Path to vmx file     Disable shared folders in Guest
                         [runtime]

listProcessesInGuest     Path to vmx file     List running processes in Guest OS

killProcessInGuest       Path to vmx file     Kill a process in Guest OS
                         process id

runScriptInGuest         Path to vmx file     Run a script in Guest OS
                         [-noWait]
                         [-activeWindow]
                         [-interactive]
                         Interpreter path
                         Script text

deleteFileInGuest        Path to vmx file     Delete a file in Guest OS
                         Path in guest

createDirectoryInGuest   Path to vmx file     Create a directory in Guest OS
                         Directory path in guest

deleteDirectoryInGuest   Path to vmx file     Delete a directory in Guest OS
                         Directory path in guest

CreateTempfileInGuest    Path to vmx file     Create a temporary file in Guest OS

listDirectoryInGuest     Path to vmx file     List a directory in Guest OS
                         Directory path in guest

CopyFileFromHostToGuest  Path to vmx file     Copy a file from host OS to guest OS
                         Path on host
                         Path in guest

CopyFileFromGuestToHost  Path to vmx file     Copy a file from guest OS to host OS
                         Path in guest
                         Path on host

renameFileInGuest        Path to vmx file     Rename a file in Guest OS
                         Original name
                         New name

typeKeystrokesInGuest    Path to vmx file     Type Keystrokes in Guest OS
                         keystroke string

connectNamedDevice       Path to vmx file     Connect the named device in the Guest OS
                         device name

disconnectNamedDevice    Path to vmx file     Disconnect the named device in the Guest OS
                         device name

captureScreen            Path to vmx file     Capture the screen of the VM to a local file
                         Path on host

writeVariable            Path to vmx file     Write a variable in the VM state
                         [runtimeConfig|guestEnv|guestVar]
                         variable name
                         variable value

readVariable             Path to vmx file     Read a variable in the VM state
                         [runtimeConfig|guestEnv|guestVar]
                         variable name

getGuestIPAddress        Path to vmx file     Gets the IP address of the guest
                         [-wait]



GENERAL COMMANDS         PARAMETERS           DESCRIPTION
----------------         ----------           -----------
list                                          List all running VMs

upgradevm                Path to vmx file     Upgrade VM file format, virtual hw

installTools             Path to vmx file     Install Tools in Guest

checkToolsState          Path to vmx file     Check the current Tools state

deleteVM                 Path to vmx file     Delete a VM

clone                    Path to vmx file     Create a copy of the VM
                         Path to destination vmx file
                         full|linked
                         [-snapshot=Snapshot Name]
                         [-cloneName=Name]



Template VM COMMANDS     PARAMETERS           DESCRIPTION
---------------------    ----------           -----------
downloadPhotonVM         Path for new VM      Download Photon VM





Examples:


Starting a virtual machine with Workstation on a Windows host
   vmrun -T ws start "c:\my VMs\myVM.vmx"


Running a program in a virtual machine with Workstation on a Windows host with Windows guest
   vmrun -T ws -gu guestUser -gp guestPassword runProgramInGuest "c:\my VMs\myVM.vmx" "c:\Program Files\myProgram.exe"


Creating a snapshot of a virtual machine with Workstation on a Windows host
   vmrun -T ws snapshot "c:\my VMs\myVM.vmx" mySnapshot


Reverting to a snapshot with Workstation on a Windows host
   vmrun -T ws revertToSnapshot "c:\my VMs\myVM.vmx" mySnapshot


Deleting a snapshot with Workstation on a Windows host
   vmrun -T ws deleteSnapshot "c:\my VMs\myVM.vmx" mySnapshot


Enabling Shared Folders with Workstation on a Windows host
   vmrun -T ws enableSharedFolders "c:\my VMs\myVM.vmx"