Windows 設置 VMware workstation 虛擬機開機啓動

       日常工作中,由於條件以及成本的限制,我們會在自己本地電腦上安裝虛擬機裝服務器系統,來滿足自己工作中對服務器的需求,但是很多時候,我們自己的電腦關機或者重啓後,虛擬機並不會自行啓動,需要我們手動啓動虛擬機,如果我們只有一兩臺虛擬機,那沒什麼,啓動很快,但是,如果我們有很多臺虛擬機呢?10 臺,20 臺,甚至更多,難道我們還要手動一個個開機嗎?那真是太讓人頭疼,又太浪費時間。下面,我們就講下,如何讓虛擬機在我們自己的主機開機時自動啓動。

一、VMware workstation 虛擬機開機自啓動的原理

要說 VMware workstation 虛擬機開機自啓動,在 VMware workstation 圖形界面是找不到設置方法的,但是圖形界面找不到設置方法,不代表它就沒有設置方法。

要設置開機自啓動,首先,我們得了解下 VMware workstation 所提供的命令行工具 vmrun 。

1、我們進入到 VMware workstation 的安裝目錄,找一下 vmrun.exe 文件,如下圖,正式這個工具,爲我們提供了虛擬機的自啓動方法。

image

2、將 VMware workstation 的安裝目錄添加到系統的環境變量,如下圖,按照途中的順序,依次操作即可

image

注意:在第 5 步時,需要找到自己的 VMware workstation 的安裝目錄,然後添加到這裏。

3、在 DOS 界面中查看 vmrun 命令的語法格式及參數

Microsoft Windows [版本 10.0.17134.471]
(c) 2018 Microsoft Corporation。保留所有權利。

C:\Users\CaoBo>vmrun

vmrun version 1.17.0 build-10952284

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"

①、驗證標誌(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

最後,我們將該命令修改爲自己的虛擬機工作目錄後寫到批處理文件中(如下圖),添加開機啓動腳本或者計劃任務。

image

二、Windows 中設置虛擬機開機自啓動

在 Windows 中設置虛擬機開機自啓動又兩種辦法

①、用計劃任務來實現。

創建計劃任務:

imageimageimage

如圖,我們將計劃任務的設置爲使用最高權限運行,觸發器設置爲在系統啓動時觸發,而操作是啓動批處理腳本。

計劃任務創建完成後,我們可以將自己的主機重啓,看看我們的虛擬機是否有自動開機。

②、用組策略來實現

在 <運行> 中輸入 gpedit.msc 打開組策略,如下圖

image

在組策略中,依次選擇 <用戶配置>  <Windows 設置> <腳本(登陸/註銷)>,雙擊 <登陸>,如下圖

image

在 <登陸 屬性> 對話框中添加我們之前寫好的啓動腳本即可,如下圖

image

設置完之後,我們重啓主機,再看看虛擬機有沒有啓動。

三、總結

在日常工作中,我們解決問題的辦法可能不止一種,但往往由於我們所學知識的侷限性性,導致我們的思路會很單一,因此,我們就要通過不斷的學習,來拓寬我們視界,才能更好的提高我們的深獲質量。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章