{"id":2015,"date":"2014-02-12T00:01:00","date_gmt":"2014-02-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/02\/12\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-3\/"},"modified":"2014-02-12T00:01:00","modified_gmt":"2014-02-12T00:01:00","slug":"set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-3\/","title":{"rendered":"Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 3"},"content":{"rendered":"<p><b>Summary<\/b>: Copy a VHD file template and inject an Unattend.xml file in preparation for attachment to a VHD file.\n<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!\nYesterday you were showing us how to edit the Unattend.XML file. How can we easily inject that into a virtual machine and maybe turn that into a virtual machine?\n&nbsp;&mdash;RD\n<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 RD,\nNever fear. Honorary Scripting Guy, Sean Kearney, is here to help you save the day with more Windows PowerShell!\n<strong>&nbsp; &nbsp; &nbsp;Note<\/strong> &nbsp;Today&#8217;s post is the third in a five-part series. To catch up, read:&nbsp;<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/10\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-1.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/11\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-2.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 2<\/a><\/li>\n<\/ul>\n<p>If you&rsquo;ve played around with Windows&nbsp;8.1 you have noticed that you can double-click an ISO or VHD file, and it will automatically mount as a virtual drive in Windows Explorer! The first time I saw that, I thought it was amazingly cool.\nOur first challenge with this VHD file is to get the Unattend.XML file sitting in the WindowsSystem32Sysprep folder.\nActually I fibbed a bit. The FIRST thing is to make a copy of one of the VHD files we just created as the basis for our virtual machine. So I need some minor criteria for the new name of the VHD:<\/p>\n<ul>\n<li>I want TemplateVHD as part of the file name.<\/li>\n<li>I am going to use the VMName as part of the VHD name.<\/li>\n<li>I will attach the drive letter to the end.<\/li>\n<li>I&rsquo;m going to grab the default location of the virtual machines from Hyper-V so I can drop the new VHDs into the proper place.<\/li>\n<\/ul>\n<p>Let&rsquo;s set up the first parameters we&rsquo;ll need:<\/p>\n<p style=\"margin-left:30px\">Param(<\/p>\n<p style=\"margin-left:30px\"># Virtual Machine Name<\/p>\n<p style=\"margin-left:30px\">$VMname,<\/p>\n<p style=\"margin-left:30px\"># Explicit name of the Server Version<\/p>\n<p style=\"margin-left:30px\">$Edition=&rdquo;WindowsServer2012R2StandardServer&rdquo;,<\/p>\n<p style=\"margin-left:30px\"># Source folder for the Virtual Machine Templates<\/p>\n<p style=\"margin-left:30px\">$Source=&rdquo;C:ISO&rdquo;<\/p>\n<p style=\"margin-left:30px\">)\nWe also need to obtain the default folder location for virtual hard disks in Hyper-V. This is stored in the registry of the computer.<\/p>\n<p style=\"margin-left:30px\">$VHDPath=(Get-ItemProperty -Path &#8216;HKLM:SoftwareMicrosoftWindows NTCurrentVersionVirtualization&#8217; -Name &#8216;DefaultVirtualHardDiskPath&#8217;).DefaultVirtualHardDiskPath\nNow we&rsquo;re into building some simple file names for a backup with the <b>Copy-Item<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">$SourceData=$SourcePath+&#8221;&#8221;+$Edition+&#8221;.vhd&#8221;<\/p>\n<p style=\"margin-left:30px\">$TargetData=$VHDPath+$VMName+&#8221;-&#8220;+$Edition+&#8221;-c.vhd&#8221;<\/p>\n<p style=\"margin-left:30px\">COPY-ITEM $SourceData $TargetData\nAfter we copy the file into a new folder, we can mount our new VHD file and inject the Unattend.xml file into it. For this to work, we need to get the drive letter of the mounted VHD file. This uses the same cmdlet we used to obtain the drive letter for the ISO file&mdash;we just pipe it to two different cmdlets.\nFirst we mount the new VHD file:<\/p>\n<p style=\"margin-left:30px\">MOUNT-DISKIMAGE $TargetData\nNext obtain the drive letter by using <b>Get-DiskImage<\/b> on the mounted VHD file, but pipe the output into <b>Get-Disk<\/b> and then to <b>Get-Partition<\/b>:<\/p>\n<p style=\"margin-left:30px\">$DriveLetter=((Get-DiskImage $TargetData | get-disk | get-partition).DriveLetter)+&#8221;:&#8221;\nThen we build out the target folder for our Unattend.xml file to drop into. Remember that you&rsquo;re dealing with the mounted VHD and its Windows folder structure:<\/p>\n<p style=\"margin-left:30px\">$Destination=$Driveletter+&#8221;WindowsSystem32Sysprepunattend.xml&#8221;\nAfter we complete our work, we copy the file and dismount the VHD file:<\/p>\n<p style=\"margin-left:30px\">COPY-ITEM $UnattendXML $Destination<\/p>\n<p style=\"margin-left:30px\">DISMOUNT-DISKIMAGE $TargetData\nAt this point, I can populate this data to <b>New-VM<\/b>, and I&rsquo;m done, right?\nWell sure, I could stop there. But then again, you wouldn&rsquo;t get to see how we can go farther. Maybe go to the next step: populate some automatically starting scripts. Or maybe if you&rsquo;re really good and eat all your Scriptos cereal, we&rsquo;ll spin all this into a brand new shiny domain controller for the lab!\nInterested? Eyes ablaze? Stay tuned for tomorrow&rsquo;s thrill-packed episode of the Hey, Scripting Guys! Blog.\nI 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=\"http:\/\/blogs.technet.commailto: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 tasty dash of Creativity.\n<b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Copy a VHD file template and inject an Unattend.xml file in preparation for attachment to a VHD file. &nbsp;Hey, Scripting Guy! Yesterday you were showing us how to edit the Unattend.XML file. How can we easily inject that into a virtual machine and maybe turn that into a virtual machine? &nbsp;&mdash;RD &nbsp;Hello RD, Never [&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-2015","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: Copy a VHD file template and inject an Unattend.xml file in preparation for attachment to a VHD file. &nbsp;Hey, Scripting Guy! Yesterday you were showing us how to edit the Unattend.XML file. How can we easily inject that into a virtual machine and maybe turn that into a virtual machine? &nbsp;&mdash;RD &nbsp;Hello RD, Never [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2015","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=2015"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2015\/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=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}