Skip to content

Instantly share code, notes, and snippets.

@shadministrator
Last active January 24, 2022 00:04
Show Gist options
  • Save shadministrator/9f1b50247c04ff23d5448cf4bbc8d176 to your computer and use it in GitHub Desktop.
Save shadministrator/9f1b50247c04ff23d5448cf4bbc8d176 to your computer and use it in GitHub Desktop.
This script connects to your VMWare vSphere/ESXI environment, copies a bash configuration script to the user directory on the guest, executes that script, and then imports the ssh public key from your management system (for remote SSH).
# PREREQUISITE: Install the VMWare PowerCLI module on your system
$presentDir = Get-Location | Select-Object -ExpandProperty Path
$vmwEnviHostname = "ESXI01"
$vmName = Read-Host "Enter the name of the Virtual Machine to connect to"
$sshKeyPath = Read-Host "Enter path to the private key (including the key file name) to use for ssh connection to this VM"
# Put contents of public ssh key into variable
$sshPubKeyContents = Get-Content "$sshKeyPath.pub"
$vmwCred = Get-Credential -Message "Enter your vSphere or ESXI host credentials"
$guestOSCred = Get-Credential -Message "Enter the username and password of the inital guest OS user"
$guestOSUser = $guestOSCred.Username
# Associated bash script in same directory as this script
$pathToBashSSHInitScript = "$presentDir\Ubuntu-SSH-Init.sh"
# Connect to vSphere/ESXI environment
try {
Connect-VIServer -Server $vmwEnviHostname -User $vmwCred.Username -Password (ConvertFrom-SecureString -SecureString $vmwCred.Password -AsPlainText)
}
catch {
throw $_
Write-Host "Invalid credentials or VMWare host cannot be reached"
break
}
# Copy bash file to VM user directory
try {
Get-Item "$pathToBashSSHInitScript" | Copy-VMGuestFile -Destination "/home/$guestOSUser" -VM $vmName -LocalToGuest -GuestUser $guestOSCred.Username -GuestPassword (ConvertFrom-SecureString -SecureString $guestOSCred.Password -AsPlainText)
}
catch {
throw $_
Write-Host "Unable to copy bash ssh init file to guest"
break
}
# Run SSH-init bash script. Does a package update, installs openssh, opens port 22 in firewall, and creates ssh dir.
try {
Invoke-VMScript -VM $vmName -ScriptText "echo $(ConvertFrom-SecureString -SecureString $guestOSCred.Password -AsPlainText) | sudo -S bash ~/Ubuntu-SSH-Init.sh" -GuestUser $guestOSCred.Username -GuestPassword (ConvertFrom-SecureString -SecureString $guestOSCred.Password -AsPlainText) -ScriptType "Bash"
}
catch {
throw $_
Write-Host "Invalid VM Name or guest OS credentials, or error running ssh-init bash script on guest"
break
}
# Import public ssh key contents on local machine to authorized_keys file on guest. Clear command history as we passed our sudo password in the command.
try {
Invoke-VMScript -VM $vmName -ScriptText "echo $sshPubKeyContents >> ~/.ssh/authorized_keys; history -c" -GuestUser $guestOSCred.Username -GuestPassword $guestOSCred.Password
}
catch {
throw $_
Write-Host "Could not find ssh public key (is it named the same as the associated private key?)"
}
Disconnect-VIServer -Confirm:$false
Read-Host "SSH Configuration for $vmName complete. Press ENTER to exit dialogue..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment