Last active
February 14, 2024 08:01
-
-
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
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
$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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")