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>