90 lines
3.3 KiB
PowerShell
90 lines
3.3 KiB
PowerShell
param(
|
|
[string[]]$VmNameAllowList = @("110", "188", "120", "121", "112"),
|
|
[string[]]$SearchRoots = @("C:\VMs", "D:\VMs", "$env:USERPROFILE\Documents\Virtual Machines"),
|
|
[string]$TaskName = "AWOOOI VMware VM AutoStart",
|
|
[switch]$WhatIfOnly
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Find-Vmrun {
|
|
$candidates = @(
|
|
"${env:ProgramFiles(x86)}\VMware\VMware Workstation\vmrun.exe",
|
|
"${env:ProgramFiles}\VMware\VMware Workstation\vmrun.exe"
|
|
)
|
|
foreach ($candidate in $candidates) {
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
throw "vmrun.exe not found. VMware Workstation must be installed before VM autostart can be configured."
|
|
}
|
|
|
|
function Read-VmxDisplayName {
|
|
param([string]$Path)
|
|
$line = Select-String -Path $Path -Pattern '^\s*displayName\s*=' -ErrorAction SilentlyContinue | Select-Object -First 1
|
|
if (-not $line) { return [IO.Path]::GetFileNameWithoutExtension($Path) }
|
|
return ($line.Line -replace '^\s*displayName\s*=\s*', '' -replace '"', '').Trim()
|
|
}
|
|
|
|
$vmrun = Find-Vmrun
|
|
$vmxFiles = @()
|
|
foreach ($root in $SearchRoots) {
|
|
if (Test-Path $root) {
|
|
$vmxFiles += Get-ChildItem -Path $root -Recurse -Filter "*.vmx" -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
$selected = @()
|
|
foreach ($vmx in $vmxFiles) {
|
|
$displayName = Read-VmxDisplayName -Path $vmx.FullName
|
|
foreach ($allowed in $VmNameAllowList) {
|
|
if ($displayName -eq $allowed -or $displayName -like "*$allowed*" -or $vmx.FullName -like "*$allowed*") {
|
|
$selected += [PSCustomObject]@{ Name = $displayName; Path = $vmx.FullName }
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
$selected = $selected | Sort-Object Name, Path -Unique
|
|
if ($selected.Count -lt $VmNameAllowList.Count) {
|
|
$found = ($selected | ForEach-Object { $_.Name }) -join ", "
|
|
throw "Not all required VMX files were found. found=[$found] required=[$($VmNameAllowList -join ', ')]"
|
|
}
|
|
|
|
Set-Service -Name "VMAuthdService" -StartupType Automatic
|
|
Set-Service -Name "VMUSBArbService" -StartupType Automatic -ErrorAction SilentlyContinue
|
|
Start-Service -Name "VMAuthdService"
|
|
|
|
$runner = "C:\ProgramData\AWOOOI\Start-AwoooIVms.ps1"
|
|
New-Item -ItemType Directory -Force -Path (Split-Path $runner) | Out-Null
|
|
|
|
$vmLines = $selected | ForEach-Object { '"' + ($_.Path -replace '"', '\"') + '"' }
|
|
$script = @"
|
|
`$ErrorActionPreference = "Continue"
|
|
`$vmrun = "$vmrun"
|
|
`$vms = @(
|
|
$($vmLines -join ",`n ")
|
|
)
|
|
Start-Sleep -Seconds 30
|
|
foreach (`$vmx in `$vms) {
|
|
& `$vmrun -T ws start `$vmx nogui | Out-File -Append C:\ProgramData\AWOOOI\vmware-autostart.log
|
|
Start-Sleep -Seconds 15
|
|
}
|
|
"@
|
|
|
|
if ($WhatIfOnly) {
|
|
Write-Output "WHATIF task=$TaskName runner=$runner"
|
|
$selected | Format-Table -AutoSize
|
|
exit 0
|
|
}
|
|
|
|
Set-Content -Path $runner -Value $script -Encoding UTF8
|
|
|
|
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$runner`""
|
|
$trigger = New-ScheduledTaskTrigger -AtStartup
|
|
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
|
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DisallowStartIfOnBatteries:$false -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
|
|
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null
|
|
|
|
Write-Output "Configured VMware autostart task: $TaskName"
|
|
$selected | Format-Table -AutoSize
|