千家信息网

Windows 设置 VMware workstation 虚拟机开机启动

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,日常工作中,由于条件以及成本的限制,我们会在自己本地电脑上安装虚拟机装服务器系统,来满足自己工作中对服务器的需求,但是很多时候,我们自己的电脑关机或者重启后,虚拟机并不会自行启动,需要我们手动启动虚拟
千家信息网最后更新 2025年01月31日Windows 设置 VMware workstation 虚拟机开机启动

日常工作中,由于条件以及成本的限制,我们会在自己本地电脑上安装虚拟机装服务器系统,来满足自己工作中对服务器的需求,但是很多时候,我们自己的电脑关机或者重启后,虚拟机并不会自行启动,需要我们手动启动虚拟机,如果我们只有一两台虚拟机,那没什么,启动很快,但是,如果我们有很多台虚拟机呢?10 台,20 台,甚至更多,难道我们还要手动一个个开机吗?那真是太让人头疼,又太浪费时间。下面,我们就讲下,如何让虚拟机在我们自己的主机开机时自动启动。

一、VMware workstation 虚拟机开机自启动的原理

要说 VMware workstation 虚拟机开机自启动,在 VMware workstation 图形界面是找不到设置方法的,但是图形界面找不到设置方法,不代表它就没有设置方法。

要设置开机自启动,首先,我们得了解下 VMware workstation 所提供的命令行工具 vmrun 。

1、我们进入到 VMware workstation 的安装目录,找一下 vmrun.exe 文件,如下图,正式这个工具,为我们提供了虚拟机的自启动方法。

2、将 VMware workstation 的安装目录添加到系统的环境变量,如下图,按照途中的顺序,依次操作即可

注意:在第 5 步时,需要找到自己的 VMware workstation 的安装目录,然后添加到这里。

3、在 DOS 界面中查看 vmrun 命令的语法格式及参数

Microsoft Windows [版本 10.0.17134.471](c) 2018 Microsoft Corporation。保留所有权利。C:\Users\CaoBo>vmrunvmrun version 1.17.0 build-10952284Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]AUTHENTICATION-FLAGS--------------------These must appear before the command and any command parameters.   -T  (ws|fusion||player)   -vp    -gu    -gp 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 VMunpause                  Path to vmx file     Unpause a VMSNAPSHOT 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 namedeleteSnapshot           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 nameGUEST 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 guestdirectoryExistsInGuest   Path to vmx file     Check if a directory exists in Guest OS                         Path to directory in guestsetSharedFolderState     Path to vmx file     Modify a Host-Guest shared folder                         Share name                         Host path                         writable | readonlyaddSharedFolder          Path to vmx file     Add a Host-Guest shared folder                         Share name                         New host pathremoveSharedFolder       Path to vmx file     Remove a Host-Guest shared folder                         Share nameenableSharedFolders      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 OSkillProcessInGuest       Path to vmx file     Kill a process in Guest OS                         process idrunScriptInGuest         Path to vmx file     Run a script in Guest OS                         [-noWait]                         [-activeWindow]                         [-interactive]                         Interpreter path                         Script textdeleteFileInGuest        Path to vmx file     Delete a file in Guest OS                         Path in guestcreateDirectoryInGuest   Path to vmx file     Create a directory in Guest OS                         Directory path in guestdeleteDirectoryInGuest   Path to vmx file     Delete a directory in Guest OS                         Directory path in guestCreateTempfileInGuest    Path to vmx file     Create a temporary file in Guest OSlistDirectoryInGuest     Path to vmx file     List a directory in Guest OS                         Directory path in guestCopyFileFromHostToGuest  Path to vmx file     Copy a file from host OS to guest OS                         Path on host                         Path in guestCopyFileFromGuestToHost  Path to vmx file     Copy a file from guest OS to host OS                         Path in guest                         Path on hostrenameFileInGuest        Path to vmx file     Rename a file in Guest OS                         Original name                         New nametypeKeystrokesInGuest    Path to vmx file     Type Keystrokes in Guest OS                         keystroke stringconnectNamedDevice       Path to vmx file     Connect the named device in the Guest OS                         device namedisconnectNamedDevice    Path to vmx file     Disconnect the named device in the Guest OS                         device namecaptureScreen            Path to vmx file     Capture the screen of the VM to a local file                         Path on hostwriteVariable            Path to vmx file     Write a variable in the VM state                         [runtimeConfig|guestEnv|guestVar]                         variable name                         variable valuereadVariable             Path to vmx file     Read a variable in the VM state                         [runtimeConfig|guestEnv|guestVar]                         variable namegetGuestIPAddress        Path to vmx file     Gets the IP address of the guest                         [-wait]GENERAL COMMANDS         PARAMETERS           DESCRIPTION----------------         ----------           -----------list                                          List all running VMsupgradevm                Path to vmx file     Upgrade VM file format, virtual hwinstallTools             Path to vmx file     Install Tools in GuestcheckToolsState          Path to vmx file     Check the current Tools statedeleteVM                 Path to vmx file     Delete a VMclone                    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 VMExamples: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" mySnapshotReverting to a snapshot with Workstation on a Windows host   vmrun -T ws revertToSnapshot "c:\my VMs\myVM.vmx" mySnapshotDeleting a snapshot with Workstation on a Windows host   vmrun -T ws deleteSnapshot "c:\my VMs\myVM.vmx" mySnapshotEnabling Shared Folders with Workstation on a Windows host   vmrun -T ws enableSharedFolders "c:\my VMs\myVM.vmx"

①、验证标志(AUTHENTICATION-FLAGS):

-T 代表目标类型,ws 表示工作站(即 Workstation ),一般使用此参数即可,其他很少使用,就不做解释了。当然还有 server、esx、vc 等等,这里没有列出。

②、电源指令(POWER COMMANDS):

start:开启虚拟机

stop:关闭虚拟机

reset:重置虚拟机

suspend:挂起虚拟机

③、快照指令(SNAPSHOT COMMANDS):

listSnapshots:列出虚拟机的快照数量及名称

snapshot:为虚拟机创建快照

deleteSnapshot:删除虚拟机的快照

revertToSnapshot:从一个快照恢复虚拟机

④、常用命令(GENERAL COMMANDS):

list:列出正在运行的虚拟机的数量及名称

upgradevm:升级虚拟机的文件格式,虚拟硬件

installTools:在 Guest 中安装工具

checkToolsState:检查工具状态

deleteVM:删除一个虚拟机

clone:克隆虚拟机


这里只介绍这么多命令,其他的命令很少用到,就不多做解释。

言归正传,我们要设置虚拟机开机自启动,就要使用上面 start 命令。那么,此命令如何使用呢,格式时这样的:

vmrun -T ws start "c:\my VMs\myVM.vmx"

"c:\my VMs\myVM.vmx" 该目录为虚拟机的工作目录

我们可以在这条命令后面加上 nogui ,表示无图形界面启动,而默认的启动模式是 gui 模式,即有图形界面启动。

vmrun -T ws start "c:\my VMs\myVM.vmx" nogui

最后,我们将该命令修改为自己的虚拟机工作目录后写到批处理文件中(如下图),添加开机启动脚本或者计划任务。


@echo off & setlocal enabledelayedexpansionvmrun -T ws start "I:\Documents\Virtual Machines\centos\centos.vmx" nogui

附上关机脚本:

@echo off & setlocal enabledelayedexpansionvmrun list > vmlist.txtfor %%i in (vmlist.txt) do (    set "f=%%i"    for /f "usebackq delims=" %%j in ("!f!") do set/a n+=1    for /f "delims=" %%m in ('"type "!f!"|more /E +1 & cd. 2^>!f!"') do set/a x+=1&if !x! leq !n! echo;%%m>>!f!    set/a n=0,x=0)for /f "delims=" %%a in (vmlist.txt) do (     vmrun -T ws stop "%%a" nogui    )pause


二、Windows 中设置虚拟机开机自启动

在 Windows 中设置虚拟机开机自启动又两种办法

①、用计划任务来实现。

创建计划任务:

如图,我们将计划任务的设置为使用最高权限运行,触发器设置为在系统启动时触发,而操作是启动批处理脚本。

计划任务创建完成后,我们可以将自己的主机重启,看看我们的虚拟机是否有自动开机。

②、用组策略来实现

在 <运行> 中输入 gpedit.msc 打开组策略,如下图

在组策略中,依次选择 <用户配置> <脚本(登陆/注销)>,双击 <登陆>,如下图

在 <登陆 属性> 对话框中添加我们之前写好的启动脚本即可,如下图

设置完之后,我们重启主机,再看看虚拟机有没有启动。

三、总结

在日常工作中,我们解决问题的办法可能不止一种,但往往由于我们所学知识的局限性性,导致我们的思路会很单一,因此,我们就要通过不断的学习,来拓宽我们视界,才能更好的提高我们的深获质量。

0