Learn about Windows PowerShell
Summary: Copy a VHD file template and inject an Unattend.xml file in preparation for attachment to a VHD file.
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?
Never fear. Honorary Scripting Guy, Sean Kearney, is here to help you save the day with more Windows PowerShell!
Note Today's post is the third in a five-part series. To catch up, read:
If you’ve played around with Windows 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.
Our first challenge with this VHD file is to get the Unattend.XML file sitting in the \Windows\System32\Sysprep folder.
Actually 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:
Let’s set up the first parameters we’ll need:
# Virtual Machine Name
# Explicit name of the Server Version
# Source folder for the Virtual Machine Templates
We also need to obtain the default folder location for virtual hard disks in Hyper-V. This is stored in the registry of the computer.
$VHDPath=(Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Virtualization' -Name 'DefaultVirtualHardDiskPath').DefaultVirtualHardDiskPath
Now we’re into building some simple file names for a backup with the Copy-Item cmdlet:
COPY-ITEM $SourceData $TargetData
After 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—we just pipe it to two different cmdlets.
First we mount the new VHD file:
Next obtain the drive letter by using Get-DiskImage on the mounted VHD file, but pipe the output into Get-Disk and then to Get-Partition:
$DriveLetter=((Get-DiskImage $TargetData | get-disk | get-partition).DriveLetter)+":"
Then we build out the target folder for our Unattend.xml file to drop into. Remember that you’re dealing with the mounted VHD and its Windows folder structure:
After we complete our work, we copy the file and dismount the VHD file:
COPY-ITEM $UnattendXML $Destination
At this point, I can populate this data to New-VM, and I’m done, right?
Well sure, I could stop there. But then again, you wouldn’t get to see how we can go farther. Maybe go to the next step: populate some automatically starting scripts. Or maybe if you’re really good and eat all your Scriptos cereal, we’ll spin all this into a brand new shiny domain controller for the lab!
Interested? Eyes ablaze? Stay tuned for tomorrow’s thrill-packed episode of the Hey, Scripting Guys! Blog.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then remember eat your Cmdlets each and every day with a tasty dash of Creativity.
Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy
Love the blog and thanks for the post, but you need to fix the links to the previous 2 articles. They currently link to the post editor.
Any pointers on how I can get some of that Scriptos cereal?
@ben Repairs on the links are in the works shobah_ar IMPORT-MODULE CEREAL GET-CONTENT -cereal 'Scriptos' | SEND-MOUTH Sean :)
Those were really great tips.. can anyone guide me about any lab information management software? currently i am navigating through a software offered by khemia software, here's a bit info about it (http://khemiasoftware.com/) please if anybody can guide me to the right way i am confused what to do.
Hey jack i think its http://khemia.com/ me too using it and its working great :)