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

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

1、VMware workstation 虛擬機開機自啓動的原理服務器

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

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

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

image

二、將 VMware workstation 的安裝目錄添加到系統的環境變量,以下圖,按照途中的順序,依次操做便可學習

image

注意:在第 5 步時,須要找到本身的 VMware workstation 的安裝目錄,而後添加到這裏。ui

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

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 等等,這裏沒有列出。3d

②、電源指令(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


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

附上關機腳本:

@echo off & setlocal enabledelayedexpansion
vmrun list > vmlist.txt
for %%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


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

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

①、用計劃任務來實現。

建立計劃任務:

imageimageimage

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

計劃任務建立完成後,咱們能夠將本身的主機重啓,看看咱們的虛擬機是否有自動開機。

②、用組策略來實現

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

image

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

image

在 <登錄 屬性> 對話框中添加咱們以前寫好的啓動腳本便可,以下圖

image

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

3、總結

在平常工做中,咱們解決問題的辦法可能不止一種,但每每因爲咱們所學知識的侷限性性,致使咱們的思路會很單一,所以,咱們就要經過不斷的學習,來拓寬咱們視界,才能更好的提升咱們的深獲質量。

相關文章
相關標籤/搜索