Auto-Start Virtual Machine Part 1
Infrastructure Tutorials
Download Auto Start VM Part 1 tutorial.
In this tutorial I will be creating a PowerShell script to automatically start an existing VirtualBox virtual machine. Refer to the prerequisites listed below to complete this tutorial.
Prerequisites
- VirtualBox VM
For instructions on how to install VirtualBox and extension pack, see my VirtualBox Install tutorial here.
If you do not already have a virtual machine, my other tutorial, CentOS 7 Server Install, is here.
Steps to complete tutorial:
Create Script
I have created an empty script file named auto_start_vm.ps1.
First, we will determine the path to the VirtualBox executable using the Get-Command cmdlet.
Get-Command -Name VirtualBox
You will notice that the field value that we need (Source) is truncated. We will use the following to improve the readability of the tabular results.
Get-Command -Name VirtualBox | Format-Table -Autosize
Using the Autosize parameter, when running Format-Table, column widths are calculated based on size of actual data.
The following will return an object with one property, Source.
Get-Command VirtualBox | Select-Object Source
Before we assign this value to a variable, we will need to extract the Source property value from the object being passed down the pipeline. The following will provide us with the Source property value and remove any empty lines from the command results:
Get-Command VirtualBox | Select-Object -ExpandProperty Source
Now we can assign the command result to a variable for later use.
$vbox = Get-Command VirtualBox | Select-Object -ExpandProperty Source
Next, we will locate the virtual machine we wish to auto-start using the following:
(NOTE: use the VM name you wish to auto-start)
VBoxManage list vms | Select-String -Pattern "centos7-VM"
You will notice that I have many VMs whose names begin with centos7-VM. PowerShell's Select-String cmdlet returned all of them, as demonstrated above.
I will use single quotes outside of the double quotes ('"centos7-VM"') to ensure that the double-quotes are included in the search.
VBoxManage list vms | Select-String -Pattern '"centos7-VM"'
Before we assign this value to a variable, we will need to remove any empty lines from the command results. To do this we will add the following to the command:
VBoxManage list vms | Select-String -Pattern '"centos7-VM"' |% { $_.Matches } | % { $_.Value }
For each object returned by Select-String, we are finding the value of each match. Although this tutorial is not a deep-dive into PowerShell cmdlets, the default output of Select-String is a MatchInfo object, which includes detailed information (properties) about the matches. The MatchInfo object has a property called Matches, which contains a list of regular expression matches. For each of the matches in the list returned, we are using the Value property to get the actual value.
Now we can assign the command result to a variable for later use.
$vm = VBoxManage list vms | Select-String -Pattern '"centos7-VM"' | % { $_.Matches } | % { $_.Value }
We now have both the path to the VirtualBox executable, as well as, the VM name assigned to variables.
Although we do not need to first start VirtualBox before we can start a virtual machine, I like having the management interface at my disposal. You can include the following in your script, or not, the choice is yours.
# start VirtualBox for access to management interface
Start-Process $vbox
if (Get-Process VirtualBox -ErrorAction SilentlyContinue) {
Write-Host "VirtualBox has successfully started."
} else {
Write-Host "Something went wrong during VirtualBox startup."
}
You will notice that I have included -ErrorAction SilentlyContinue in our test. This is what’s known as a “Common Parameter” of the Get-Process cmdlet. If VirtualBox did not start, instead of generating error messages to the PowerShell console, the command will continue execution and jump to the else clause of the if/else statement.
Now we are ready to start the virtual machine, whose name we assigned to the variable $vm.
# NOTE: works without VirtualBox being started first
VBoxManage startvm $vm
if ( "$?" -eq "True") {
Write-Host "$vm is booting up."
} else {
Write-Host "Problem starting $vm"
}
This time we are using the PowerShell automatic variable $?. This variable contains the status of the last command executed:
True if it succeeded and False if it failed.
We are ready to review the contents of the entire script.
Review Script
Write-Host "Automate VirtualBox VM startup."
# retrieve path to VirtualBox executable
$vbox = Get-Command VirtualBox | Select Source -ExpandProperty Source
# locate VM based on it's name
# remove any empty lines from result of command
$vm = VBoxManage list vms | Select-String -Pattern '"centos7-VM"' | % { $_.Matches } | % { $_.Value }
# start VirtualBox for access to management interface
Start-Process $vbox
# ensure VirtualBox has started
if (Get-Process VirtualBox -ErrorAction SilentlyContinue) {
Write-Host "VirtualBox has successfully started."
} else {
Write-Host "Something went wrong during VirtualBox startup."
}
# NOTE: works without VirtualBox being started first
VBoxManage startvm $vm
# verify success of last command executed
if ( "$?" -eq "True") {
Write-Host "$vm is booting up."
} else {
Write-Host "Problem starting $vm"
}
Write-Host "Have a productive session on $vm."
sleep 10
Execute Script
To execute the script, open a PowerShell console, if need be, change directory to the location of your script and execute the following: .\auto_start_vm.ps1
VirtualBox successfully started and so did the virtual machine.
Hopefully, you’ve enjoyed completing this tutorial and found it helpful.
Now that you can auto-start your VM, you might want to see my other tutorial, Auto-Stop Virtual Machine Part 1,
accessible here.
If you would like to see my other tutorials, they can be accessed here.
Finally, if need be, you can download this tutorial Auto Start VM Part 1.