Deploy your domain joined and ip configured Nano server

With Windows Server 2016 Technical Preview 4 that has been released by Microsoft, a few weeks ago, a lot of person start playing with the new Nano Server…including myself. Smile

As part of my requirements, I would like to get my Nano Server deployed directly from the ISO file, AD Domain Joined, IP Configured, Firewall disabled (oops Smile with tongue out)…. In order to be able to do all that in an automated way, I created a small PowerShell script that I would like to share with you.

Run this script from the regular PowerShell Command Prompt (on you Hyper-v 2016 TP4 host) and not from PowerShell ISE. If you are running it from PowerShell ISE, for a reason that I didn’t figure it out yet, you risk to receive the following error message:ERROR  : Cannot find type [WIM2VHD.WimFile]: verify that the assembly containing this type is loaded.

#####################
#Parameters

#Script Info
$ScriptPath = "C:\Users\Christopher\Downloads"
$ISOPath = "C:\Users\Christopher\Downloads\ISO\10586.0.151029-1700.TH2_RELEASE_SERVER_OEMRET_X64FRE_EN-US.ISO"

#Domain Info
$DomainName = "vnextlab.be"
$DCName = "DC001"
$DCUser = "Administrator"
$DCPassword = ConvertTo-SecureString "P@$$w0rd01" -AsPlainText -Force

#Nano Server Info
$NanoServerName = "NANOSRV001"
$NanoAdminPwd = ConvertTo-SecureString 'P@$$w0rd01' -AsPlainText -Force 
$NanoIP = '192.168.50.201'
$NanoMask = '255.255.255.0'
$NanoGW = '192.168.50.1'
$NanoDNS = '192.168.50.10'

#Nano VM Configuration
$VMCores = 2
$VMMemory = 2  #GB
$VMVHDPath = "E:\Hyper-V"
$VMSwitch = "vSwitch"


#####################
#Script

# Mount the OS ISO image onto the local machine
Mount-DiskImage -ImagePath $ISOPath 
$iSOImage = Get-DiskImage -ImagePath $ISOPath | Get-Volume
$iSODrive = "$([string]$iSOImage.DriveLetter):"

#Copy the module and script
Copy-Item -Path $iSODrive\NanoServer\NanoServerImageGenerator.psm1 -Destination $ScriptPath -force
Copy-Item -Path $iSODrive\NanoServer\Convert-WindowsImage.ps1 -Destination $ScriptPath -force

#Connection to the domain controller via PowerShell Direct
$Creds = New-Object System.Management.Automation.PSCredential ("$DomainName\$DCUser", $DCPassword)

$Domainblob = Invoke-Command -VMname $DCName -Credential $Creds -Scriptblock {
    
    param($DomainName, $NanoServerName)
    
    djoin.exe /provision /domain $DomainName /machine $NanoServerName /savefile $env:TEMP\$NanoServerName | Out-Null
    $Domainblob = Get-content $env:TEMP\$NanoServerName
    Remove-item $env:TEMP\$NanoServerName -force

    $Domainblob 
} -ArgumentList $DomainName, $NanoServerName

#Save the Domainblob File Locally
$Domainblob | Out-File $ScriptPath\$NanoServerName

#Nano Server Info
#Create the VHD
Import-Module $ScriptPath\NanoServerImageGenerator.psm1
New-Item $ScriptPath\$NanoServerName-tmp -type directory
New-NanoServerImage -MediaPath $iSODrive -BasePath $ScriptPath\$NanoServerName-tmp -TargetPath $ScriptPath\$NanoServerName.vhd -GuestDrivers -AdministratorPassword $NanoAdminPwd -DomainBlobPath $ScriptPath\$NanoServerName -InterfaceNameOrIndex Ethernet -Ipv4Address $NanoIP -Ipv4SubnetMask $NanoMask -Ipv4Gateway $NanoGW

#Cleanup
Remove-Module -Name NanoServerImageGenerator
Remove-Item $ScriptPath\$NanoServerName
Remove-Item $ScriptPath\$NanoServerName-tmp -Recurse -force
Remove-Item $ScriptPath\NanoServerImageGenerator.psm1 -force
Remove-Item $ScriptPath\Convert-WindowsImage.ps1 -force
Dismount-DiskImage -ImagePath $ISOPath 

#####################
#Create the VM

New-Item $VMVHDPath\$NanoServerName -type directory
Copy-Item -Path $ScriptPath\$NanoServerName.vhd -Destination $VMVHDPath\$NanoServerName
Remove-item $ScriptPath\$NanoServerName.vhd -Force

$VMMemory= $VMMemory * 1024 * 1024 * 1024
New-VM –Name $NanoServerName –MemoryStartupBytes $VMMemory –VHDPath $VMVHDPath\$NanoServerName\$NanoServerName.vhd -SwitchName $VMSwitch
SET-VMProcessor –VMName $NanoServerName –Count $VMCores
Start-VM –Name $NanoServerName

#####################
#Configure tne VM

#Check if the computer is online / Need to find something better.
Start-Sleep 120

#Connection to the VM via PowerShell Direct
$Creds = New-Object System.Management.Automation.PSCredential ("$NanoServerName\Administrator", $NanoAdminPwd)
Invoke-Command -VMName $NanoServerName -Credential $Creds -Scriptblock {
    
    param($NanoDNS)

    #Disable the firewall
    netsh advfirewall firewall add rule name="Local Any" dir=in action=allow profile=any remoteip=any 

    #Set the DNS
    Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses $NanoDNS

    shutdown -r -t 5

} -ArgumentList $NanoDNS

In a few minutes, the script will create the configured VHD and deploy it to Hyper-V:

image

you could now manage your brand new Nano server via PowerShell:

image

Or remotely from another server:

image

Have a nice day
Christopher

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Email this to someoneShare on TumblrPin on PinterestDigg thisShare on RedditFlattr the authorBuffer this pageShare on StumbleUpon

About Christopher Keyaert

Christopher Keyaert is a Consultant, focused on helping partners to leverage the System Center and Microsoft Azure cloud platform. He is also a Microsoft Most Valuable Professional (MVP) for Cloud and Data Center Management and a Microsoft Certified Trainer (MCT).
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Deploy your domain joined and ip configured Nano server

  1. Ferry says:

    awsome, thanks for this create script!
    saves a lot of time for others 🙂

    • Christopher Keyaert says:

      Happy to help 🙂
      Concerning the error msg: ERROR : Cannot find type [WIM2VHD.WimFile]: verify that the assembly containing this type is loaded.
      It seems that it comes just after the installation of the hyper-v role, it could be a bug, Microsoft is currently looking at it.

Leave a Reply

Your email address will not be published. Required fields are marked *