2

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

3 Answers 3

1

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.

4
  • How do I do them separately? Im still a bit confused at what a VHD is....is it shared by ALL VM's or just one? and how are they linked? EIther way, do I literally just move the unattended.xml onto the VHD? I guess Im a bit confused at what I should be changing on my script. Also do I mount the VHD to a specific VM.....or just to a Computer im keeping the VM's on through Hyper V?
    – user57862
    Mar 7, 2013 at 17:55
  • 2
    A VHD is a file that contains the data that will be perceived by the VM as a disk. They are not shared between your VMs. Each VM has its own. (There are ways of sharing a base image between multiple VMs, but that's a more advanced topic.) You can mount the VHD file as a disk on any machine, whether it's a VM or not. The example I gave was assuming that you would mount it on the machine running the script, at least temporarily. I've also added some detail to the answer. Mar 8, 2013 at 21:32
  • Awesome, thanks for the info! Where would I put the actual unattend file though?
    – user57862
    Mar 8, 2013 at 21:37
  • 1
    You'd format the disk presented by the VHD, probably with NTFS. And then you'd put the unattend file at the root of that file system. Mar 9, 2013 at 23:05
0

The answer I used to this question is a script I found that would create ISO files. When I would attach a VHD with the unattend.xml to the VM, it wouldn't use it. When I add a 2nd DVD to the VM, it takes it just fine.

The idea for the project I'm working on is to start with a bare metal box, and a flash drive w/ Hyper-V 2012, and Win2012, my script, and a config.xml.

The script takes the VM config out of the XML, builds the first one, inserts firstlogon commands based on hostname, and then moves on to the next VM. I'll be releasing it on TechNet once I'm done with it, but the ISO creation script isn't mine. If you look on TechNet under my name, I've linked it there.

-1

You can manually create new floppy drive on installed virtual machine, it will appear as disk A: inside VM. Than you can login into virtual machine, format disk A: and place your unattended.xml there, in the root folder. After this you can simply detach virtual floppy.

Later, you can insert this floppy to newly created VM using Set-VMFloppyDiskDrive commandlet and ISO with OS. OS will pick configuration from floppy automatically.

You must log in to answer this question.