Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Using DeployImage and Windows PowerShell to Build a Nano Server: Part 5

$
0
0

Summary: Use the DeployImage module to build bootable Windows PE media with deployment content.

Honorary Scripting Guy, Sean Kearney, is here today to show you one of my favorite pieces of my DeployImage module—building a piece of bootable USB media that has everything I need to deploy Nano Server.

   Note   This is a five-part series that includes the following posts:

Last week in the series, Build a PowerShell-Enabled Windows PE Key, I talkedl about building a Windows PE USB key with Windows PowerShell. This week, I explored how to use the new DeployImage module to build out Nano Server. Today we’re going to combine them.

Much of the work we did last week…well, that’s now simply a few cmdlets in the DeployImage module. We can now build out the custom WIM file with Windows PowerShell with the New-WindowsPEWim cmdlet.

If you provide no parameters, it will presume the Windows 10 ADK is on drive C: and that you’d like the custom WIM file to exist at C:\Pewim\custom.wim. Running this command will build the custom Windows PE WIM file with Windows PowerShell built in, and it will capture the resulting file and path into the $Wimfile object:

$Wimfile=New-WindowsPEWim

You can provide an alternate destination and temporary folder by using the WinPETemp and Destination parameters. The following example will use a temporary folder called C:\WinPEtemp and place the custom.wim file in  the C:\CustomWim folder:

New-WindowsPEWim –WinPETemp ‘C:\WinPETemp’ –Destination ‘C:\CustomWim’

It also tweaks that environment to automatically launch PowerShell and attempt to import the DeployImage module.

From this point, we pick a drive Letter to use for the USB media, grab an available USB disk from the menu, and partition it:

$OSDrive=’L’

$Disk=Get-AttachedDisk -USB -GUI

New-PartitionStructure -Disk $disk -OSDrive $OSDrive -USB -MBR

We then build out the Windows PE media folder from the Windows 10 ADK content:

$WinPETemp='C:\TempPE'

$OSdrive='L'

$Env:WinPERoot="$($WinPEDrive)`:\Program Files$(Get-ArchitectureString)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment"

$WinADK="$($Env:WinPERoot)\amd64"

Remove-item -Path $WinPETemp -Recurse -Force

New-Item -ItemType Directory -Path $WinPETemp -Force

Copy-Item -Path "$WinAdk\Media" -Destination $WinPETemp -Recurse -Force

New-Item -ItemType Directory -Path "$WinPETemp\Media\Sources" -Force

Copy-Item -path $Wimfile -Destination "$WinPETemp\Media\Sources\boot.wim"

The following command partitions the USB media we selected earlier, drops in our Windows PE structure, and makes it bootable:

New-PartitionStructure -Disk $disk -OSDrive $OSDrive -USB -MBR

$WinPEKey=$OsDrive+':'

Copy-Item -Path "$WinPETemp\Media\*" -destination "$WinPeKey\" -Recurse

Send-BootCode -OSDrive $OSDrive -USB

We need to identify the current location of the DeployImage module. To be honest, it could be in my GitHub folder, your personal modules folder, or almost anywhere. To do this, we’ll grab its location from the Path property when using Get-Module, and we’ll pipe this to Split-Path to get only the parent folder:

$Modulepath=Split-path ((get-module deployimage).path)

We can now create a folder to hold the module and copy it to our Windows PE key:

New-Item -Path "$WinPeKey\DeployImage" -ItemType Directory -Force

Copy-Item -Path "$ModulePath\*" -Destination "$WinPEkey\DeployImage" -Recurse

If we have any drivers to bring over to Nano Server, we’ll populate a driver’s folder on the Windows PE key:

New-Item -Path "$WinPEKey\Drivers" -ItemType Directory -Force

Copy-Item -Path "$DriverPath\*" -Destination "$WinPEkey\Drivers" –Recurse

Then of course, the most critical piece, the Nano Server media:

New-Item -Path "$WinPeKey\NanoServer" -ItemType Directory -Force

Copy-Item -Path "$($NanoMedia)NanoServer\*" -Destination "$WinPEKey\NanoServer\" -Recurse

Copy-Item -Path $CustomNano -Destination "$WinPeKey\NanoServer"

If you built out a custom Nano Server WIM file, you can also copy that over. Personally, I would throw it into the Nano Server folder—seems to make more sense:

Copy-Item –path C:\Nanotemp\NanoCustom.wim –destination “$WinPeKey\NanoServer”

Believe it or not, that’s it! You should now be able to have a USB key that boots into Windows PowerShell 5.0, contains all the media and scripts to not only build a custom Nano Server WIM file, but also deploy it to a physical disk.

The DeployImage module is available at the following locations:

This module can be used easily to deploy any WIM file including Windows to Go.

This is by no means a final production. If you have feedback, comments, or critique, by all means pass it along to me. There is a lot that can be done to improve this module, including error checking, trapping, and additional Help.

Don’t miss next week when Dave Wyatt talks all about using Pester in Windows PowerShell.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>