Installing Hyper-V
Hyper-V is just a feature of Windows. If you are running Windows Server you can see that the feature is already there, you just have to enable it. Run this script to find it:
Get-WindowsFeature Hyper-V |
If you are running Windows 10 and have hardware that can handle it, you can enable Hyper-V too. You can find it using this script:
Get-WindowsOptionalFeature -FeatureName "Microsoft-Hyper-V*" -online | Format-Table |
To enable Hyper-V on Windows Server, use the short script below:
Install-WindowsFeature Hyper-V –Restart |
If you don't want your machine to immediately restart, just get rid of the -Restart parameter.
To enable Hyper-V on Windows 10, use the following script:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All |
If you just installed the Hyper-V role on Windows Server you still need to install the Hyper-V PowerShell Module using the script below:
Add-WindowsFeature RSAT-Hyper-V-Tools -IncludeAllSubFeature |
If you are on Windows 10, the Enable-WindowsOptionalFeature script you ran above has already installed it. Either way you will need to restart your computer.
If you had to change BIOS settings, or if you have to do so later, make sure you fully power off, and not just restart through the operating system to ensure the changes take effect for the processor.
Finally, make sure you update all of your help files since you just installed a new PowerShell module and you want to make sure you have the latest help files:
Update-Help |
Creating Virtual Machine Resources
A virtual machine that can connect to the outside world will consist of the following components:
- A virtual machine - This is a space to actually run the guest operating system
- Boot media - For this exercise we will use an Ubuntu ISO just because it's easy
- A virtual DVD drive to mount the iso to the VM
- A virtual disk to install the guest operating system into and save changes
- A virtual network adapter - This is pretty literally a virtual version of a network card that you would install into a server
- A virtual network switch - The virtual switch is what the virtual network card connects to and provides a path for data to get to and from the guest operating system through the host
First we are going to create a couple directories to hold the files we need. Later when you get comfortable with creating VMs you can start to put files wherever works best for you.
New-Item -ItemType directory -Path ("c:\vm","c:\vm\iso") -Force |
Getting the Ubuntu install media is easy. We're using Ubuntu Desktop. Download the ISO and put it in c:\vmiso
We will continue laying the groundwork by creating a virtual switch before we create the virtual machine. To create the virtual switch, we need to know the name of the network adapter it will connect to with the host operating system. You probably created it a long time ago and forgot the name, but don't worry, there's an easy way to find the name you need with the script below.
Conveniently we only have one. The name is all you'll need to create the switch as shown below.
Combining these two kinds of switches we can create configurations such as a group of machines in a virtual intranet with a single VM acting as a gateway.
Next we'll create the virtual hard disk as a fixed size vhdx file.
Creating VMs
To create a Hyper-V VM takes just one short command as shown below.
The command above referred to the vhdx we created earlier, which was a pre-sized disk. If you are low on drive space you could also bypass creating a fixed size disk and create a VM first with a dynamically sized disk that would grow as you used it. The command for dynamic sized Hyper-V VMs is show below.
For this demo we will continue with the first machine and the fixed-size disk, but we aren't going to start it just yet. If we start it right now we won't have any way to boot an operating system or connect to the internet.
To add a boot device, we will map the ISO to a virtual DVD drive using this command:
Add-VMDvdDrive -VMName "Ubuntu Desktop" -Path .\iso\ubuntu-16.04-desktop-amd64.iso |
If you forget to do that first and start the VM you will not only get a boot failure, but you will have to shut down the VM before you can add the drive. Attempting to add it while the VM is running will only result in an error.
We could stop here and boot the machine, but we'll connect the network adapter first to make sure we can connect to the internet. In order to create the network adapter, we need to know the name of the switch we created earlier. If you don't remember the name that's no problem, we can find any virtual switches on the machine as shown below.
Since this is PowerShell, to connect the adapter to the switch we can use the Pipeline. First we'll look at the VM we just created and find the network adapters. There should only be one.
Having confirmed there's only one, we can pipe it to the next Cmdlet to get it connected to the switch and out to the Internet.
You won't get any return from this script so to confirm that it works, just run the first part of it again.
The SwitchName property tells us that the virtual adapter is now connected to the virtual switch.
Starting And Working With VMs
With the network adapter connected and the boot disk in place we can now start the VM. You can start it with the Start-VM Cmdlet:
Start-VM -Name "Ubuntu Desktop" |
Before we connect to the VM though, let's look at some of its properties in PowerShell and confirm that everything is working as expected. The screenshot below shows one way to do that.
We can see the machine is running and now has an uptime number, and the network adapter has a MAC address.
To connect to the VM and see what it looks like, click on the start menu, type Hyper-V, and click on the Hyper-V Manager.
Once in the Hyper-V Manager, right click on your new VM and click connect as shown below.
When you connect, the boot disk has probably had a minute to get the machine started, so you should see something like this:
If you continue with the installation the VM will eventually want to reboot, and you will probably get the same error message that you would if you rebooted a physical machine and forgot to remove the boot disk. The VM will complain that OS installation is complete and you need to remove the install media.
Removing the virtual disk is easy. Just execute the script below:
Stop-VM "ubuntu desktop" -Passthru | Get-VMDvdDrive | Remove-VMDvdDrive |
Before we restart the VM let's create a checkpoint just to show how easy it is.
First we checkpoint the VM and create a unique name for it so it's easy to get back to exactly that spot.
Get-VM "ubuntu desktop" | Checkpoint-VM -SnapshotName 'Starting Point' |
Make any changes you like and then run the following script to revert the machine back to the snapshot.
Get-VM "ubuntu desktop" | |
Since those Cmdlets all accept and pass pipeline input, you can run that same command to checkpoint and restore any number of VMs you like. You can run a demo on a ten server environment, and then restore them to a check point and run the demo all over again.
No comments:
Post a Comment