<#
Setup_w10.ps1

Build WinPE 10

http://en.community.dell.com/techcenter/enterprise-client/w/wiki/2065.dell-command-deploy-driver-packs-for-enterprise-client-os-deployment#winpe
http://technet.microsoft.com/en-us/library/hh825144.aspx
http://technet.microsoft.com/en-us/library/hh824926.aspx

ISO: makewinpemedia /iso c:\winpe_amd64 c:\winpe_amd64\winpe.iso
USB: makewinpemedia /ufd /f c:\winpe_amd64 <drive letter>:

Sailer, Adam
2017.11.28
#>


$invoke = split-path -path $myInvocation.myCommand.Path -parent
$os = gcim win32_OperatingSystem
$proc = gcim win32_Processor


$arch = if ($proc.AddressWidth -eq 64) { 'amd64' } else { 'x86' }


$dest = "$env:SystemDrive\winpe_$arch"
$mount = "$dest\mount"
$image = "$dest\media\sources\boot.wim"
$label = "WinPE 10 $arch"


$unattend = @"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="windowsPE">
<component name="Microsoft-Windows-Setup" processorArchitecture="$arch" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Display>
<ColorDepth>32</ColorDepth>
<HorizontalResolution>1440</HorizontalResolution>
<RefreshRate>60</RefreshRate>
<VerticalResolution>900</VerticalResolution>
</Display>
</component>
</settings>
</unattend>
"@


Function Prep
{
write-host "`n`n@@ Called Prep" -fore magenta


$path = if ($proc.AddressWidth -eq 64) { ${env:ProgramFiles(x86)} } else { $env:ProgramFiles }

$map = @{
'KitsRoot' = "$path\Windows Kits\10\";
'OSCDImgRoot' = "$path\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$arch\Oscdimg";
'WinPERoot' = "$path\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment";
}

$array = $env:path.Split(';')

$map.keys | sort-object | % { $key = $_
$array += $map.$key; [System.Environment]::SetEnvironmentVariable($key, $map.$key, "process")
}

$array = $array | sort-object -unique; $array
$path = [string]::join(';', $array)
[System.Environment]::SetEnvironmentVariable('path', $path, 'process')
}


Function CopySource
{
write-host "`n`n@@ Called CopySource" -fore magenta

remove-item $dest -recurse -force -ea silentlyContinue
copype $arch $dest
}


Function MountCurrent
{
write-host "`n`n@@ Called MountCurrent" -fore magenta

mount-windowsImage -imagePath $image -index 1 -path $mount -verbose
}


Function DismountCurrent
{
write-host "`n`n@@ Called DismountCurrent" -fore magenta

if (get-windowsImage -mounted)
{ dismount-windowsImage -path $mount -save }
}


Function Packages
{
write-host "`n`n@@ called Packages" -fore magenta

$lang = 'en-us'

## order matters! http://technet.microsoft.com/en-us/library/hh824926.aspx

$order = @(
'winpe-wmi'
,'winpe-netfx'
,'winpe-scripting'
,'winpe-powershell'
,'winpe-dismcmdlets'
,'winpe-securebootcmdlets'
,'winpe-storagewmi'
,'winpe-securestartup'
,'winpe-hta'
,'winpe-fmapi'
)

$order | % { $item = $_

dir $env:WinPeRoot\$arch -recurse -include "$($_).cab", "$($_)_$($lang).cab" | sort-object -descending | % {

write-host "`n$($_.Name)" -fore cyan; add-windowsPackage -path $mount -packagePath "$($_.FullName)"
}
}
}


Function Drivers
{
write-host "`n`n@@ called Drivers" -fore magenta

dism /image:$mount /add-driver /driver:$invoke\drivers\shared /recurse
dism /image:$mount /add-driver /driver:$invoke\drivers\$arch /recurse
}


Function Apps
{
write-host "`n`n@@ Called Apps" -fore magenta

copy-item $invoke\winpe_.jpg $mount\windows\system32\winpe_.jpg -force -ea silentlyContinue

reg load hklm\winpe "$dest\mount\windows\system32\config\default"

new-itemProperty -path 'HKLM:\winpe\Control Panel\Desktop' -name Wallpaper -value "%systemroot%\system32\winpe_.jpg" -propertyType String -force
new-itemProperty -path 'HKLM:\winpe\Control Panel\Desktop' -name MaxMonitorDimension -value 1920 -propertyType DWORD -force
new-itemProperty -path 'HKLM:\winpe\Control Panel\Desktop' -name MaxVirtualDesktopDimension -value 1920 -propertyType DWORD -force

reg unload hklm\winpe

copy-item $invoke\apps\$arch $mount\apps -recurse -ea silentlyContinue
$unattend | out-file -encoding ascii $mount\unattend.xml
}


Function Media
{
write-host "`n`n@@ Called Media" -fore magenta

makewinpemedia /iso $dest $dest\winpe_$arch.iso
}


#
#


Clear
Prep
DismountCurrent
CopySource
MountCurrent
Packages
Drivers
Apps
DismountCurrent
Media