Skip to content

Instantly share code, notes, and snippets.

@Mattti0
Last active February 14, 2024 08:01
Show Gist options
  • Save Mattti0/b8bc1bc52be26f5b32d43c995212ec08 to your computer and use it in GitHub Desktop.
Save Mattti0/b8bc1bc52be26f5b32d43c995212ec08 to your computer and use it in GitHub Desktop.
Powershell script to start virtual box guest and connect to it over SSH
$vmName = "VBGuest"
$guestAddress = "use vb 'host only' address here"
$sshUsername = "root_or_something_else"
$sshPort = "fill_port_here"
# Check if the VM is running by pinging the guest address
$pingResult = Test-Connection -ComputerName $guestAddress -Count 2 -Quiet
if ($pingResult) {
Write-Host "VM is running. Connecting via SSH..."
} else {
Write-Host "VM is not running. Starting VirtualBox in headless mode..."
# Start the VM in headless mode
Start-Process -FilePath "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" -ArgumentList "startvm", $vmName, "--type", "headless" -Wait
Start-Sleep -Seconds 10
while ($true) {
$pingResult = Test-Connection -ComputerName $guestAddress -Count 1 -Quiet
if ($pingResult) {
Write-Host "VM started. Connecting via SSH..."
break
}
else {
Write-Host "Guest not operating yet, please wait..."
Start-Sleep -Seconds 5
}
}
}
ssh ${sshUsername}@${guestAddress} -p ${sshPort}
# When guest is up and running, this will ask password for ssh account
@Mattti0
Copy link
Author

Mattti0 commented Jan 22, 2024

Use this as Windows Terminal profile by adding new profile and put following to Command line :
pwsh.exe -File "C:\<path>\<to>\<script>\VB-guest-start-and-ssh.ps1"

(As you probably noticed this is using "new powershell")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment