Saturday 11 February 2012

Create VHD (PowerShell version)

In an attempt to learn PowerShell I've updated the CreateVHD script. It should now be saved as CreateVHD.ps1. The same parameters as before are required:

CreateVHD.ps1 {VHD Name} {Size in GB} {Configure BootFromVHD : YES}

A new feature of this version is that it creates a batch file to remove the GUID entry from the boot process using BCDEDIT. The new file will be named Delete - {VHD Name}.cmd.

See previous post for more information: http://matt-gelder.blogspot.com/2012/02/script-to-create-vhd-populate-it-from.html
_________________________________________________________________________

#Define Parameters Section
#-------------------------
Param
(
[String]$VDisk,
[INT16]$DiskSizeGB,
[String]$BootFromVHD
)
#Variables section
#-----------------
$WimFile = "E:\Sources\install.wim"
$ImageIndex = 1
$TargetDrive = "C:"
$VHDFolder = "\VMs"
#Verify parameters meet conditions
#---------------------------------
if
($VDisk -eq "")
{"You need to specify a name for the Virtual Disk file {Parameter 1}"
EXIT
}
ELSE
{
$VDiskFP = "$TargetDrive$VHDFolder\$VDisk.vhd"
"Virtual Hard Disk Name: $VDiskFP"}
if
($DiskSizeGB -eq "")
{"You need to specify a disk size in GB {Parameter 2}"
EXIT
}
ELSE
{
$DiskSizeMB = $DiskSizeGB * 1024
"Disk size is $($DiskSizeMB)MB"
}
if
($BootFromVHD -eq "")
{"Boot from VHD will not be enabled"}
ELSEIF
($BootFromVHD -eq "YES")
{"Boot from VHD will be enabled"}
ELSE
{"Boot From VHD if specified must be set to Yes {Parameter 3}"
EXIT
}
if
((Test-Path -path $VDiskFP) -eq $True)
{
"Error: $VDiskFP already exists"
EXIT
}
#Create the Diskpart-create.txt file
#-----------------------------------
$File = New-Item -type file "Diskpart-create.txt" -Force
add-content $file "create vdisk file=$VDiskFP maximum=$DiskSizeMB type=expandable"
add-content $file "select vdisk file=$VDiskFP"
add-content $file "attach vdisk"
add-content $file "create partition primary"
add-content $file "format quick FS=NTFS label=$VDiskFP"
add-content $file "assign letter=v"
add-content $file "active"
diskpart /s Diskpart-create.txt
del diskpart-create.txt
#ImageX
#------
"Applying Windows image No. $ImageIndex from $WimFile to $VDiskFP"
.\imagex /apply $WimFile $ImageIndex V:
bcdboot V:\Windows /s V:

#Create the Diskpart-detach.txt file
#-----------------------------------
$File = New-Item -type file "Diskpart-detach.txt" -Force
add-content $file "select vdisk file=$VDiskFP"
add-content $file "detach vdisk"
diskpart /s Diskpart-detach.txt
del diskpart-detach.txt
#Create the Boot From VHD Menu Option
#------------------------------------
IF
($BootFromVHD -eq "YES")
{
$VdiskBCD = ('"{0}"' -f $VDisk)
$VdiskBCD = " bcdedit /copy {current} /d $VdiskBCD "
$VdiskBCD = ('"{0}"' -f $VDiskBCD)
$VdiskBCD = "cmd /c $VdiskBCD > GUID.txt"
$File = New-Item -type file "RunBCD.cmd" -Force
add-content $file "@echo off"
add-content $file "$VdiskBCD"
.\RunBCD.cmd
del .\RunBCD.cmd
$GUID = get-content .\GUID.txt
$GUID = $GUID -split "to ", 2
$GUID = $GUID -split ".", 0, "simplematch"
$GUID = $GUID[1]
del .\GUID.txt
cmd /c "bcdedit /set $GUID device vhd=[locate]$VHDFolder\$Vdisk.vhd"
cmd /c "bcdedit /set $GUID osdevice vhd=[locate]$VHDFolder\$Vdisk.vhd"
cmd /c "bcdedit /set $GUID detecthal on"
cmd /c "bcdedit /default $GUID"

#Create a batch file to remove the GUID from the boot config
$File = New-Item -type file "Delete - $Vdisk.cmd" -Force
$DeleteGUID = "bcdedit /delete $GUID"
$DeleteGUID = ('"{0}"' -f $DeleteGUID)
$DeleteGUID = "cmd /c $DeleteGUID"
add-content $file "$DeleteGUID"
}

No comments:

Post a Comment