{"id":75171,"date":"2015-12-10T00:01:00","date_gmt":"2015-12-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/10\/using-deployimage-and-windows-powershell-to-build-a-nano-server-part-4\/"},"modified":"2019-02-18T09:20:43","modified_gmt":"2019-02-18T16:20:43","slug":"using-deployimage-and-windows-powershell-to-build-a-nano-server-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-deployimage-and-windows-powershell-to-build-a-nano-server-part-4\/","title":{"rendered":"Using DeployImage and Windows PowerShell to Build a Nano Server: Part 4"},"content":{"rendered":"<p><b>Summary<\/b>: Use the new cmdlets in the <b>DeployImage<\/b> module to simplify the deployment of a Nano Server.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here to get into the really fun part of deploying a Nano Server&mdash;the actual deployment part.<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-deployimage-module-and-powershell-to-build-a-nano-server-part-1\/\" target=\"_blank\">Use DeployImage Module and PowerShell to Build a Nano Server: Part 1<\/a><br>Introducing the&nbsp;<b>DeployImage<\/b>&nbsp;module and the cmdlets for Nano Server.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/08\/use-deployimage-module-and-powershell-to-build-a-nano-server-part-2.aspx\" target=\"_blank\">Use DeployImage Module and PowerShell to Build a Nano Server: Part 2<\/a><br>Use the&nbsp;<b>New-NanoServerWim<\/b>&nbsp;cmdlet from the&nbsp;<b>DeployImage<\/b>&nbsp;module to build an updated Nano Server WIM file.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/09\/use-deployimage-module-and-powershell-to-build-a-nano-server-part-3.aspx\" target=\"_blank\">Use DeployImage Module and PowerShell to Build a Nano Server: Part 3<\/a><br>Use the&nbsp;<strong>New-UnattendXMLContent<\/strong>&nbsp;cmdlet in his&nbsp;<strong>DeployImage<\/strong>&nbsp;module to automate naming the Nano Server.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/10\/using-deployimage-and-windows-powershell-to-build-a-nano-server-part-4.aspx\" target=\"_blank\">Using DeployImage and Windows PowerShell to Build a Nano Server: Part 4<\/a><br>Use the new cmdlets in the&nbsp;<b>DeployImage<\/b>&nbsp;module to simplify the deployment of a Nano Server.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/11\/using-deployimage-and-windows-powershell-to-build-a-nano-server-part-5.aspx\" target=\"_blank\">Using DeployImage and Windows PowerShell to Build a Nano Server: Part 5<\/a><br>Use the&nbsp;<b>DeployImage<\/b>&nbsp;module to build bootable Windows PE media with deployment content.<\/li>\n<\/ul>\n<p>Yesterday, I showed you the <b>New-UnattendXMLContent<\/b> cmdlet to help you name the Nano Server and provide direct settings, such as the time zone. Today we&rsquo;re going to look at creating partitions and applying the image&mdash;our actual process.<\/p>\n<p>If you were to try to apply Nano Server to a VHD file, it&rsquo;s actually very easy in Windows&nbsp;10. You can natively create the VHD, mount it, and then access it directly as if it were a physical disk. Following is a simple example of creating a new VHD file, mounting it, and then assigning it to a disk object in PowerShell. Our example is called Sample.VHD and is a 20 gigabyte dynamic disk in the C:\\FOO folder:<\/p>\n<p style=\"margin-left:30px\">New-VHD C:\\FOO\\sample.vhd -SizeBytes 20gb &ndash;Dynamic<\/p>\n<p style=\"margin-left:30px\">Mount-VHD C:\\FOO\\sample.vhd<\/p>\n<p style=\"margin-left:30px\">$Disk=Get-Disk C:\\FOO\\sample.vhd<\/p>\n<p>If this was a physical disk inside a computer (not a VHD), I would normally use the <b>Get-Disk<\/b> cmdlet and filter on the type of SCSI, SATA, or SAS. But <b>DeployImage<\/b> has a cmdlet with that wrapped into it. We can grab all internal disks in the following manner:<\/p>\n<p style=\"margin-left:30px\">Get-AttachedDisk<\/p>\n<p>It&rsquo;s simply a wrapper around Get-Disk | Where-Object { $_.BusType }, but I find it a little nicer to use.<\/p>\n<p>For a physical disk on a target server (if there is only one), we can use this:<\/p>\n<p style=\"margin-left:30px\">$Disk=Get-AttachedDisk<\/p>\n<p>Next we need to partition the disk. For this, I&rsquo;ve set up a small cmdlet to deal with the two most common partition setups for WIM files: basic MBR and GPT.<\/p>\n<p>You can create your own custom script for this, but if you&rsquo;d like to easily create a GPT partition on a physical disk, you can use the <b>New-PartitionStructure<\/b> cmdlet. It has three mandatory parameters for a physical disk: the disk object, the operating system drive letter, and the boot drive letter.<\/p>\n<p>This cmdlet will clear the old partition structure, create a new one, format the needed drives, and assign the drive letters. Here is an example on a physical disk with the new boot drive being assigned drive letter G and the operating system drive being assigned drive letter H:<\/p>\n<p style=\"margin-left:30px\">$OSDrive=&rsquo;H&rsquo;<\/p>\n<p style=\"margin-left:30px\">$BootDrive=&rsquo;G&rsquo;<\/p>\n<p style=\"margin-left:30px\">New-PartitionStructure -Disk $disk -BootDrive $Bootdrive -OSDrive $OSDrive<\/p>\n<p>We can use the same structure on a virtual disk, but we can also lay out a simple MBR structure. This will be part of the simple process of converting a WIM to a VHD. In this case, both the boot drive and the operating system share the same drive.<\/p>\n<p style=\"margin-left:30px\">New-PartitionStructure -Disk $disk &ndash;MBR -BootDrive $Bootdrive -OSDrive $OSDrive<\/p>\n<p>Now that we have a partition structure, we can use the <b>Expand-WindowsImage<\/b> cmdlet on the operating system drive letter.<\/p>\n<p>Remember that custom Nano Server WIM file we created on Tuesday? Grab the location provided (it was defaulting to &lsquo;C:\\Nanotemp\\Nanocustom.wim&rsquo;):<\/p>\n<p>$Wimfile=&rsquo;C:\\Nanotemp\\Nanocustom.wim&rsquo;<\/p>\n<p style=\"margin-left:30px\">Expand-WindowsImage &ndash;imagepath &#8220;$wimfile&#8221; &ndash;index 1 &ndash;ApplyPath &#8220;$OSDrive`:\\&#8221;<\/p>\n<p>Now that we have an image on a disk, we can build out our Unattend file content to automatically name the system and join it to the Contoso domain (remember yesterday&rsquo;s example):<\/p>\n<p style=\"margin-left:30px\">$Content= New-UnattendXMLContent &ndash;computername &lsquo;TESTNano&rsquo; &ndash;joindomain &ndash;Domainname &lsquo;Contoso&rsquo; &ndash;Domainaccount &lsquo;JoinAccount&rsquo; &ndash;DomainPassword &lsquo;P@ssw0rd&rsquo; &ndash;DomainOU &lsquo;CN=Ourusers,DC=Contoso,DC=local&rsquo;<\/p>\n<p style=\"margin-left:30px\">Add-Content C:\\Foo\\Unattend.xml &ndash;value $content<\/p>\n<p style=\"margin-left:30px\">Copy-item C:\\Foo\\Unattend.xml &ndash;destination &ldquo;$OSDrive`:\\Windows\\system32\\Sysprep\\unattend.xml&rdquo; &ndash;force<\/p>\n<p>We are actually almost done&mdash;except for two key pieces. We need drivers for the target computer (we need to see that hard disk and network card), and we need to apply boot code.<\/p>\n<p>The cmdlet for adding drivers is nice and simple&mdash;you need to provide the source folder for your drivers and the destination for the Windows folder. The beautiful part is that the cmdlet will recurse through the folder structure and grab all the drivers from there.<\/p>\n<p style=\"margin-left:30px\">$DriverPath=&rsquo;C:\\Drivers&rsquo;<\/p>\n<p style=\"margin-left:30px\">Add-WindowsDriver -Driver $DriverPath -Recurse -Path &#8220;$OSDrive`:\\&#8221; -ErrorAction SilentlyContinue<\/p>\n<p>Now there is only one additional piece&mdash;making the system bootable. There are two commands in Windows for making a disk bootable: BCDBoot and Bootsect. For most cases, we can use BCDBoot.<\/p>\n<p>To make this process more seamless, BCDBoot and Bootsect have been wrapped into a cmdlet called <b>Send-Bootcode<\/b>, which only needs the drive letter for the boot drive and the operating system drive.<\/p>\n<p style=\"margin-left:30px\">Send-Bootcode &ndash;bootdrive $Bootdrive &ndash;osdrive $OSdrive &ndash;bootdrive $Bootdrive<\/p>\n<p>&#8230;and we&rsquo;re done. Yep. You&rsquo;ve just deployed a Nano Server. If you use a VHD as the target and create the MBR partition with the same code, you&rsquo;ve converted a WIM to a VHD.<\/p>\n<p>Within the <b>DeployImage<\/b> structure is a script called DeployNanoServerPhysical.ps1, which is a simple example of this post in action.<\/p>\n<p>Come back tomorrow for our final day of this series. I&rsquo;ll combine last week&rsquo;s work of a bootable Windows PE image with PowerShell and this week&rsquo;s work. I&rsquo;ll show you how to automatically build out deployment media.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy, Cloud and Datacenter Management MVP&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the new cmdlets in the DeployImage module to simplify the deployment of a Nano Server. Honorary Scripting Guy, Sean Kearney, is here to get into the really fun part of deploying a Nano Server&mdash;the actual deployment part. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: Use DeployImage Module and PowerShell [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,651,154,45],"class_list":["post-75171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-nano-server","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the new cmdlets in the DeployImage module to simplify the deployment of a Nano Server. Honorary Scripting Guy, Sean Kearney, is here to get into the really fun part of deploying a Nano Server&mdash;the actual deployment part. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: Use DeployImage Module and PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75171\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}