- Set up nested Hyper-V virtualization - Mon, May 20 2019
- Set up a Hyper-V home lab with the AutomatedLab PowerShell module - Thu, May 9 2019
Getting started
To get started automating your home lab with the AutomatedLab PowerShell module, we first need to make sure we've got a couple of things up and running ahead of time.
- Hyper-V
- Windows 10: check out the 4sysops article on enabling Hyper-V
- Windows Server: Install Hyper-V using Server Manager
- The AutomatedLab PowerShell Module
Install-Module AutomatedLab -AllowClobber
Before you can deploy your first lab, you'll need to set up AutomatedLab so that it knows where to find the appropriate ISO files. To do this, you must first create what it refers to as the LabSources folder:
New-LabSourcesFolder
After the command finishes, it will output the location of the folder it creates. This is where AutomatedLab will look for installation files for operating systems and supported applications.
If you don't have any operating system ISO files, be sure to check out Microsoft's evaluation website . I grabbed the Windows Server 2019 ISO for this demo. Next, copy the ISO files of the operating system you want to deploy into the \ISOs folder inside of LabSources:
Now you'll want to make sure AutomatedLab recognizes them by running:
Get-LabAvailableOperatingSystem -Path C:\LabSources
Be sure to take note of the OperatingSystemName values; you're going to use these in your script for building your lab.
Building your first lab
Now that you have the AutomatedLab module installed and your operating system ISO files ready, we can build the first lab! To get started, open up your PowerShell script editor of choice. I prefer VS Code, but the Integrated Scripting Environment (ISE) will work fine if you don't want the hassle of installing another program.
In your script, you want to start by defining a Lab Definition—this is the metadata for your lab. As a very basic example, you only need to define a lab name and your virtualization engine, but you can get pretty fancy and set the path and limit resources for the entire lab if desired.
The easiest example to start with is:
New-LabDefinition -Name 'FirstLab' -DefaultVirtualizationEngine HyperV
You'll notice it did a couple of things:
- Put the lab definition files in the default directory of C:\ProgramData\AutomatedLab\Labs
- Ran an update on the SysInternals Suite tools since AutomatedLab uses those extensively
- Rescanned the operating system files
Once you have the lab definition configured, you can start adding virtual machines (VMs) to your lab with the Add-LabMachineDefinition cmdlet.
It is important to note that by running this command you are not creating a VM; you are simply adding the data to the lab definition so that AutomatedLab will know what to create when you install the lab.
We can add a root domain controller; notice I'm using an operating system from before:
Add-LabMachineDefinition -Name DC01 -OperatingSystem 'Windows Server 2019 Datacenter Evaluation (Desktop Experience)' -Roles RootDC -DomainName 'theposhwolf.com'
By specifying RootDC as the role for this server, it will automatically install the specified domain. Also, since I didn't select a network, it assigns one.
Then, if we want to add a member server with no role, we can run:
Add-LabMachineDefinition -Name Member01 -OperatingSystem 'Windows Server 2019 Datacenter Evaluation (Desktop Experience)' -IsDomainJoined -DomainName 'theposhwolf.com'
This defines a member server in the specified domain:
Now that we've defined our two servers, we can have AutomatedLab deploy our lab by running:
Install-Lab
If this is your first time running this command, and, like me, you chose not to specify a storage drive for your VMs in New-LabDefinition, AutomatedLab will run some tests to find the best storage drive.
After it completes its testing, it will also need to adjust some PowerShell remoting settings so it can talk to the VMs once they start up. If you are running your lab on a work computer, chances are you won't be able to adjust many of the settings, but for a home system or a dedicated lab server, these are fine.
Now this will take some time to run because it will be creating the VMs and setting up Active Directory with any other roles that you defined.
After it finishes, assuming you have the Hyper-V Manager installed, you can launch that and admire your easily built VMs!
If you didn't specify a password, the default is: Somepass1.
Next, you can have AutomatedLab clean everything up by running:
Remove-Lab
Looking forward
If you save your script as a .ps1 file, any time you need to rebuild that lab, all you need to do is to run the script, wait the 10 to 15 minutes, and you'll have a lab ready to go!
If you'd like to see some of the advanced features of AutomatedLab, be sure to check out their Github repository.
Anthony,
This is a very convenient tool for automating the process of building a Hyper-V lab. I followed the steps and was succesful in creating the VMs and connecting to them. I'm running into an issue joining my host machine to the domain though, I receive the following error:
The following error occurred when DNS was queried for the service location (SRV) resource record used to locate an Active Directory Domain Controller (AD DC) for domain "homelab.local":
The error was: "DNS name does not exist."
(error code 0x0000232B RCODE_NAME_ERROR)
One thing to note: I don't have access to the router that the host machine is connected to, as I use the WiFi provided by my apartment complex.
Chris,
you will need to modify your host DNS settings to point to the created Domain controller, not to your router. Actually you dont need to join the Hyper-V host to your newly build domain. It will work even without it.
Leos
Yea, I still want to some add devices to the domain for experimentation purposes (I'm taking my MCSA soon). Does the DC need to have a static IP?
You may add as many devices to the domain as you like, you just need to configure proper DNS server on the device (your DC) so it finds proper SRV record for your domain.
Yes, normally DNS/DC have static IPs as you need to configure this on a client somehow (manually or via DHCP).
With further development, AutomatedLab utility can be considered for vagrant replacement at least for Microsoft Platform.
How do i get the lab to actually be permanent? Seems like it deletes itself after a day yet when I go to try and recreate it, it doesn't let me use the same server names. Tells me the server name is already assigned.
I honestly haven't seen that issue before. I've got several labs that have been around for weeks.
Are the VMs not showing up in Hyper-V Manager after a day? Or what are you seeing?
I’m getting this error message when I try to recreate the machines by running Add-LabMachineDefinition -Name DCSRV -OperatingSystem ‘Windows Server 2019 Standard Evaluation
(Desktop Experience)’ -Roles RootDC -DomainName ‘homelab.local’
Write-Error : A machine with the name 'DCSRV' does already exist
At C:Program FilesWindowsPowerShellModulesAutomatedLabDefinition5.9.0AutomatedLabDefinition.psm1:2055 char:13
+ Write-Error $errorMessage
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Write-Error
Write-LogFunctionExitWithError : Add-LabMachineDefinition…leaving: A machine with the name 'DCSRV' does already exist
At C:Program FilesWindowsPowerShellModulesAutomatedLabDefinition5.9.0AutomatedLabDefinition.psm1:2056 char:13
+ Write-LogFunctionExitWithError -Message $errorMessage
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Write-LogFunctionExitWithError
Hi Chris,
Please run Remove-Lab to remove the entries from the config files and clean environment.
And then start over.
Also, the error implies VMs were no deleted through Automated Lab. The Remove function of Automated Lab removes the local configuration file entries as well.
This is really great stuff!
I do have a question, though:
Is there a way to specify the location of the Lab Sources folder? It looks like it defaults to C:\LabSources and it is unclear if you can specify a different location.. I have limited room on my C: drive and would rather place it on my Data volume.
@David Gross
I just ran the following:
You can always use the Get-Help cmdlet to see what parameters are available to commands.
Hope that helps.
Is there a way to add a vm to a lab after it has been created and used? I see the new-labvm module. I wonder if I can add it the the definition and then use the new-labvm command.