start-virtualbox-vms.ps1
· 1.9 KiB · PowerShell
Orginalformat
# 描述:用于批量启动VirtualBox虚拟机的PowerShell脚本
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " VirtualBox 虚拟机批量启动工具" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 1. 配置区域:请根据您的实际情况修改以下变量
# 将虚拟机名称放在下面的数组中,可以是一个或多个
$VM_Names = @("CentOS", "fnOS")
# 设置启动模式: 'gui' 或 'headless'
$StartType = 'gui'
# 如果VBoxManage不在系统PATH中,请指定完整路径(通常不需要)
$VBoxManagePath = "C:\Users\NAS\Programs\VirtualBox\VBoxManage.exe" # 默认使用系统路径中的命令
# 2. 获取当前所有虚拟机列表(可选,用于确认)
Write-Host "正在获取虚拟机列表..." -ForegroundColor Yellow
$AllVMs = & $VBoxManagePath list vms
Write-Host "当前已注册的虚拟机:" -ForegroundColor Green
$AllVMs | ForEach-Object { Write-Host " $_" }
Write-Host ""
Write-Host "开始启动虚拟机 [$StartType 模式]..." -ForegroundColor Yellow
# 3. 循环启动虚拟机
foreach ($VM in $VM_Names) {
Write-Host "正在启动虚拟机: $VM" -ForegroundColor White
try {
# 执行启动命令
& $VBoxManagePath startvm $VM --type $StartType
Write-Host "[成功] `"$VM`" 已启动." -ForegroundColor Green
} catch {
Write-Host "[错误] 启动 `"$VM`" 时发生问题: $_" -ForegroundColor Red
}
Write-Host "----------------------------------------" -ForegroundColor DarkGray
}
Write-Host "批量启动操作已完成!" -ForegroundColor Cyan
Write-Host "使用 'VBoxManage list runningvms' 命令查看正在运行的虚拟机。" -ForegroundColor Yellow
# 可选:自动列出当前正在运行的虚拟机
Write-Host ""
Write-Host "当前正在运行的虚拟机:" -ForegroundColor Green
& $VBoxManagePath list runningvms
pause
| 1 | # 描述:用于批量启动VirtualBox虚拟机的PowerShell脚本 |
| 2 | |
| 3 | Write-Host "========================================" -ForegroundColor Cyan |
| 4 | Write-Host " VirtualBox 虚拟机批量启动工具" -ForegroundColor Cyan |
| 5 | Write-Host "========================================" -ForegroundColor Cyan |
| 6 | Write-Host "" |
| 7 | |
| 8 | # 1. 配置区域:请根据您的实际情况修改以下变量 |
| 9 | # 将虚拟机名称放在下面的数组中,可以是一个或多个 |
| 10 | $VM_Names = @("CentOS", "fnOS") |
| 11 | # 设置启动模式: 'gui' 或 'headless' |
| 12 | $StartType = 'gui' |
| 13 | # 如果VBoxManage不在系统PATH中,请指定完整路径(通常不需要) |
| 14 | $VBoxManagePath = "C:\Users\NAS\Programs\VirtualBox\VBoxManage.exe" # 默认使用系统路径中的命令 |
| 15 | |
| 16 | # 2. 获取当前所有虚拟机列表(可选,用于确认) |
| 17 | Write-Host "正在获取虚拟机列表..." -ForegroundColor Yellow |
| 18 | $AllVMs = & $VBoxManagePath list vms |
| 19 | Write-Host "当前已注册的虚拟机:" -ForegroundColor Green |
| 20 | $AllVMs | ForEach-Object { Write-Host " $_" } |
| 21 | |
| 22 | Write-Host "" |
| 23 | Write-Host "开始启动虚拟机 [$StartType 模式]..." -ForegroundColor Yellow |
| 24 | |
| 25 | # 3. 循环启动虚拟机 |
| 26 | foreach ($VM in $VM_Names) { |
| 27 | Write-Host "正在启动虚拟机: $VM" -ForegroundColor White |
| 28 | try { |
| 29 | # 执行启动命令 |
| 30 | & $VBoxManagePath startvm $VM --type $StartType |
| 31 | Write-Host "[成功] `"$VM`" 已启动." -ForegroundColor Green |
| 32 | } catch { |
| 33 | Write-Host "[错误] 启动 `"$VM`" 时发生问题: $_" -ForegroundColor Red |
| 34 | } |
| 35 | Write-Host "----------------------------------------" -ForegroundColor DarkGray |
| 36 | } |
| 37 | |
| 38 | Write-Host "批量启动操作已完成!" -ForegroundColor Cyan |
| 39 | Write-Host "使用 'VBoxManage list runningvms' 命令查看正在运行的虚拟机。" -ForegroundColor Yellow |
| 40 | |
| 41 | # 可选:自动列出当前正在运行的虚拟机 |
| 42 | Write-Host "" |
| 43 | Write-Host "当前正在运行的虚拟机:" -ForegroundColor Green |
| 44 | & $VBoxManagePath list runningvms |
| 45 | |
| 46 | pause |