|
1、列出所有虚拟机
import console;
import com;
//接口文档:https://www.virtualbox.org/sdkref/interface_i_virtual_box.html
var vbox = com.CreateObject("VirtualBox.VirtualBox")
var vSession = com.CreateObject("VirtualBox.Session")
for i,machines in table.eachIndex( vbox.Machines ){
console.log( machines.name, machines.state == vbox.MachineState_Running ? "运行中" : "已关闭")
}
console.pause()
2、启动、关闭虚拟机: import console;
import com;
//接口文档:https://www.virtualbox.org/sdkref/interface_i_virtual_box.html
var vbox = com.CreateObject("VirtualBox.VirtualBox")
var vSession = com.CreateObject("VirtualBox.Session")
//获取虚拟机,参数填VirtualBox里显示的虚拟机名称
var vm = vbox.findMachine ("xp-big5")
//GUI模式下启动虚拟机
var vProcess = vm.launchVMProcess(vSession,"gui")
//等待操作完成
vProcess.waitForCompletion(-1)
//检测操作是否成功
if (vProcess.getResultCode() != 0) {
console.log("启动虚拟机失败")
return console.pause()
}
//关机
vProcess = vSession.getConsole().powerDown();
//等待操作完成
vProcess.waitForCompletion(-1);
//检测操作是否成功
if (vProcess.getResultCode() != 0) { // we failed to stop the VM
console.log("关闭虚拟机失败")
return console.pause()
}
console.log("所有操作已完成,虚拟机:",vm.name)
console.pause()
3、恢复虚拟机到指定快照: import console;
import com;
var vbox = com.CreateObject("VirtualBox.VirtualBox")
var vSession = com.CreateObject("VirtualBox.Session")
var vm = vbox.findMachine ("xp-big5")
//锁定虚拟机会话
vm.LockMachine (vSession, vm.LockType_Shared);
//如果虚拟机已经在运行
if(vSession.console){
//先关机
vProcess = vSession.getConsole().powerDown();
//等待操作完成
vProcess.waitForCompletion(-1);
//检测操作是否成功
if (vProcess.getResultCode() != 0) { // we failed to stop the VM
console.log("关闭虚拟机失败")
return console.pause()
}
}
//如果参数为null,则获取根快照
var snapShot = vm.FindSnapshot("快照1");
//这里只能用vSession.machine,不能直接用vm.RestoreSnapshot
var snapShotProgress = vSession.machine.RestoreSnapshot(snapShot);
//等待操作完成
snapShotProgress.waitForCompletion(-1);
//检测操作是否成功
if (snapShotProgress.getResultCode() != 0) { // we failed to stop the VM
console.log("恢复快照失败")
return console.pause()
}
//释放会话
vSession.UnLockMachine()
console.log("所有操作已完成,虚拟机:",vm.name)
console.pause(true);
|
|