Skip to content

Instantly share code, notes, and snippets.

@awiddersheim
Created January 12, 2016 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awiddersheim/fa7bbce450df57e35af3 to your computer and use it in GitHub Desktop.
Save awiddersheim/fa7bbce450df57e35af3 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Builds a WinRE image.
.DESCRIPTION
Builds a WinRE image. It will inject iSCSI, some hot fixes, powershell and
a number of packages useful for installing Windows.
.EXAMPLE
./build-winre.ps1
.LINK
http://wiki.inetu.net
#>
Param(
# Where to create image and intermediate files. Defaults to current directory.
[ValidateNotNullOrEmpty()]
[alias("w")]
[string]$work_dir = $null,
# Where extra drivers needed for image are stored. Defaults to 'extra-dir' in current directory.
[ValidateNotNullOrEmpty()]
[alias("d")]
[string]$driver_dir = $null
)
function IsAdmin {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# all errors are bad errors
$ErrorActionPreference = "stop"
$adk_version = "8.1"
# get directory script is running from
$current_dir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
if (!(IsAdmin)) {
Write-Host "Running as ($(whoami)). Script must be run with Administrative privileges."
exit 1
}
if (!$work_dir) {
$work_dir = $current_dir
}
if (!$driver_dir) {
$driver_dir = (Join-Path $current_dir "extra-drivers")
}
$wim_path = Join-Path $current_dir "winre.wim"
$output_path = Join-Path $work_dir "boot.wim"
$hotfix_path = Join-Path $work_dir "Windows8-RT-KB2871318-x64.msu"
$mount_dir = Join-Path $work_dir "mount"
Write-Host "Making sure wim ($($wim_path)) exists"
if (!(Test-Path -PathType Leaf $wim_path)) {
Write-Host "Could not find ($($wim_path))"
exit 1
}
Write-Host "Copying ($($wim_path)) to ($($output_path))"
Copy-Item -Path $wim_path -Destination $output_path -Force
Write-Host "Creating mount directory ($($mount_dir)) exists"
New-Item -ItemType directory -Path $mount_dir -Force | Out-Null
Write-Host "Making sure hotfix ($($hotfix_path)) exists"
if (!(Test-Path -PathType Leaf $hotfix_path)) {
Write-Host "Could not find ($($hotfix_path))."
Write-Host "Download it here: http://support.microsoft.com/kb/2853726 http://support.microsoft.com/kb/2871318"
exit 1
}
$adk_path = Join-Path $([Environment]::GetFolderPath("ProgramFilesX86")) "Windows Kits\$($adk_version)\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64"
$package_path = Join-Path $adk_path "WinPE_OCs"
if (!(Test-Path -PathType Container $adk_path)) {
Write-Host "Could not find the ADK or it is not intsalled in the default location."
exit 1
}
Import-Module dism
try {
Write-Host "Mounting ($($output_path)) to ($($mount_dir))"
Mount-WindowsImage -ImagePath $output_path -Index 1 -Path $mount_dir | Out-Null
# this order is documented in http://technet.microsoft.com/library/hh824926.aspx
$packages = @(
"WinPE-WMI",
"WinPE-NetFX",
"WinPE-Scripting",
"WinPE-PowerShell",
"WinPE-LegacySetup",
"WinPE-Setup",
"WinPE-Setup-Server",
"WinPE-Setup-Client"
)
foreach ($package in $packages) {
Write-Host "Installing ($($package)) to image"
Add-WindowsPackage -PackagePath $(Join-Path $package_path "$($package).cab") -Path $mount_dir | Out-Null
}
Write-Host "Adding hotfix for http://support.microsoft.com/kb/2853726 http://support.microsoft.com/kb/2871318"
Add-WindowsPackage -PackagePath $hotfix_path -Path $mount_dir | Out-Null
$system_path = [Environment]::GetFolderPath("SYSTEM")
$mount_system_path = Join-Path $mount_dir "Windows\System32"
Write-Host "Adding iSCSI GUI"
Copy-Item -Force -Path $(Join-Path $system_path "iscsicpl.exe") -Destination $mount_system_path
Copy-Item -Force -Path $(Join-Path $system_path "iscsicpl.dll") -Destination $mount_system_path
Copy-Item -Force -Path $(Join-Path $system_path "en-us\iscsicpl.exe.mui") -Destination $(Join-Path $mount_system_path "en-us")
Copy-Item -Force -Path $(Join-Path $system_path "en-us\iscsicpl.dll.mui") -Destination $(Join-Path $mount_system_path "en-us")
if (Test-Path -PathType Container -Path $driver_dir) {
Write-Host "Adding drivers from ($($driver_dir))"
Add-WindowsDriver -Path $mount_dir -Driver $driver_dir -Recurse | Out-Null
} else {
Write-Host "Could not find driver directory ($($driver_dir))"
}
} finally {
Write-Host "Unmounting and saving ($($mount_dir))"
Dismount-WindowsImage -Save -Path $mount_dir | Out-Null
}
Write-Host "Finished creating wim which can be found here ($($output_path))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment