Creating a Nano server with Powershell

Published on 16 October 2018

A critical part of any modern IT infrastructure should be the ability to spin up replacement servers at the drop of a hat. We should be long past the point of spending hours diagnosing errors on a users PC, and the same should apply to servers. In fact, it should be easier on servers because of standards that are applied in the datacenter, and no configuration drift. Right? :)

So why create an image from a sript rather than a template? Flexibility really. If you have "gold images" or templates, you need to maintain them, which means applying updates, testing, and hoping that nothing slips by your regressing testing which means that the changes you just made to you image, don't stop it working if you need to rebuild a server you built 12 months ago. Assuming you test that kind of thing at all.

What else? A template tends to be specific to an infrastruture type (VMware, Hyper-V, AWS, Azure, whatever) where a good script can potentially allow you to create the same server on different platforms. For example both Hyper-V and VMware support a "new-VM" command. Sure, the syntax isn't quite the same so you need to make some platform specific changes, but actually, that is easy if you get your scripted install working. And of course you could use the same script for different platforms, just by accepting a parameter at the start which sets the desired platform...

So, creating a Nano server image is actually pretty straight forward. You need to load the nanoserverimagegenerator module from your Windows Server media, then run the "new-nanoserverimage" command:

import-module d:\nanoserver\nanoserverimagegenerator
$params = @{
    deploymenttype = 'guest'
    Edition = 'Standard' 
    mediapath = 'D:\'
    targetpath = 'C:\VHDs\server2.vhdx'
    Computername = 'server2'
    Domainname = ''
    enableremotemanagement = $true
    Package = 'Microsoft-NanoServer-DSC-Package', 'Microsoft-NanoServer-IIS-Package'
    InterfaceNameorIndex = 'Ethernet'
    reuse = $true

New-NanoServerImage @params


So there, I am importing the module from my ISO, mounted on d:, setting my parameters, and then creating the image. In this case, I am adding two packages to my base image, and it is using DHCP. The base image ends up not much more than 500mb. Not bad for a Windows Server.

DHCP? Really? Yes, really. When creating a VM based on this image, I set a MAC address as part of that process, which matches a reservation in DHCP, so I know what the IP address will be as soon as the server is up, so this way is much more straight forward.

Note that if the PC you create the image from (that you run the above command on) is NOT in the domain that you specify, you can't join it like this, you need to use a domainblob.

comments powered by Disqus