Skip to content

Instantly share code, notes, and snippets.

@manelrodero
Last active July 2, 2022 15:19
Show Gist options
  • Save manelrodero/b1b8eba118b9d59b1f7b013193a86fb6 to your computer and use it in GitHub Desktop.
Save manelrodero/b1b8eba118b9d59b1f7b013193a86fb6 to your computer and use it in GitHub Desktop.
Script to extract the Windows Server 2022 Standard with Desktop Experience image from a Windows Server 2022 media
<#
.SYNOPSIS
Script to extract the Windows Server 2022 Standard with Desktop Experience image from a Windows Server 2022 media.
.DESCRIPTION
This script extracts the Windows Server 2022 Standard with Desktop Experience image from a Windows Server 2022 media.
.NOTES
Created : 1 July 2022
Modified : 2 July 2022
Version : 1.1
Author : Manel Rodero
Twitter : @manelrodero
Blog : https://www.manelrodero.com/
Based on the original script by Johan Arwidmark:
https://github.com/DeploymentResearch/DRFiles/blob/master/Scripts/Export-WindowsServer2022WIMfromISO.ps1
Disclaimer:
This script has only been tested with PowerShell 5.1.
This script is provided "AS IS" with no warranties, confers no rights and is not supported by the author.
.PARAMETER HydrationKitBase
Specifies the directory to which the software will be downloaded and the installers will be extracted.
It is recommended that you do not use a directory with a name that contains spaces.
.PARAMETER UseEvaluation
Specifies whether you want to use the evaluation version of Microsoft Windows Server 2022
The default option is $true.
.PARAMETER ISOName
Specifies the name of the licensed Windows Server 2022 ISO file.
.EXAMPLE
.\Export-WindowsServer2022WIMfromISO.ps1 -HydrationKitBase "D:\HydrationKitBase"
Extracts the Windows Server 2022 Standard image (with Desktop Experience) from the
Windows Server 2022 evaluation media to "D:\HydrationKitBase\Setup\ReferenceWIM\REFWS2022-001.wim".
.EXAMPLE
.\Export-WindowsServer2022WIMfromISO.ps1 -HydrationKitBase "D:\HydrationKitBase" -UseEvaluation $false -ISOName "WindowsServer2022_x64_en-us_Licensed.iso"
Extracts the Windows Server 2022 Standard image (with Desktop Experience) from the
Windows Server 2022 licensed media to "D:\HydrationKitBase\Setup\ReferenceWIM\REFWS2022-001.wim".
#>
#Requires -RunAsAdministrator
#Requires -Version 5.1
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[parameter(Mandatory=$true, HelpMessage="Specify the directory to which the software has been downloaded.")]
[ValidateNotNullOrEmpty()]
[string]$HydrationKitBase,
[parameter(Mandatory=$false, HelpMessage="Specify whether you want to use the evaluation version of Microsoft Windows Server 2022.")]
[ValidateSet($false,$true)]
[boolean]$UseEvaluation = $true,
[parameter(Mandatory=$false, HelpMessage="Specify the name of the licensed Windows Server 2022 ISO file.")]
[ValidatePattern("\.iso$")]
[string]$ISOName
)
# Function to color the text
Function Write-Color ($Color, $Text) {
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = $Color
Write-Output $Text
$host.ui.RawUI.ForegroundColor = $t
}
# Directory structure
$HKDownloads = "$HydrationKitBase\Downloads"
# Verify Evaluation vs License
if ($UseEvaluation) {
$ISOName = "WindowsServer2022_Eval_x64_en-us.iso"
$ISO = "$HKDownloads\$ISOName"
} else {
if ($ISOName) {
$ISO = "$HKDownloads\$ISOName"
} else {
Write-Warning "You must specify the name of the Windows Server 2022 ISO file. Aborting..."
Break
}
}
# General parameters
$WIMPath = "$HydrationKitBase\Setup\ReferenceWIM"
$WIMFile = "$WIMPath\REFWS2022-001.wim"
$Edition = "Windows Server 2022 SERVERSTANDARD"
# Verify that the Hydration Kit base directory exist
If (!(Test-Path -Path $HydrationKitBase)) {
Write-Warning "The Hydration Kit base directory does not exist. Use the 'Download-SoftwareForHydrationKit.ps1' script to download the kit and the necessary software. Aborting..."
Break
}
# Goal is to have a single index WIM File, so checking if target WIM file exist (if a WIM file exists, a new index will be appended)
If (Test-Path -Path $WIMFile) {
Write-Warning "WIM File: $WIMFile does already exist. Rename or delete the file, then try again. Aborting..."
Break
}
# ISO Validation
If (!(Test-Path -Path $ISO)) {
Write-Warning "ISO File: $ISO does not exist. Aborting..."
Break
}
# Mount ISO
Write-Color "Cyan" "Mounting '$ISOName' ..."
Mount-DiskImage -ImagePath $ISO | Out-Null
$ISOImage = Get-DiskImage -ImagePath $ISO | Get-Volume
$ISODrive = [string]$ISOImage.DriveLetter+":"
Write-Color "Cyan" "Drive is $ISODrive\"
# Source WIM validation
$SourceWIMFile = "$ISODrive\sources\install.wim"
# Get-WindowsImage -ImagePath $SourceWIMFile
If (-not (Get-WindowsImage -ImagePath $SourceWIMFile | Where-Object {$_.ImageName -ilike "*$($Edition)"})){
Write-Warning "WIM Edition: $Edition does not exist in WIM: $SourceWIMFile. Aborting..."
Dismount-DiskImage -ImagePath $ISO | Out-Null
Break
}
# Export WIM
Write-Color "Cyan" "Exporting image to '$WIMFile' ..."
If (!(Test-Path -Path $WIMPath)) { New-Item -Path $WIMPath -ItemType Directory -Force | Out-Null }
Export-WindowsImage -SourceImagePath $SourceWIMFile -SourceName $Edition -DestinationImagePath $WIMFile | Out-Null
# .NET Framework 3.5 offline
Write-Color "Cyan" "Solving .NET Framework 3.5 offline install ..."
Write-Color "Green" "Copying file 'microsoft-windows-netfx3-ondemand-package~31bf3856ad364e35~amd64~~.cab' ..."
Copy-Item -Path "$ISODrive\sources\sxs\microsoft-windows-netfx3-ondemand-package~31bf3856ad364e35~amd64~~.cab" -Destination $WIMPath
Set-ItemProperty -Path "$WIMPath\microsoft-windows-netfx3-ondemand-package~31bf3856ad364e35~amd64~~.cab" -Name IsReadOnly -Value $false
# Dismount ISO
Write-Color "Cyan" "Dismounting '$ISOName' ..."
Dismount-DiskImage -ImagePath $ISO | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment