#Ajout d'une entrée windows 2008

$cheminvhd="d:\disquevhd\"
$disquebase=$cheminvhd + "win2008base.vhd"

$fin="`nAppuyer sur Entrée pour terminer ... "

if (-not(Test-Path $disquebase)){
    Read-host "Le disque de base $disquebase n'existe pas, $fin"
    break
}

$description=Read-Host -Prompt "Description de la nouvelle entrée windows 2008 "
if ($description -eq "") {
    Read-host "La description est requise, $fin"
    break
}

$i=0
$disquediff=$cheminvhd + "win2008diff"+$i+".vhd"
while (Test-Path $disquediff){
    $i++
    $disquediff=$cheminvhd + "win2008diff"+$i+".vhd"
}

set-content -path "createvhddiff.ps1" -value "create vdisk file=$disquediff parent=$disquebase" -Force
[string]$ret=diskpart /s createvhddiff.ps1 
if (-not($ret.Contains("DiskPart a correctement créé le fichier de disque virtuel."))) {
    Read-host "$ret, $fin"
    break
}

$vhdvalue = "[locate]$($disquediff.Substring(2))"
$rx = [regex] "\{\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}"
$ret = bcdedit /copy '{current}' /d $description
$m = $rx.match($ret)
if (-not ($m.success)) {
	Read-host "$ret, $fin"
    break
}
else {
	$guid = $m.value
	$ret = bcdedit /set $guid device vhd=$vhdvalue
	if ($ret -ne "L'opération a réussi."){
		Read-host "$ret, $fin"
        break
    }
	$ret = bcdedit /set $guid osdevice vhd=$vhdvalue 
	if ($ret -ne "L'opération a réussi."){
		Read-host "$ret, $fin"
        break
	}
	$ret = bcdedit /set $guid detecthal on 
	if ($ret -ne "L'opération a réussi."){
		Read-host "$ret, $fin"
        break
	}
	Read-host "$description ($(split-path -leaf $disquediff)) VHD Boot ajouté, $fin"
}






