<#
	.Synopsis
		Script for Windows 10 Deployment
	
	.DESCRIPTION
		Script for Windows 10 Deployment
	
	.PARAMETER WinPEArchitecture
		A description of the WinPEArchitecture parameter.
	
	.PARAMETER Winpe_Langage
		A description of the Winpe_Langage parameter.
	
	.PARAMETER ADDPackages
		A description of the ADDPackages parameter.
	
	.PARAMETER CreateISO
		A description of the CreateISO parameter.
	
	.PARAMETER Drivers
		A description of the Drivers parameter.
	
	.EXAMPLE
		C:\Setup\Scripts\New-Winpe.ps1 -Architecture amd64 -Langage fr-fr
	
	.EXAMPLE
		C:\Setup\Scripts\New-Winpe.ps1 -Architecture amd64 -Langage fr-fr -CreateISO YES
	
	.EXAMPLE
		C:\Setup\Scripts\New-Winpe.ps1 -Architecture amd64 -Langage fr-fr -CreateISO YES -Drivers YES
	
	.NOTES
		Created:	 2016-02-26
		Version:	 1.0
		
		Author - Jérôme Bezet-Torres
		Twitter: @JM2K69
		
		Disclaimer:
		This script is provided "AS IS" with no warranties, confers no rights and
		is not supported by the author.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
	[Parameter(Mandatory = $true,
			   ValueFromPipelineByPropertyName = $true,
			   Position = 0)]
	[ValidateNotNullOrEmpty()]
	[ValidateSet('x86', 'amd64')]
	$WinPEArchitecture,
	[Parameter(Mandatory = $true)]
	[ValidateSet('fr-fr', 'es-es', 'it-it')]
	$Winpe_Langage,
	[ValidateSet('YES')]
	$ADDPackages,
	[Parameter(Mandatory = $true)]
	[ValidateSet('YES', 'NO')]
	$CreateISO,
	[ValidateSet('YES')]
	$Drivers
)

# Check for elevation
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "Oupps, you need to run this script from an elevated PowerShell prompt!`nPlease start the PowerShell prompt as an Administrator and re-run the script."
	Write-Warning "Aborting script..."
    Throw
}

#region variables

$WinPE_Final_ISO = "c:\WinPE"
$WinPE_BuildFolder = "c:\WinPE\WinPE"+"_"+"$WinPEArchitecture"
$WinPE_BuildFolder1 = $WinPE_BuildFolder+"\fwfiles"
$WinPE_BuildFolder2 = $WinPE_BuildFolder + "\media"
$WinPE_BuildFolder3= $WinPE_BuildFolder2+"\sources"
$WinPE_Drivers = $WinPE_BuildFolder+"\Drivers"
$WinPE_MountFolder = $WinPE_BuildFolder +"\Mount"
$ADK_Path = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit"
$WinPE_ADK_Path = $ADK_Path + "\Windows Preinstallation Environment"
$WinPE_OCs_Path = $WinPE_ADK_Path + "\$WinPEArchitecture\WinPE_OCs"
$DISM_Path = $ADK_Path + "\Deployment Tools" + "\$WinPEArchitecture\DISM"
$DISM_Path2 = $ADK_Path + "\Deployment Tools" + "\$WinPEArchitecture\Oscdimg"
$ADK_Path_Media = $ADK_Path + $WinPE_ADK_Path + "\$WinPEArchitecture\Media"
$ADK_Deploy = $ADK_Path + "\Deployment Tools"
$Winpe_Name_Iso = "WinPe5_" + "$WinPEArchitecture" + ".iso"


#endregion

# Delete existing WinPE build folder (if exist)
try
{
	if (Test-Path -path $WinPE_BuildFolder) { Remove-Item -Path $WinPE_BuildFolder -Recurse -ErrorAction Stop }
}
catch
{
	Write-Warning "Oupps, Error: $($_.Exception.Message)"
	Write-Warning "Most common reason is existing WIM still mounted, use ImageX /unmount to clean up and run script again"
	Break
}

#Création des répertoires pour le WinPE
if (!(Test-Path -path $WinPE_BuildFolder)) { New-Item $WinPE_BuildFolder -Type Directory | Out-Null }
if (!(Test-Path -path $WinPE_BuildFolder1)) { New-Item $WinPE_BuildFolder1 -Type Directory | Out-Null }

# on copy l'ensemble des fichiers necessaires 
Copy-Item "$WinPE_ADK_Path\$WinPEArchitecture\Media" -Recurse $WinPE_BuildFolder
Copy-Item "$DISM_Path2\efisys_noprompt.bin" $WinPE_BuildFolder1
Copy-Item "$DISM_Path2\etfsboot.com" $WinPE_BuildFolder1

# Make a copy of the WinPE 5.0 boot image from ADK 8.1
if (!(Test-Path -path $WinPE_BuildFolder3)) { New-Item $WinPE_BuildFolder3 -Type Directory | Out-Null }
Copy-Item "$WinPE_ADK_Path\$WinPEArchitecture\en-us\winpe.wim" "$WinPE_BuildFolder3"
Rename-Item "$WinPE_BuildFolder3\winpe.wim" -NewName "boot.wim"

# Mount the WinPE image
if (!(Test-Path -path $WinPE_MountFolder)) { New-Item $WinPE_MountFolder -Type Directory | Out-Null }
$WimFile = "$WinPE_BuildFolder3\boot.wim"
Mount-WindowsImage -ImagePath $WimFile -Path $WinPE_MountFolder -Index 1;

write-host "#################################################################" -ForegroundColor yellow
Write-Host "             New WinPe Default Langage is set to $Winpe_Langage                        "-ForegroundColor Yellow
write-host "#################################################################"-ForegroundColor Yellow 

& $DISM_Path\dism.exe /Image:$WinPE_MountFolder /Add-Package /PackagePath:$WinPE_OCs_Path\$Winpe_Langage\lp.cab
& $DISM_Path\dism.exe /Set-AllIntl:$Winpe_Langage /Image:$WinPE_MountFolder

if ($ADDPackages -eq 'YES')
{
	write-host ""
	write-host "#################################################################" -ForegroundColor yellow
	Write-Host "                 Add Packages for WinPe                        " -ForegroundColor Yellow
	write-host "#################################################################" -ForegroundColor Yellow
	
	"HTA", "Scripting", "WMI", "Dot3Svc", "NetFx", "PowerShell", "DismCmdlets", "MDAC" | ForEach-Object {
		write-host "Add Package $_" -ForegroundColor Yellow
		
		& $DISM_Path\dism.exe /Image:$WinPE_MountFolder /Add-Package /PackagePath:$WinPE_OCs_Path\winpe-$_.cab
		
	}
	
	"HTA", "Scripting", "WMI", "Dot3Svc", "NetFx", "PowerShell", "DismCmdlets", "MDAC" | ForEach-Object {
		$package = "winpe-" + $_ + "_" + $Winpe_Langage + ".cab"
		write-host "Add Package $package" -ForegroundColor Yellow
		
		& $DISM_Path\dism.exe /Image:$WinPE_MountFolder /Add-Package /PackagePath:$WinPE_OCs_Path\$Winpe_Langage\$package
		
	}
}

if ($Drivers-eq 'YES')
{
	if (!(Test-Path -path $WinPE_Drivers)) { New-Item $WinPE_Drivers -Type Directory | Out-Null }
	
	write-host "#################################################################" -ForegroundColor yellow
	Write-Host "Copy all drivers in $WinPE_Drivers folder" -ForegroundColor Yellow
	write-host "#################################################################" -ForegroundColor yellow
	
	pause
	& $DISM_Path\dism.exe /Image:$WinPE_MountFolder /Add-Driver /Driver:$WinPE_Drivers /Recurse
}

Dismount-WindowsImage -Path $WinPE_MountFolder -Save
Start-Sleep 3 
Export-WindowsImage -SourceImagePath "$WinPE_BuildFolder3\boot.wim" -SourceIndex 1 -DestinationImagePath "$WinPE_BuildFolder3\boot2.wim"
Remove-Item "$WinPE_BuildFolder3\boot.wim"
Rename-Item -Path "$WinPE_BuildFolder3\boot2.wim" -NewName "$WinPE_BuildFolder3\boot.wim"

if ($CreateISO -eq 'YES')
{
	#create Image ISO for UEFI and Bios
	$Here = Get-Location
	Set-Location $DISM_Path2
	$BOOTDATA = "2#p0,e,b" + "$WinPE_BuildFolder1\etfsboot.com" + "#pEF,e,b" + "$WinPE_BuildFolder1\efisys_noprompt.bin"
	$ArgumentList = "-m" + " -o" + " -u2" + " -udfver102 " + "-bootdata:$BOOTDATA", " $WinPE_BuildFolder2" + " $WinPE_Final_ISO\$Winpe_Name_Iso"
	start-Process ".\oscdimg.exe" -ArgumentList "$ArgumentList"
	Set-Location $Here
}