Última atividade 1757040328

Windows开机启动VirtualBox虚拟机脚本,将脚本放到C:\Users\YourName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

start-virtualbox-vms.ps1 Bruto
1# 描述:用于批量启动VirtualBox虚拟机的PowerShell脚本
2
3Write-Host "========================================" -ForegroundColor Cyan
4Write-Host " VirtualBox 虚拟机批量启动工具" -ForegroundColor Cyan
5Write-Host "========================================" -ForegroundColor Cyan
6Write-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. 获取当前所有虚拟机列表(可选,用于确认)
17Write-Host "正在获取虚拟机列表..." -ForegroundColor Yellow
18$AllVMs = & $VBoxManagePath list vms
19Write-Host "当前已注册的虚拟机:" -ForegroundColor Green
20$AllVMs | ForEach-Object { Write-Host " $_" }
21
22Write-Host ""
23Write-Host "开始启动虚拟机 [$StartType 模式]..." -ForegroundColor Yellow
24
25# 3. 循环启动虚拟机
26foreach ($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
38Write-Host "批量启动操作已完成!" -ForegroundColor Cyan
39Write-Host "使用 'VBoxManage list runningvms' 命令查看正在运行的虚拟机。" -ForegroundColor Yellow
40
41# 可选:自动列出当前正在运行的虚拟机
42Write-Host ""
43Write-Host "当前正在运行的虚拟机:" -ForegroundColor Green
44& $VBoxManagePath list runningvms
45
46pause