hyper-v-direct-ssh
is a PowerShell module designed to manage Hyper-V virtual machines (VMs) and establish direct SSH connections to them. This module provides functions for starting and stopping VMs, retrieving VM information, and ensuring the script runs with administrator privileges.
- Ensure script runs with administrator privileges.
- Check and enable Hyper-V on the system.
- Verify PowerShell version is 7 or greater.
- Retrieve and manage VM names.
- Start and stop VMs.
- Retrieve VM IP addresses.
- Initiate SSH connections to VMs.
- Check VM heartbeat status.
Verify Administrative Privileges
Ensure you have administrative privileges on your system. The script requires elevated permissions to manage Hyper-V and execute certain commands.
Enable Hyper-V
Make sure Hyper-V is enabled on your system. You can enable Hyper-V using the following steps:
- Open PowerShell as an administrator.
- Run the following command to enable Hyper-V:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
- Restart your system to apply the changes.
Install PowerShell 7 or Greater
The script requires PowerShell version 7 or greater. Follow these steps to install or update PowerShell:
- Go to the PowerShell GitHub releases page.
- Download the latest release for your operating system.
- Follow the installation instructions provided on the release page.
Install SSH Client
Ensure that the SSH client is installed on your system. You can install the OpenSSH client by following these steps:
- Open PowerShell as an administrator.
- Run the following command:
Add-WindowsCapability -Online -Name OpenSSH.Client*
- Open Terminal.
- Run the following command to check if SSH is already installed:
ssh -V
- If SSH is not installed, use Homebrew to install it:
brew install openssh
-
Open Terminal.
-
Run the following command:
sudo apt-get install openssh-client
or for RPM-based distributions:
sudo yum install openssh-clients
Configure Network for VMs
Ensure that your VMs are configured to obtain an IP address and that they are accessible via SSH. Verify the network settings and connectivity before running the script.
Download the Script
Save the script as VMManagement.ps1
on your local machine. Make sure the file path is accessible and note down the path for running the script.
Run the Script
- Open PowerShell as an administrator.
- Navigate to the directory where the
VMManagement.ps1
script is saved. - Run the script using the following command:
.\VMManagement.ps1
To use the module, save the script as VMManagement.ps1
and run it using PowerShell. Make sure you have the necessary administrative privileges and that Hyper-V is enabled on your system.
.\VMManagement.ps1
Ensures the script runs with administrator privileges.
Parameters:
ScriptPath
: The path to the script that needs to be run as administrator.
Example:
Get-Run-AsAdministrator -ScriptPath "C:\Path\To\Script.ps1"
Checks if Hyper-V is enabled on the system and enables it if not.
Example:
Enable-HyperV
Checks if the current PowerShell version is 7 or greater.
Example:
Test-PwshVersion
Retrieves a list of VM names.
Example:
$vms = Get-VMList
Prompts the user to select a VM from a list.
Example:
$vmName = Get-VMName
Prompts the user to enter a username for SSH.
Example:
$username = Get-UserName
Starts a specified VM.
Parameters:
vmName
: The name of the VM to start.
Example:
$vm = Start-VMInstance -vmName "MyVM"
Adds dashes to a string every two characters.
Parameters:
str
: The string to which dashes will be added.
Example:
$dashedString = Add-Dash -str "A1B2C3D4"
Retrieves the IP address of a specified VM.
Parameters:
vmObject
: The VM object for which the IP address is to be retrieved.
Example:
$ipAddress = Get-VMIP -vmObject $vm
Stops a specified VM.
Parameters:
vmName
: The name of the VM to stop.
Example:
Stop-VMInstance -vmName "MyVM"
Initiates an SSH connection to a specified VM.
Parameters:
user
: The username for the SSH connection.vmIP
: The IP address of the VM.
Example:
Invoke-SSHConnection -user "username" -vmIP "192.168.1.100"
Checks the heartbeat status of a specified VM.
Parameters:
vmObject
: The VM object for which the heartbeat status is to be checked.
Example:
$heartbeat = Get-VMHeartbeat -vmObject $vm
- Ensure the script runs with administrator privileges.
- Enable Hyper-V if it is not already enabled.
- Check that the PowerShell version is 7 or greater.
- Retrieve and display a list of VMs for the user to select from.
- Start the selected VM.
- Check the VM's heartbeat status.
- Retrieve the VM's IP address.
- Initiate an SSH connection to the VM.
- Stop the VM after the SSH session ends.