Last active
May 7, 2016 20:20
-
-
Save stavarotti/ab781d957bf758c6c3dec66c1f59a752 to your computer and use it in GitHub Desktop.
This file contains 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
# Returns true if the vm is running, else false | |
Function isVmRunning([string]$name) { | |
# Whether the named vm is running | |
[bool]$isRunning = $false | |
# Use `vmrun` to get the list of currently running vms | |
[string[]]$runningVms = Invoke-Expression "vmrun list" | |
# Get the number of running VMs | |
[int16]$numOfRunningVms = $runningVms.Get(0).split(":").Get(1).Trim() | |
if ($numOfRunningVms -gt 0) { | |
for ($i = 1; $i -lt $runningVms.length; $i += 1) { | |
if ($runningVms[$i].Contains($name)) { | |
$isRunning = $true | |
break | |
} | |
} | |
} else { | |
# The `vmrun list` command appears not to show vms started before a user logs in. | |
# Using Get-Process to doubly check if the process isn't already running. | |
try { | |
$process = Get-Process -Name "vmware-vmx" -ErrorAction SilentlyContinue | |
} catch {} | |
if ($process) { | |
$isRunning = $true | |
} | |
} | |
Return $isRunning | |
} | |
Function LogEvent([string]$source, [int16]$eventId, [string]$entryType, [string]$message) { | |
Write-EventLog -LogName Application -Source $source -EventId $eventId -EntryType $entryType -Message $message | |
[string]$logBoundMessage = (Get-Date -Format g) + " " + $message | |
switch -wildcard ($entryType.ToLower()) { | |
"error" { | |
$logBoundMessage >> "C:\vm\vmware\windows-7-error-log.txt" | |
} | |
"info" { | |
$logBoundMessage >> "C:\vm\vmware\windows-7-info-log.txt" | |
} | |
default { | |
$logBoundMessage >> "C:\vm\vmware\windows-7-info-log.txt" | |
} | |
} | |
} | |
Function main() { | |
# Messages | |
[string]$errorLogMessage = "The Windows 7 virtual machine failed to start." | |
[string]$vmRunningMessage = "The Windows 7 virtual machine is already running." | |
[string]$successLogMessage = "The Windows 7 virtual machine started successfully." | |
[string]$eventLogSource = "VMWare Custom Scripts" | |
[string]$vmFileLocation = "H:\vm\vmware\Windows 7\Windows 7.vmx" | |
[string]$vmName = "Windows 7.vmx" | |
# Create the event log source. | |
try { | |
New-EventLog -LogName Application -Source $eventLogSource -ErrorAction SilentlyContinue | |
} catch {} | |
if (isVmRunning $vmName) { | |
LogEvent $eventLogSource 2004 Information $vmRunningMessage | |
} else { | |
# Start the vm | |
vmrun -T player -gu REPLACE_ME_WITH_GUEST_USERNAME -gp REPLACE_ME_WITH_GUEST_PASSWORD start $vmFileLocation nogui | |
# Exit with an error if the VM process is not detected | |
if (isVmRunning $vmName -eq $False) { | |
LogEvent $eventLogSource 2000 Information $successLogMessage | |
exit 0 | |
} else { | |
LogEvent $eventLogSource 5000 Error $errorLogMessage | |
# The caller of this script knows to retry on an error exit code. We don't want to continuously retry | |
# starting the vm as it may land us in an infinite loop. | |
exit 1 | |
} | |
} | |
} | |
# Kick off the startup process. | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment