Hyper-V HomeLab Intro

综合技术 2017-04-13

So I’ve been playing with Hyper-V for a while now. If you recall it was one of my2016 goalsto build a virtualization lab.

I’ve done that, building out the base Microsoft Test Lab Guide
several times:

  • Manually (clicking in the GUI)
  • Using PowerShell commands (contained in the guides)
  • Using Lability
    and PS-AutoLab-Env

I was also fortunate enough to be a technical development editor for Learn Hyper-V in a Month of Lunches
, which should be released this fall.

One thing that I’ve found is that being able to spin up a VM quickly is really nice. With the Hyper-V cmdlets, that’s pretty easy.

Spinning up a machine from scratch and building a bootable image is not as easy. Fortunately there are some tools to help.

In this post, I’m going to share a simple function I’ve written to help me get things built faster.

The goal of the function is to take the following information:

  • Which ISO to use
  • Which edition from the ISO to select
  • The Name of the VM (and VHDX)
  • How much memory
  • How many CPUs

With that information, it converts the windows image from the ISO to a VHDX, creates a VM with the right specs and using the VHDX, sets up the networking (or starts to, anyway), and starts the VM.

The bulk of the interesting work is done by Convert-WindowsImage
, a function that pulls the correct image from an ISO and creates a virtual disk.

There are some problems with that script (read the Q&A on the Technet site and you’ll see what I mean). The main one is when it tries to find the edition you ask for (by number or name). The code is in lines 4087-4095, and should look like this:

$Edition | ForEach-Object -Process {

                    $Edtn = $PSItem
    
                    if ([Int32]::TryParse($Edtn, [ref]$null)) {
                        $openImage = $openWim[[Int32]($Edtn)]    
                    } else {
                        $openImage = $openWim[$Edtn]
                    }

There’s a more recent copy of the function on github, but it has slightly different parameters and seems to be stale as well (according to the page it’s on). I’ve got an email out to find the “live” version.

With that, here’s my function:

function new-BootableVM {
    param($ISOPath = 'E:isos2012R2_x64_EN_Eval.iso',
        $Name,
        $MemoryInGB,
        $vCPUs,
        [switch]$Stopped)


    $switch = 'LabNet'
    $vhdpath = "c:usersPublicDocumentsHyper-VVirtual hard disks$name.vhdx"

    Convert-WindowsImage -SourcePath $ISOPath -Edition $edition -VHDPath $vhdpath -VHDFormat VHDX -VHDType Dynamic -SizeBytes 8GB 
    $vm = New-VM -Name $name -MemoryStartupBytes ($memoryInGB * 1GB) -VHDPath $vhdpath -Generation 2 
    Set-VMProcessor -VM $vm -Count $vCPUs
    Add-VMNetworkAdapter -vm $vm -SwitchName $switch

    if (!$stopped) {
        Start-VM -VM $vm
    }
    $vm
}

Once the function is done running (assuming it didn’t have any issues), a VM will be created and ready for you. You will need to accept the license, set the locale, and set the administrators password, but that only takes a minute. I’ll be adding functions (or adding to this function) to take care of those as well as things like renaming the guest, joining a domain, copying files to the drive, etc.

It’s still a work in progress, so you will see some hardcoded values. Hopefully you can see what’s going on and adapt it to your needs.

I’ll be writing more as I play more with Hyper-V, DSC, and containers.

Let me know what you think

–Mike

责编内容by:PowerShell Station (源链)。感谢您的支持!

您可能感兴趣的

Microsoft Hyper-V 2016 Features list Quite an impressive list of new stuff in the Hyper-V are with 2016 release, and ...
Android 模拟器添加对AMD和Hyper-V的支持 自两年前谷歌对 Android 模拟器进行重大更新以来,我们一直致力于开发出一款运行速度快、功能全面的模拟器,帮助您为用户打造卓越应用体验。Android 模拟...
Swapping PVS MAC Addresses for Hyper-V VDI Machine... I ran into a bit of an irritating problem while provisioning some VDI machines o...
深入了解通过访客虚拟机群集实现共享虚拟存储... 访客群集是故障转移群集,其中所有群集点都是虚拟机。访客群集提供了一种方式让单个工作负载实现高可用性。与基于物理服务器的Windows故障诊断群集一样,访客群集也...
CPanel Unable to Add Addon Domain indicates that i... Unable to redirect addon domain 301 . I have the current s...