Introduction
Configuring Hyper-V on Windows Server using PowerShell is a crucial task for creating and managing virtual machines. Hyper-V is a powerful virtualization platform that enables users to run multiple operating systems and applications on a single server. This guide will provide you with a step-by-step approach to configure Hyper-V on Windows Server, allowing you to harness the benefits of virtualization and optimize your server’s resources.
Step by Step Configuration using RSAT and PowerShell.
I’m Using A Windows Server Core Machine and Using Windows 10 PC with RSAT tools
RSAT stands for “Remote Server Administration Tools.” In simple terms, RSAT is a collection of tools provided by Microsoft that allows system administrators to manage and administer Windows servers remotely from their own computers. These tools provide a graphical user interface (GUI) or command-line interface (CLI) for performing various administrative tasks, such as managing Active Directory, Group Policy, Hyper-V, DNS, DHCP, and more. With RSAT installed on their local computers, administrators can efficiently manage and configure Windows servers without physically being present at the server’s location.
Configure Hyper-V
- get-netadapters
- This command retrieves information about network adapters on the computer. It will display a list of network adapters along with their details like InterfaceIndex, Name, MAC Address, and more.
- New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.3.200 -PrefixLength 24 -DefaultGateway 192.168.3.2
- This command creates a new IP address configuration for the network adapter with InterfaceIndex 12. It assigns the IP address 192.168.3.200 with a subnet mask of 255.255.255.0 (PrefixLength 24) and sets the default gateway to 192.168.3.2.
- Set-DNSClientServerAddress -interfacealias ethernet0 -ServerAddress 192.168.3.10
- This command sets the DNS server address for the network adapter with the alias “ethernet0.” It configures the DNS server to use the address 192.168.3.10 for name resolution.
- New-NetFirewallRule -displayname “Allow All Traffic” -direction outbound -action allow
- This command creates a new outbound firewall rule with the display name “Allow All Traffic.” It allows all outbound network traffic.
- New-NetFirewallRule -displayname “Allow All Traffic” -direction inbound -action allow
- This command creates a new inbound firewall rule with the display name “Allow All Traffic.” It allows all inbound network traffic.
- hostname
- This command displays the hostname of the computer, which is the unique name assigned to it on the network.
- Add-Computer -newname hyperv1 -domainname spacex.co -restart
- This command adds the computer to a domain named “spacex.co” and renames the computer to “hyperv1.” After completing this operation, the computer will restart to apply the changes and join the specified domain with the new name.
In admin PC
- get-windowsfeature -computername hyperv1
- This command retrieves information about the installed Windows features on the computer named “hyperv1.”
- install-windowsfeature -computername hyperv1 -name hyper-v
- This command installs the Hyper-V feature on the computer named “hyperv1.” Hyper-V is a virtualization technology that allows you to create and manage virtual machines on your computer.
- restart-computer -computername hyperv1
- This command restarts the computer named “hyperv1” to apply any changes or updates that have been made.
- install-windowsfeature -computername hyperv1 -name rsat-hyper-v-tools
- This command installs the Remote Server Administration Tools (RSAT) for Hyper-V on the computer named “hyperv1.” RSAT provides management tools for administering Hyper-V and other server roles remotely.
- install-windowsfeature -computername hyperv1 -name hyper-v-powershell
- This command installs the Hyper-V PowerShell module on the computer named “hyperv1.” The Hyper-V PowerShell module allows you to manage and automate Hyper-V using PowerShell commands.
- get-windowsoptionalfeature -online
- This command retrieves information about the optional features available on the Windows operating system that is currently running.
- get-windowsoptionalfeature -online | where featurename -like “*hyper*”
- This command filters the list of optional features to display only those that have “hyper” in their feature name. It helps you find optional features related to Hyper-V.
In admin PC — Hyper-V VMS
To create a new virtual machine named “VM1” on the computer “hyperv1” with Hyper-V, you use the command New-VM
. Here’s what each parameter means:
-ComputerName hyperv1
: Specifies the name of the computer where the virtual machine will be created.-Name VM1
: Sets the name of the virtual machine as “VM1”.-Generation 2
: Specifies that the virtual machine should be created with Generation 2, which offers certain performance and feature improvements.-MemoryStartupBytes 2GB
: Sets the initial memory (RAM) allocation for the virtual machine to 2GB.-NewVHDPath vm1.vhdx
: Specifies the path where the virtual hard disk (VHD) file for the virtual machine will be created. In this case, it will be named “vm1.vhdx”.-NewVHDSizeBytes 60GB
: Sets the size of the virtual hard disk to 60GB.-SwitchName "ExternalSwitch"
: Specifies the name of the external virtual switch that the virtual machine will be connected to. You need to provide the actual name of the switch.
This command will create a new virtual machine named “VM1” with the specified settings on the computer “hyperv1”.
New-VM -computername hyperv1 -name VM1 -generation 2 -memorystartupbytes 2gb -newVHDpath vm1.vhx -newVHDsizebytes 60000000000 -switchname -external
start-vm -computername hyperv1 -name vm1
The Start-VM
command is used to initiate the startup of a virtual machine. In this case, it starts the virtual machine named “VM1” on the computer “hyperv1”.
get-vm -computername hyperv1
The Get-VM
command is used to fetch information about the virtual machines that are present on the computer “hyperv1”. This command will provide you with details such as the name, state, and other properties of the virtual machines.
In PowerShell Direct
To work with a virtual machine sitting on top of a Hype-V host for working with virtual machines that are on that specific Hyper-V host
Interactive Session
“Enter-PSSession” is a command that allows you to directly enter into a remote session on another computer.
Enter-PSsession -vmname nanoserver1
This command establishes a remote session with a virtual machine named “nanoserver1.” It’s like having a virtual window into that virtual machine.
get-process
This command is executed within the remote session. It retrieves information about the processes running on the “nanoserver1” virtual machine.
Single-use Session
In a single-use session, you can run the desired script or command without the need to establish a long-term connection to the remote machine.
invoke-command -vmname nanoserver 1 -scriptBlock { get-process }
The command executes the Get-Process
command on the remote virtual machine named “nanoserver1” without establishing a persistent connection.
Persistent Session
In a persistent session, you establish a long-term connection to a remote machine that you store in a variable, allowing you to interact with the machine and run commands or scripts as needed.
$session = new-PSSession -vmname nanoserver1 -credential (get-credential)
This command creates a new PowerShell session (New-PSSession
) with the virtual machine named “nanoserver1.” It also prompts for credentials (Get-Credential
) to authenticate the session. The created session is then stored in the variable $session
.
copy-item -fromsession $session -path c:\nanoserver.txt -destination c:\
This command uses the stored session ($session
) to copy a file (Copy-Item
) from the remote virtual machine. It copies the file located at C:\nanoserver.txt
on the virtual machine to the local machine’s C:\
directory.
Upgrade VM
- get-vm
- This command retrieves information about all virtual machines on the local computer.
- get-vm -computername hyperv1
- This command retrieves information about all virtual machines on the computer “hyperv1”. It queries the virtual machines on a remote computer.
- get-vm -computername hyperv1 -name vm2 -version 7.0
- This command retrieves information about a specific virtual machine named “vm2” on the computer “hyperv1” with a specific version of 7.0. It filters the results to return information for that specific virtual machine.
- update-vmversion -computername hyperv1 -name vm2
- This command updates the virtual machine named “vm2” on the computer “hyperv1” to the latest virtual machine configuration version. It ensures that the virtual machine is compatible with the current version of Hyper-V on the computer.
Delegate VM
Delegate VM is a feature in Windows Server that allows administrators to give specific permissions to non-administrative users for managing virtual machines. It gives control over tasks like starting, stopping, checkpointing, and accessing the console of virtual machines. This feature helps organizations distribute management responsibilities without giving full administrative access to everyone.
JEA TOOL
JEA (Just Enough Administration) is a security technology in Windows Server that lets administrators restrict and delegate administrative tasks to non-administrative users or groups. It works by creating special PowerShell sessions with limited capabilities. JEA allows administrators to define what commands and actions users can perform, ensuring they only have access to what they need for their specific tasks. This reduces the risk of accidental or intentional misuse of administrative privileges.
Enter-PSSession hyperv1
This command allows you to connect and interact with a remote computer called “hyperv1” using PowerShell.
New-PSrolecapabilityfile -path ./rebootonly.psrc
This command creates a file named “rebootonly.psrc” that defines a specific set of actions or capabilities.
New-PSrolecconfigurationfile -path ./rebootonly.pssc -full
This command creates another file named “rebootonly.pssc” that holds the complete configuration for a specific role or group of users.
NESTED
“Nested” refers to the concept of running Hyper-V virtual machines (VMs) inside other Hyper-V VMs. In simple terms, it means creating virtual machines within virtual machines.
set-vmprocessor -vmname nanoserver1 -exposevirtualizationExtensions $true
This command configures the processor settings for a virtual machine named “nanoserver1”. By setting the “-exposevirtualizationExtensions” parameter to “$true”, it enables the virtual machine to expose virtualization extensions to the guest operating system.
get-vmnetadapter -vmname nanoserver1 | set-vmnetworkadapter -macaddressSpoofing on
This command retrieves the network adapter configuration for the virtual machine named “nanoserver1”. The “| set-vmnetworkadapter -macaddressSpoofing on” part modifies the network adapter settings.
Conclusion
Configuring Hyper-V on Windows Server opens up a world of possibilities for efficient and flexible server management. By following the steps outlined in this guide, you have learned how to set up and configure Hyper-V, enabling you to create and manage virtual machines effectively. Virtualization offers numerous benefits, including better resource utilization, enhanced scalability, and improved disaster recovery options. With your newfound knowledge of Hyper-V, you are now equipped to make the most of your Windows Server environment and maximize its potential.