<#
	.Synopsis
		Script for Windows 10 Deployment
	
	.DESCRIPTION
		Script for Windows 10 Deployment
	
	.PARAMETER WimFilePath
		A description of the WimFilePath parameter.
	
	.PARAMETER Architecture
		A description of the Architecture parameter.
	
	.PARAMETER FileName
		A description of the FileName parameter.
	
	.PARAMETER Langage
		A description of the Langage parameter.
	
	.EXAMPLE
		C:\Setup\Scripts\Set-WinpeFix.ps1 -wimFilePath c:\winpe\media\sources -Architecture amd64 -FileName boot
	
	.EXAMPLE
		C:\Setup\Scripts\Set-WinpeFix.ps1 -wimFilePath c:\winpe\media\sources -Architecture amd64 -FileName winpe

	.NOTES
		Created:	 2016-02-1
		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 authors or Deployment Artist.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
	[Parameter(Mandatory = $true,
			   ValueFromPipelineByPropertyName = $true,
			   Position = 1)]
	[ValidateNotNullOrEmpty()]
	$WimFilePath,
	[Parameter(Mandatory = $true,
			   ValueFromPipelineByPropertyName = $true,
			   Position = 0)]
	[ValidateNotNullOrEmpty()]
	[ValidateSet('x86', 'amd64')]
	$Architecture,
	[Parameter(Mandatory = $true,
			   Position = 2)]
	[ValidateSet('winpe', 'boot')]
	$FileName
)

# 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

$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 + "\$Architecture\WinPE_OCs"
$DISM_Path = $ADK_Path + "\Deployment Tools" + "\$Architecture\DISM"
$DISM_Path2 = $ADK_Path +"\Deployment Tools" +"\$Architecture\Oscdimg"
$ADK_Path_Media =$ADK_Path + $WinPE_ADK_Path + "\$Architecture\Media"
$ADK_Deploy = $ADK_Path + "\Deployment Tools"


#endregion

$WinPE_BuildFolder = $WimFilePath
$WinPE_MountFolder = $WimFilePath+"\"+"Mount"

if (!(Test-Path -path $WinPE_BuildFolder)) { break }

if (!(Test-Path -path $WinPE_MountFolder)) {New-Item $WinPE_MountFolder -Type Directory| Out-Null}
$WimFile = "$WinPE_BuildFolder\$Filename.wim"
Mount-WindowsImage -ImagePath $WimFile -Path $WinPE_MountFolder -Index 1;

& icacls $WinPE_MountFolder\Windows\System32\schema.dat /save "$env:TEMP\AclFile"
& takeown /F $WinPE_MountFolder\Windows\System32\schema.dat /A
& icacls $WinPE_MountFolder\Windows\System32\schema.dat /grant "BUILTIN\Administrateurs:(F)"
if ($Architecture -eq "amd64"){
Write-Host "Architecture $Architecture" -ForegroundColor Green
Copy-Item .\schema-x64.dat $WinPE_MountFolder\Windows\System32\schema.dat -Force
}
if ($Architecture -eq "x86"){
	Write-Host "Architecture $Architecture" -ForegroundColor Green
Copy-Item .\schema-x86.dat $WinPE_MountFolder\Windows\System32\schema.dat -Force
}
& icacls $WinPE_MountFolder\Windows\System32\schema.dat /setowner "NT SERVICE\TrustedInstaller"
& icacls $WinPE_MountFolder\Windows\System32\ /restore "$env:TEMP\AclFile"

Dismount-WindowsImage -Path $WinPE_MountFolder -Save
Remove-Item $WinPE_MountFolder -Force | Out-Null
Start-Sleep 2 
Export-WindowsImage -SourceImagePath "$WinPE_BuildFolder\$FileName.wim" -SourceIndex 1 -DestinationImagePath "$WinPE_BuildFolder\$FileName`2.wim"
Remove-Item "$WinPE_BuildFolder\$FileName.wim"
Rename-Item -Path "$WinPE_BuildFolder\$FileName`2.wim" -NewName "$WinPE_BuildFolder\$FileName.wim"

