Created
August 9, 2024 16:20
-
-
Save Mohammad-Reihani/f8d838a56b6677363f499005512f9ebb to your computer and use it in GitHub Desktop.
Auto start VMware VMs with powershell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # VMware Startup PowerShell Script | |
| # This script starts multiple VMware virtual machines in the background | |
| # and handles lock file removal if necessary. | |
| # ---- Configuration ---- | |
| $vmrunPath = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" | |
| $logFile = Join-Path $PSScriptRoot "vm_startup_log.txt" | |
| $delay = 50 # Delay between starting VMs (in seconds) | |
| # ---- VM Paths ---- | |
| # Add or remove VM paths as needed | |
| $vmPaths = @( | |
| "D:\Vritual Machines\Alpine\Other Linux 6.x kernel 64-bit.vmx", | |
| "D:\Vritual Machines\Ubuntu-22.04.3-64-bit\Ubuntu-22.04.3-64-bit.vmx" | |
| ) | |
| # ---- Functions ---- | |
| function Write-Log { | |
| param([string]$message) | |
| $logMessage = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message" | |
| Write-Host $logMessage | |
| Add-Content -Path $logFile -Value $logMessage | |
| } | |
| function Check-VMRunning { | |
| param([string]$vmxPath) | |
| try { | |
| $output = & $vmrunPath list | |
| $vmxFileName = [System.IO.Path]::GetFileName($vmxPath) | |
| if ($output -contains $vmxPath) { | |
| Write-Log "VM is already running: $vmxFileName" | |
| return $true | |
| } else { | |
| return $false | |
| } | |
| } catch { | |
| Write-Log "ERROR: Failed to check if VM is running: $vmxPath. Error: $($_.Exception.Message)" | |
| return $false | |
| } | |
| } | |
| function Remove-LockFiles { | |
| param([string]$vmxPath) | |
| $vmDir = Split-Path $vmxPath -Parent | |
| Write-Log "Checking for lock files in: $vmDir" | |
| $lockFiles = Get-ChildItem -Path $vmDir -Filter "*.lck" -Force | |
| if ($lockFiles) { | |
| Write-Log "Lock files found. Attempting to remove..." | |
| try { | |
| $lockFiles | Remove-Item -Force -Recurse -Confirm:$false | |
| Write-Log "Lock files removed successfully from $vmDir" | |
| } catch { | |
| Write-Log "WARNING: Failed to remove some lock files in $vmDir. Error: $($_.Exception.Message)" | |
| Write-Log "Waiting for 20 seconds before continuing..." | |
| Start-Sleep -Seconds 20 | |
| Write-Log "Continuing script execution after 20 seconds." | |
| } | |
| } else { | |
| Write-Log "No lock files found in $vmDir" | |
| } | |
| } | |
| function Start-VM { | |
| param([string]$vmxPath) | |
| Write-Log "Starting VM: $vmxPath" | |
| try { | |
| & $vmrunPath start $vmxPath nogui | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Log "Successfully started VM: $vmxPath" | |
| } else { | |
| Write-Log "ERROR: Failed to start VM: $vmxPath. Exit code: $LASTEXITCODE" | |
| } | |
| } catch { | |
| Write-Log "ERROR: Exception when starting VM: $vmxPath. Error: $($_.Exception.Message)" | |
| } | |
| } | |
| # ---- Main Script ---- | |
| Write-Log "---- VMware Startup Script Started ----" | |
| # Check if vmrun.exe exists | |
| if (-not (Test-Path $vmrunPath)) { | |
| Write-Log "ERROR: vmrun.exe not found at $vmrunPath. Please check the path." | |
| exit | |
| } | |
| # Process each VM | |
| foreach ($vm in $vmPaths) { | |
| if (Test-Path $vm) { | |
| $isRunning = Check-VMRunning $vm | |
| if (-not $isRunning) { | |
| Remove-LockFiles $vm | |
| Start-VM $vm | |
| Start-Sleep -Seconds $delay | |
| } else { | |
| Write-Log "Skipping VM as it is already running: $vm" | |
| } | |
| } else { | |
| Write-Log "ERROR: VMX file not found: $vm" | |
| } | |
| } | |
| Write-Log "---- VMware Startup Script Completed ----" | |
| Write-Log "Total VMs processed: $($vmPaths.Count)" | |
| Write-Log "Check the log file for details: $logFile" | |
| Read-Host "Press Enter to exit" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
schedule this task to run on start up
comment any issues if found
NOTE : Maybe you do not need to delete .lck files of the VM!