{"id":2024,"date":"2014-02-10T00:01:00","date_gmt":"2014-02-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/02\/10\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-1\/"},"modified":"2014-02-10T00:01:00","modified_gmt":"2014-02-10T00:01:00","slug":"set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-1\/","title":{"rendered":"Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 1"},"content":{"rendered":"<p><b>Summary<\/b>: Use the freely available Convert-WindowsImage.ps1 to extract multiple bootable VHD files for Hyper-V Server&nbsp;2012&nbsp;R2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy!<\/p>\n<p>Is there an easier way to create a VHD file that already has the operating system, rather than installing it and clicking Next&hellip;Next&hellip;Next? I&rsquo;d like something that could spin up a simple machine like it was a template.<\/p>\n<p>&mdash;JA<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JA,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. Funny you should ask. I was poking about with that idea the other day. I decided for a good simple lab, I would need the following:<\/p>\n<ul>\n<li>A blank preconfigured VHD file that I could copy for a virtual machine<\/li>\n<li>A domain controller that I could easily spin up<\/li>\n<li>An easy way to set up the machine name and basic settings on said virtual machines<\/li>\n<li>A simple way to edit and pass data<\/li>\n<li>A way to keep the costs low, including software<\/li>\n<\/ul>\n<p>Ideally this solution, as I was thinking to myself, could even potentially leverage Windows Powershell Desired State Configuration in the long term. More importantly, I wanted something that could be simple enough that a person with minimal knowledge of Windows PowerShell could easily use to spin up machines.<\/p>\n<p>I will make some important notes. Although this solution is targeted to use the free tools, it works equally well in the full versions of Windows Server&nbsp;2012&nbsp;R2 and Windows&nbsp;8.1. It might work with Windows Server&nbsp;2012, but I haven&rsquo;t tested it yet. Theoretically, it should all work.<\/p>\n<p>So a few pieces came together. For software, I opted for the following free tools from Microsoft and the IT community.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"312\" valign=\"top\">\n<p><a href=\"http:\/\/technet.microsoft.com\/en-us\/evalcenter\/dn205299.aspx\" target=\"_blank\">Hyper-V Server 2012&nbsp;R2<\/a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<\/td>\n<td width=\"312\" valign=\"top\">\n<p>Free hosting environment for virtual machines from Microsoft<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"312\" valign=\"top\">\n<p><a href=\"http:\/\/technet.microsoft.com\/en-us\/evalcenter\/dn205286.aspx\" target=\"_blank\">Evaluation version of Windows Server&nbsp;2012&nbsp;R2<\/a><\/p>\n<\/td>\n<td width=\"312\" valign=\"top\">\n<p>180-day trial of Windows Server&nbsp;2012&nbsp;R2 as an operating system for the virtual machines<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"312\" valign=\"top\">\n<p><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Convert-WindowsImageps1-0fe23a8f\" target=\"_blank\">Convert-WindowsImage.ps1<\/a><\/p>\n<\/td>\n<td width=\"312\" valign=\"top\">\n<p>Free script written by a Microsoft partner, Pronichkin, to directly convert a WIM file to a bootable VHD. This is a great tool!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I&rsquo;m going to presume that you at least know how to setup Hyper-V. If you download the free version, you&rsquo;ll need to run <b>sconfig<\/b> to configure the environment. It is a really nice easy-to-use VBScript script that does all the nasty work. You&rsquo;ll also have to remember that it defaults to Cmd.exe, and you will have to launch Powershell.exe to execute all of your scripts.<\/p>\n<p>If you&rsquo;re curious about trying to get a Hyper-V environment initially configured, here&rsquo;s a series I wrote a while back on TechNet that will give you some tips: <a href=\"http:\/\/bitly.com\/bundles\/energizedtech\/5\" target=\"_blank\">Hyper-V and the Windows PowerShell Cmdlets<\/a>.<\/p>\n<p>So with the presumption you have a server running Hyper-V, our first task is to get all those goodies out of the WIM file. I want to avoid typing anything as much as possible. I would also like the VHD files that I create to reflect the version of the server.<\/p>\n<p>Within the latest Windows Server environments, there is a built-in cmdlet called <b>Mount-DiskImage<\/b>, which will allow us to mount either an ISO file or a VHD file directly from Windows PowerShell, and then access its contents.<\/p>\n<p style=\"margin-left:30px\">$ISOFile=&rdquo;C:\\ISO\\9600.16384.WINBLUE_RTM.130821-1623_X64FRE_SERVER_EVAL_EN-US-IRM_SSS_X64FREE_EN-US_DV5.ISO&quot;<\/p>\n<p style=\"margin-left:30px\">MOUNT-DISKIMAGE &ndash;imagepath $ISO<\/p>\n<p>This is all good. But it doesn&rsquo;t return the drive letter of the mounted VHD or ISO file. You can obtain this by using two other cmdlets, which we pipe together for this information:<\/p>\n<p style=\"margin-left:30px\">GET-DISKIMAGE &ndash;imagepath $ISO | GET-Volume<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3301.1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3301.1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If we need only the drive letter, we&rsquo;ll access the property directly and store it away:<\/p>\n<p style=\"margin-left:30px\">$DriveLetter=((GET-DISKIMAGE &ndash;imagepath $ISO | GET-Volume).DriveLetter)+&rdquo;:&rdquo;<\/p>\n<p>Now with the drive letter in a scriptable state, we need to get some information about the WIM file. We need to know how many images are in the WIM file and the descriptions for each image. We will leverage this information when launching the Convert-WindowsImage.ps1 file.<\/p>\n<p>To get this information, we can leverage the <b>Get-WindowsImage<\/b> cmdlet from the DISM module. We target the WIM file, and it will return a list of images, their index numbers, and descriptions for it:<\/p>\n<p style=\"margin-left:30px\">GET-WINDOWSIMAGE &ndash;ImagePath ($DriveLetter+&rdquo;\\sources\\install.wim&rdquo;)<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6355.2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6355.2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now we&rsquo;ll capture that information to make it useful by storing it away:<\/p>\n<p style=\"margin-left:30px\">$Images=GET-WINDOWSIMAGE &ndash;ImagePath ($DriveLetter+&rdquo;\\sources\\install.wim&rdquo;)<\/p>\n<p>This is where the fun begins. We&rsquo;re going to make some bootable VHDs. The <b>Convert-WindowsImage <\/b>command<b> <\/b>needs to be provided only three pieces of information to make it useful:<\/p>\n<ul>\n<li>Location of the WIM file<\/li>\n<li>Index or name of the edition we want to extract<\/li>\n<li>Name and location to assign to the VHD file<\/li>\n<\/ul>\n<p>A simple example of the script in action would be this:<\/p>\n<p style=\"margin-left:30px\">CONVERT-WindowsImage.PS1 &ndash;sourcepath install.wim &ndash;VHDPath c:\\vhds\\filename.vhd &ndash;edition ServerDataCenterCore<\/p>\n<p>Or if we&rsquo;re going to specify the index number (same version here too):<\/p>\n<p style=\"margin-left:30px\">CONVERT-WindowsImage.PS1 &ndash;sourcepath install.wim &ndash;VHDPath c:\\vhds\\filename.vhd &ndash;edition 3<\/p>\n<p>We already know the name and location of the WIM file. Now we start up a nice <b>FOR<\/b> loop, step through the images:<\/p>\n<p style=\"margin-left:30px\">FOREACH ($Image in $Images)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p>&#8230;and grab the index number:<\/p>\n<p style=\"margin-left:30px\">$Index=$Image.Index<\/p>\n<p>If only I could remove the spaces from the description and make that into a file name&hellip;<\/p>\n<p>Oh, right! I&rsquo;m in Windows PowerShell! I can! We&rsquo;ll grab the description first:<\/p>\n<p style=\"margin-left:30px\">$ImageDescription=$Image.ImageDescription<\/p>\n<p>&hellip;and then use the <b>Replace<\/b> method to remove the spaces:<\/p>\n<p style=\"margin-left:30px\">$ImageDescription=$ImageDescription.replace(&ldquo; &ldquo;,&rdquo;&rdquo;)<\/p>\n<p>Then add our destination location and file extension to our new name:<\/p>\n<p style=\"margin-left:30px\">$VHDName=&rdquo;C:\\ISO\\&rdquo;+$ImageDescription+&rdquo;.VHD&rdquo;<\/p>\n<p>And with one-fell swoop, launch the script to create a bootable VHD from a WIM file entry:<\/p>\n<p style=\"margin-left:30px\">C:\\ISO\\Convert-WindowsImage.PS1 &ndash;sourcepath &ldquo;$Driveletter\\Sources\\Install.wim&rdquo; &ndash;Edition $Index &ndash;VHDPath $VHDName<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Save this file into a folder. In this post, I am referencing C:\\ISO, which is where I wrote this environment from. Make that location and save it there for ease-of-use. I called my CREATE-VMTemplate.PS1.<\/p>\n<p>Place the Convert-WindowsImage.ps1 and CREATE-VMTemplate.PS1 in this folder and execute CREATE-VMTemplate.PS1:<\/p>\n<p style=\"margin-left:30px\">.\\CREATE-VMTemplate.PS1<\/p>\n<p>Watch&hellip;and in about 10 minutes (depending on the speed of your computer and hard disk), you&rsquo;ll have a lovely pile of VHDs that look like this:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3323.3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3323.3.png\" alt=\"Image of directory\" title=\"Image of directory\" \/><\/a><\/p>\n<p>Come back tomorrow when we go to the next step and make an easier way to configure the VHD before attaching it to a virtual machine.<\/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 an email to The Scripting Guys 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 remember eat your Cmdlets each and every day with a taste dash of Creativity.<\/p>\n<p><b>Sean Kearney<\/b>, Windows PowerShell MVP and Honorary Scripting Guy&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the freely available Convert-WindowsImage.ps1 to extract multiple bootable VHD files for Hyper-V Server&nbsp;2012&nbsp;R2. &nbsp;Hey, Scripting Guy! Is there an easier way to create a VHD file that already has the operating system, rather than installing it and clicking Next&hellip;Next&hellip;Next? I&rsquo;d like something that could spin up a simple machine like it was a [&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,154,45],"class_list":["post-2024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the freely available Convert-WindowsImage.ps1 to extract multiple bootable VHD files for Hyper-V Server&nbsp;2012&nbsp;R2. &nbsp;Hey, Scripting Guy! Is there an easier way to create a VHD file that already has the operating system, rather than installing it and clicking Next&hellip;Next&hellip;Next? I&rsquo;d like something that could spin up a simple machine like it was a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2024","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=2024"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2024\/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=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}