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 you have administrative privileges on your system. The script requires elevated permissions to manage Hyper-V and execute certain commands.
Make sure Hyper-V is enabled on your system. You can enable Hyper-V using the following steps:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-AllThe script requires PowerShell version 7 or greater. Follow these steps to install or update PowerShell:
Ensure that the SSH client is installed on your system. You can install the OpenSSH client by following these steps:
Add-WindowsCapability -Online -Name OpenSSH.Client*ssh -Vbrew install opensshOpen Terminal.
Run the following command:
sudo apt-get install openssh-clientor for RPM-based distributions:
sudo yum install openssh-clientsEnsure 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.
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.
VMManagement.ps1 script is saved..VMManagement.ps1To 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.ps1Get-Run-AsAdministratorEnsures 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:PathToScript.ps1"Enable-HyperVChecks if Hyper-V is enabled on the system and enables it if not.
Example:
Enable-HyperVTest-PwshVersionChecks if the current PowerShell version is 7 or greater.
Example:
Test-PwshVersionGet-VMListRetrieves a list of VM names.
Example:
$vms = Get-VMListGet-VMNamePrompts the user to select a VM from a list.
Example:
$vmName = Get-VMNameGet-UserNamePrompts the user to enter a username for SSH.
Example:
$username = Get-UserNameStart-VMInstanceStarts a specified VM.
Parameters:
vmName: The name of the VM to start.Example:
$vm = Start-VMInstance -vmName "MyVM"Add-DashAdds dashes to a string every two characters.
Parameters:
str: The string to which dashes will be added.Example:
$dashedString = Add-Dash -str "A1B2C3D4"Get-VMIPRetrieves 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 $vmStop-VMInstanceStops a specified VM.
Parameters:
vmName: The name of the VM to stop.Example:
Stop-VMInstance -vmName "MyVM"Invoke-SSHConnectionInitiates 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"Get-VMHeartbeatChecks 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