windows(win10)批處理腳本
打開vm虛擬機的服務,並且開啟無界面虛擬機
@echo off
net start "vds"
net start "VMAuthdService"
net start "VMnetDHCP"
net start "VMware NAT Service"
net start "VMUSBArbService"
net start "VMwareHostd"
"D:\ApplicationFiles\System\VMware Workstation\vmrun.exe" start "F:\virtualMachine\ubuntu15.1X64\ubuntu15.1X64.vmx" nogui
保存為startvm.bat
@echo off
"D:\ApplicationFiles\System\VMware Workstation\vmrun.exe" stop "F:\virtualMachine\ubuntu15.1X64\ubuntu15.1X64.vmx" nogui
net stop "vds"
net stop "VMnetDHCP"
net stop "VMware NAT Service"
net stop "VMwareHostd"
net stop "VMUSBArbService"
net stop "VMAuthdService"
保存為shutdownvm.bat
說明:
D:\ApplicationFiles\System\VMware Workstation\vmrun.exe 為虛擬機安裝目錄/ps:如果你把vmrun路徑加入了環境變量path后,可以直接使用命令vmrun
F:\virtualMachine\ubuntu15.1X64\ubuntu15.1X64.vmx 為虛擬機文件位置
生成的文件創建快捷方式,右鍵-屬性-高級,設置下管理員權限運行,以后雙擊即可執行
打開還要等個一分鍾左右啟動好,具體看你的系統性能
接下來就可以正常ssh連接了
-------------------------------------------
下面重點說一下用到的知識點原理
一,首先說一下,window上面如何操作服務(win10 x64)
windows上使用sc命令net命令設置操作服務,批處理關閉服務命令 開啟服務命令
使用sc命令:
1 修改服務啟動類型
sc config 服務名 start= demand (設置服務為手動啟動)
start= auto (設置服務為自動啟動)
start= disabled (設置服務為禁用)
2 安裝服務:sc create 服務名
3 停止服務:sc stop 服務名
4 啟動服務 : sc start 服務名
5 卸載服務:sc delete 服務名(卸載前先停止服務)
使用net命令
查看運行的服務: net start
啟動服務: net start 服務名
停止服務: net stop 服務名
卸載服務: 服務名 -uninstall (卸載前先停止服務)
注意注意注意:
start后面不能有空格 =后面的參數需要有一個空格
不是一個單詞的服務名字,需要用引號
綜上所述我們可以使用:
啟動服務: net start 服務名 或者 sc start 服務名
停止服務: net stop 服務名 或者 sc stop 服務名
二 查看虛擬機上面的服務
[SC] StartService: OpenService 失敗 1060:指定的服務未安裝。怎么辦?
虛擬機安裝好以后,有以下幾個服務
注意:服務的名字不一定是顯示的名字,右鍵屬性查看出來的服務名稱才是真正的名字
這就是找不到服務的意思
vm幾個服務的名字為:
"vds"
"VMAuthdService"
"VMnetDHCP"
"VMware NAT Service"
"VMUSBArbService"
"VMwareHostd"
三 新建腳本
@echo off
net start "vds"
net start "VMAuthdService"
net start "VMnetDHCP"
net start "VMware NAT Service"
net start "VMUSBArbService"
net start "VMwareHostd"
@echo off
net stop "vds"
net stop "VMnetDHCP"
net stop "VMware NAT Service"
net stop "VMwareHostd"
net stop "VMUSBArbService"
net stop "VMAuthdService"
分別保存為
startvm.bat
shutdownvm.bat
這樣批處理腳本就創建好了
雙擊腳本執行,發現服務沒被啟動,在腳本后面加一個pause,這樣子窗口就不會立即消失了
拒絕訪問肯定是權限的問題了,可以右鍵--管理員權限運行,但是比較麻煩
可以創建一個快捷方式
右鍵,設置以后,雙擊就可以管理員身份運行了
開啟虛擬機
vmrun start "F:\virtualMachine\ubuntu15.1X64/ubuntu15.1X64.vmx" nogui
附錄:
windows命令行里面直接輸入 sc和net (net help)查看命令信息
切換到vm虛擬機安裝路徑,輸入vmrun,可以查看 vmrun命令詳細信息
以下摘自系統
-----------系統給出來的提示信息------win10 命令行下-----------------
C:\WINDOWS\system32>sc
描述:
SC 是用來與服務控制管理器和服務進行通信
的命令行程序。
用法:
sc <server> [command] [service name] <option1> <option2>...
<server> 選項的格式為 "\\ServerName"
可通過鍵入以下命令獲取有關命令的更多幫助: "sc [command]"
命令:
query-----------查詢服務的狀態,
或枚舉服務類型的狀態。
queryex---------查詢服務的擴展狀態,
或枚舉服務類型的狀態。
start-----------啟動服務。
pause-----------向服務發送 PAUSE 控制請求。
interrogate-----向服務發送 INTERROGATE 控制請求。
continue--------向服務發送 CONTINUE 控制請求。
stop------------向服務發送 STOP 請求。
config----------更改服務的配置(永久)。
description-----更改服務的描述。
failure---------更改失敗時服務執行的操作。
failureflag-----更改服務的失敗操作標志。
sidtype---------更改服務的服務 SID 類型。
privs-----------更改服務的所需特權。
managedaccount--更改服務以將服務帳戶密碼
標記為由 LSA 管理。
qc--------------查詢服務的配置信息。
qdescription----查詢服務的描述。
qfailure--------查詢失敗時服務執行的操作。
qfailureflag----查詢服務的失敗操作標志。
qsidtype--------查詢服務的服務 SID 類型。
qprivs----------查詢服務的所需特權。
qtriggerinfo----查詢服務的觸發器參數。
qpreferrednode--查詢服務的首選 NUMA 節點。
qmanagedaccount-查詢服務是否將帳戶
與 LSA 管理的密碼結合使用。
qprotection-----查詢服務的進程保護級別。
delete----------(從注冊表中)刪除服務。
create----------創建服務(並將其添加到注冊表中)。
control---------向服務發送控制。
sdshow----------顯示服務的安全描述符。
sdset-----------設置服務的安全描述符。
showsid---------顯示與任意名稱對應的服務 SID 字符串。
triggerinfo-----配置服務的觸發器參數。
preferrednode---設置服務的首選 NUMA 節點。
GetDisplayName--獲取服務的 DisplayName。
GetKeyName------獲取服務的 ServiceKeyName。
EnumDepend------枚舉服務依賴關系。
以下命令不需要服務名稱:
sc <server> <command> <option>
boot------------(ok | bad)指示是否應將上一次啟動另存為
最近一次已知的正確啟動配置
Lock------------鎖定服務數據庫
QueryLock-------查詢 SCManager 數據庫的 LockStatus
示例:
sc start MyService
QUERY 和 QUERYEX 選項:
如果查詢命令帶服務名稱,將返回
該服務的狀態。其他選項不適合這種
情況。如果查詢命令不帶參數或
帶下列選項之一,將枚舉此服務。
type= 要枚舉的服務的類型(driver, service, userservice, all)
(默認 = service)
state= 要枚舉的服務的狀態 (inactive, all)
(默認 = active)
bufsize= 枚舉緩沖區的大小(以字節計)
(默認 = 4096)
ri= 開始枚舉的恢復索引號
(默認 = 0)
group= 要枚舉的服務組
(默認 = all groups)
語法示例
sc query - 枚舉活動服務和驅動程序的狀態
sc query eventlog - 顯示 eventlog 服務的狀態
sc queryex eventlog - 顯示 eventlog 服務的擴展狀態
sc query type= driver - 僅枚舉活動驅動程序
sc query type= service - 僅枚舉 Win32 服務
sc query state= all - 枚舉所有服務和驅動程序
sc query bufsize= 50 - 枚舉緩沖區為 50 字節
sc query ri= 14 - 枚舉時恢復索引 = 14
sc queryex group= "" - 枚舉不在組內的活動服務
sc query type= interact - 枚舉所有不活動服務
sc query type= driver group= NDIS - 枚舉所有 NDIS 驅動程序
C:\WINDOWS\system32>
--------------------------------------------------------------------------
C:\WINDOWS\system32>net ?
此命令的語法是:
NET
[ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
HELPMSG | LOCALGROUP | PAUSE | SESSION | SHARE | START |
STATISTICS | STOP | TIME | USE | USER | VIEW ]
C:\WINDOWS\system32>net help
此命令的語法是:
NET HELP
command
-或-
NET command /HELP
可用的命令為:
NET ACCOUNTS NET HELPMSG NET STATISTICS
NET COMPUTER NET LOCALGROUP NET STOP
NET CONFIG NET PAUSE NET TIME
NET CONTINUE NET SESSION NET USE
NET FILE NET SHARE NET USER
NET GROUP NET START NET VIEW
NET HELP
NET HELP NAMES 解釋 NET HELP 語法行中的不同類型名稱。
NET HELP SERVICES 列出可以啟動的一些服務。
NET HELP SYNTAX 解釋如何讀取 NET HELP 語法行。
NET HELP 命令 | MORE 顯示幫助,一次顯示一屏。
============vmrun命令========
D:\ApplicationFiles\System\VMware Workstation>vmrun
vmrun version 1.15.0 build-2985596
Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]
AUTHENTICATION-FLAGS
--------------------
These must appear before the command and any command parameters.
-h <hostName> (not needed for Workstation)
-P <hostPort> (not needed for Workstation)
-T <hostType> (ws|server|server1|fusion|esx|vc|player)
for example, use '-T server' for Server 2.0
use '-T server1' for Server 1.0
use '-T ws' for VMware Workstation
use '-T ws-shared' for VMware Workstation (shared mode)
use '-T esx' for VMware ESX
use '-T vc' for VMware vCenter Server
-u <userName in host OS> (not needed for Workstation)
-p <password in host OS> (not needed for Workstation)
-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
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
register Path to vmx file Register a VM
unregister Path to vmx file Unregister a VM
listRegisteredVM List registered VMs
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]
Examples:
Starting a virtual machine with Workstation on a Windows host
vmrun -T ws start "c:\my VMs\myVM.vmx"
Stopping a virtual machine on an ESX host
vmrun -T esx -h https://myHost.com/sdk -u hostUser -p hostPassword stop "[storage1] vm/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"
Running a program in a virtual machine with Server on a Linux host with Linux guest
vmrun -T server -h https://myHost.com:8333/sdk -u hostUser -p hostPassword -gu guestUser -gp guestPassword runProgramInGuest "[standard] vm/myVM.vmx" /usr/bin/X11/xclock -display :0
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"
D:\ApplicationFiles\System\VMware Workstation>









