Adding Unattended.xml Files to Hyper-V Powershell Creation

hyper-vpowershell-v3.0unattended

As of right now I have a Powershell 3.0 Script Provisioning and Creating Servers. What I want to do is add an Unattended.xml to take it a step further (So I wouldn't have to get on and click next/choose disk/etc..)+ for the windows installation, How would I go about doing this?

The Script Works Fine, I just want to be able to automate the entire thing. I can read up on unattended.xml files but im mainly concerned at HOW to insert it into this script, I've read about attaching a floppy with an unattended.xml, but im not entirely sure.

#Variables
$VHDBool = 1 #Bool asking for a Non-VHD Linked VM

#Ask for Name of VM
$VMNAME = Read-Host "Enter the Name of the Server You would Like to Set up: "

#Ask for Memory Requirements of VM
[int64]$VMMEMpre = Read-Host "Ok now Enter the Memory in GB you would like to allocate: (Press [Enter] for 512MB)"
if ($VMMEMpre -eq ""){$VMMEM=512MB} ; if ($VMMEMpre -eq $NULL) {$VMMEM=512MB}
[int64]$MEMALL = 1073741824
[int64]$VMMEM = $VMMEMpre * $MEMALL

#Ask for Boot Device for VM, Should be left to CD for ISO's
$VMBOOT = Read-Host "Select a Boot Device (CD, Floppy, LegacyNetworkAdapter, and IDE), Default is CD: "
if ($VMBOOT -eq ""){$VMBOOT="CD"} ; if ($VMBOOT -eq $NULL){$VMBOOT="CD"}

#Ask for path to VM File Storage
$VMSTO = Read-Host "Enter Directory to Store new Files: (c:\storage for example, Default is c:\VMfiles)"
if ($VMSTO -eq ""){$VMSTO="c:\VMfiles"} ; if ($VMSTO -eq $NULL){$VMSTO="c:\VMfiles"}

#Ask For path to store VHDX File, or NO to not link one
$VHDXLOC = Read-Host "Enter the Path to Store your VHDX File: (c:\base.vhdx for example or enter NO for NO VHD)"
if ($VHDXLOC -eq "NO") {$VHDBOOL = 0}

#Ask for HD Space (If applicable) for VHD
if ($VHDXLOC -ne "NO"){
[int64]$VMHDpre = Read-Host "Enter The Amount of Hard Disk Space you would like allocated to the VM: (Default is 40GB)"
if ($VMHDpre -eq ""){$VMHD=40GB} ; if ($VMHDpre -eq $NULL) {$VMHD=40GB}
}
[int64]$VMHD = $VMHDpre * $MEMALL

#Creates VM (With VHDX)
if ($VHDBOOL -eq 1){
New-VM -Name $VMNAME -MemoryStartupBytes $VMMEM -BootDevice $VMBOOT -path $VMSTO -NewVHDSizeBytes $VMHD –NewVHDPath $VHDXLOC}
#Creates VM (Without VHDX)
if ($VHDBOOL -eq 0){
New-VM -Name $VMNAME -MemoryStartupBytes $VMMEM -BootDevice $VMBOOT -path $VMSTO –NoVHD}

#Ask for Location of ISO
$ISOLOC = Read-Host "Enter the Location of the ISO you would Like to Install: (c:\ISOFILE\Windows_NAME.iso)"

#Installs ISO Via Location
Set-VMDvdDrive -VMName $VMNAME -Path $ISOLOC

#Start the Server
Start-VM -Name $VMNAME

Best Answer

I think it would be simplest to create the VHD and the VM separately. Then, before you boot the VM, mount the VHD and place your unattend.xml file on it. (Mount-vhd is the cmdlet you want.) Then dismount the VHD and attach it to the VM as the last part of VM creation.

You can do the same with a virtual floppy image, but it's a little more complicated since there's no local-mount option for that and because it just adds to the number of virtual disk images you're dealing with.

After you've put your unattend file on the VHD, then you can dismount the VHD with the "dismount-vhd" cmdlet. Then attach it to the VM you created with the "add-vmharddiskdrive" cmdlet.