Invoke-OSBuildTask.ps1

function Invoke-OSBuildTask {
    [CmdletBinding()]
    Param (
        [switch]$DownloadUpdates,
        [switch]$DontUseNewestMedia,
        [switch]$Execute
    )
    #======================================================================================
    # Start 18.9.13
    #======================================================================================
    Write-Host "===========================================================================" -ForegroundColor Green
    Write-Host "Invoke-OSBuildTask" -ForegroundColor Green
    Write-Host "===========================================================================" -ForegroundColor Green
    #======================================================================================
    # Validate Administrator Rights 18.9.13
    #======================================================================================
    if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
            [Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
        Write-Host ""
        Write-Host "This function needs to be run as Administrator" -ForegroundColor Yellow
        Write-Host ""
        Return
    }
    #======================================================================================
    # Initialize OSBuilder 18.9.13
    #======================================================================================
    Get-OSBuilder -CreatePaths -HideDetails
    Write-Host ""
    #======================================================================================
    # Select Task JSON
    #======================================================================================
    $SelectTask = Get-ChildItem -Path $TasksPath *.json -File | Where-Object {$_.Name -notlike "*OSMedia*"} | Select-Object -Property BaseName, FullName, Length, CreationTime | Sort-Object -Property FullName
    $SelectTask = $SelectTask | Out-GridView -Passthru -Title "OSBuilder Tasks: Select one or more Tasks to execute and press OK (Cancel to Exit)"

    if($null -eq $SelectTask) {
        Write-Warning "OSBuild Task was not selected or found . . . Exiting!"
        Return
    }
    #======================================================================================
    # Start Task
    #======================================================================================
    foreach ($TaskFile in $SelectTask) {
        #======================================================================================
        # Read Task Contents
        #======================================================================================
        $Task = Get-Content "$($TaskFile.FullName)" | ConvertFrom-Json
        $TaskName = $($Task.TaskName)
        $TaskVersion = $($Task.TaskVersion)
        $TaskType = $($Task.TaskType)
        $MediaName = $($Task.MediaName)
        $MediaPath = "$OSMediaPath\$MediaName"
        $BuildName = $($Task.BuildName)
        $CustomBuildName = $($Task.BuildName)
        $EnableNetFX3 = $($Task.EnableNetFX3)
        $RemoveAppx = $($Task.RemoveAppx)
        $RemovePackages = $($Task.RemovePackages)
        $RemoveCapability = $($Task.RemoveCapability)
        $EnableFeature = $($Task.EnableFeature)
        $DisableFeature = $($Task.DisableFeature)
        $LanguagePacks = $($Task.LanguagePacks)
        $Drivers = $($Task.Drivers)
        $Packages = $($Task.Packages)
        $ExtraFiles = $($Task.ExtraFiles)
        $Scripts = $($Task.Scripts)
        $StartLayout = "$($Task.StartLayout)"
        $Unattend = "$($Task.Unattend)"
        $WinPEDaRT = $($Task.WinPEDaRT)
        $WinPEDrivers = $($Task.WinPEDrivers)
        $SetupWimADK = $($Task.SetupWimADK)
        $WinPEWimADK = $($Task.WinPEWimADK)
        $WinREWimADK = $($Task.WinREWimADK)
        $SetupWimExtra = $($Task.SetupWimExtra)
        $WinPEWimExtra = $($Task.WinPEWimExtra)
        $WinREWimExtra = $($Task.WinREWimExtra)
        $SetupWimScripts = $($Task.SetupWimScripts)
        $WinPEWimScripts = $($Task.WinPEWimScripts)
        $WinREWimScripts = $($Task.WinREWimScripts)
        #$ServicingStack = "$StacksPath\$($Task.ServicingStack)"
        #$WindowsUpdate = "$UpdatesPath\$($Task.WindowsUpdate)"
        #======================================================================================
        # Start Task
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Green
        Write-Host "Starting Task: $TaskName" -ForegroundColor Green
        Write-Host "===========================================================================" -ForegroundColor Green
        #======================================================================================
        # Validate Proper TaskVersion
        #======================================================================================
        if ([System.Version]$TaskVersion -lt [System.Version]"18.7.23") {
            Write-Warning "OSBuilder Tasks need to be version 18.7.23 or newer"
            Write-Warning "Please recreate this Task using New-OSBuildTask"
            Return
        }
        #======================================================================================
        # Select Latest Media
        #======================================================================================
        if (!($DontUseNewestMedia)) {
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Task Source OSMedia" -ForegroundColor Yellow
            Write-Host "-Media Name: $MediaName" -ForegroundColor Cyan
            Write-Host "-Media Path: $MediaPath" -ForegroundColor Cyan

            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Latest Source OSMedia" -ForegroundColor Yellow
            $LatestSource = Get-ChildItem $OSMediaPath -Directory -filter "*$($MediaName.split(".")[0]).*" | Sort-Object {[int] $_.Name.Split(".")[1]} | Select-Object -Last 1
            $MediaName = $LatestSource.BaseName
            $MediaPath = "$OSMediaPath\$MediaName"
            Write-Host "-Media Name: $MediaName" -ForegroundColor Cyan
            Write-Host "-Media Path: $MediaPath" -ForegroundColor Cyan
        }
        #======================================================================================
        # Get Windows Image Information
        #======================================================================================
        $OSSourcePath = $MediaPath
        $OSImagePath = "$OSSourcePath\OS\sources\install.wim"
        $OSImageIndex = 1
        $WindowsImage = Get-WindowsImage -ImagePath "$OSImagePath" -Index $OSImageIndex | Select-Object -Property *

        $OSImageName = $($WindowsImage.ImageName)
        $OSImageDescription = $($WindowsImage.ImageDescription)
        if ($($WindowsImage.Architecture) -eq 0) {$OSArchitecture = 'x86'}
        elseif ($($WindowsImage.Architecture) -eq 1) {$OSArchitecture = 'MIPS'}
        elseif ($($WindowsImage.Architecture) -eq 2) {$OSArchitecture = 'Alpha'}
        elseif ($($WindowsImage.Architecture) -eq 3) {$OSArchitecture = 'PowerPC'}
        elseif ($($WindowsImage.Architecture) -eq 6) {$OSArchitecture = 'ia64'}
        elseif ($($WindowsImage.Architecture) -eq 9) {$OSArchitecture = 'x64'}
        else {$OSArchitecture = $null}
        $OSEditionID = $($WindowsImage.EditionId)
        $OSInstallationType = $($WindowsImage.InstallationType)
        $OSLanguages = $($WindowsImage.Languages)
        $OSBuild = $($WindowsImage.Build)
        $OSVersion = $($WindowsImage.Version)
        $OSSPBuild = $($WindowsImage.SPBuild)
        $OSSPLevel = $($WindowsImage.SPLevel)
        $OSImageBootable = $($WindowsImage.ImageBootable)
        $OSWIMBoot = $($WindowsImage.WIMBoot)
        $OSCreatedTime = $($WindowsImage.CreatedTime)
        $OSModifiedTime = $($WindowsImage.ModifiedTime)
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "OSMedia Information" -ForegroundColor Yellow
        Write-Host "-Source Path: $OSSourcePath" -ForegroundColor Cyan
        Write-Host "-Image File: $OSImagePath" -ForegroundColor Cyan
        Write-Host "-Image Index: $OSImageIndex" -ForegroundColor Cyan
        Write-Host "-Name: $OSImageName" -ForegroundColor Cyan
        Write-Host "-Description: $OSImageDescription" -ForegroundColor Cyan
        Write-Host "-Architecture: $OSArchitecture" -ForegroundColor Cyan
        Write-Host "-Edition: $OSEditionID" -ForegroundColor Cyan
        Write-Host "-Type: $OSInstallationType" -ForegroundColor Cyan
        Write-Host "-Languages: $OSLanguages" -ForegroundColor Cyan
        Write-Host "-Build: $OSBuild" -ForegroundColor Cyan
        Write-Host "-Version: $OSVersion" -ForegroundColor Cyan
        Write-Host "-SPBuild: $OSSPBuild" -ForegroundColor Cyan
        Write-Host "-SPLevel: $OSSPLevel" -ForegroundColor Cyan
        Write-Host "-Bootable: $OSImageBootable" -ForegroundColor Cyan
        Write-Host "-WimBoot: $OSWIMBoot" -ForegroundColor Cyan
        Write-Host "-Created Time: $OSCreatedTime" -ForegroundColor Cyan
        Write-Host "-Modified Time: $OSModifiedTime" -ForegroundColor Cyan
        #======================================================================================
        if (Test-Path "$OSSourcePath\info\xml\CurrentVersion.xml") {
            $RegCurrentVersion = Import-Clixml -Path "$OSSourcePath\info\xml\CurrentVersion.xml"
            $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
        } else {
            if ($OSBuild -eq 17134) {$OSVersionNumber = 1803}
            if ($OSBuild -eq 16299) {$OSVersionNumber = 1709}
            if ($OSBuild -eq 15063) {$OSVersionNumber = 1703}
            if ($OSBuild -eq 14393) {$OSVersionNumber = 1607}
            if ($OSBuild -eq 10240) {$OSVersionNumber = 1507}
        }
        #======================================================================================
        # Set Working Path
        #======================================================================================
        $BuildName = "build$((Get-Date).ToString('mmss'))"
        $WorkingPath = "$Script:OSBuildsPath\$BuildName"
        #======================================================================================
        # Validate Exiting WorkingPath
        #======================================================================================
        if (Test-Path $WorkingPath) {
            Write-Warning "$WorkingPath exists. Contents will be replaced"
            Remove-Item -Path "$WorkingPath" -Force -Recurse
            Write-Host ""
        }
        #======================================================================================
        # Download Update Catalog 18.9.13
        #======================================================================================
        if (!(Test-Path $CatalogXml)) {Get-OSBuilderUpdates -CatalogUpdate -HideDetails}
        #======================================================================================
        # Get Current Updates XML 18.9.13
        #======================================================================================
        if (Test-Path $CatalogXml) {
            $Catalog = Import-Clixml -Path "$CatalogXml"
        } else {
            Write-Warning "Update Catalog does not exist. Exiting"
            Break
        }
        #======================================================================================
        # Update Adobe Path
        #======================================================================================
        $UpdateAdobe = $Catalog | Where-Object {$_.Category -eq 'Adobe'}
        $UpdateAdobe = $UpdateAdobe | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
        $UpdateAdobe = $UpdateAdobe | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
        if ($OSInstallationType -like "*Server*") {
            $UpdateAdobe = $UpdateAdobe | Where-Object {$_.KBTitle -like "*Server*"}
        } else {
            $UpdateAdobe = $UpdateAdobe | Where-Object {$_.KBTitle -notlike "*Server*"}
        }
        #======================================================================================
        # Update Component Path
        #======================================================================================
        $UpdateComponent = $Catalog | Where-Object {$_.Category -eq 'Component'}
        $UpdateComponent = $UpdateComponent | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
        $UpdateComponent = $UpdateComponent | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
        if ($OSInstallationType -like "*Server*") {
            $UpdateComponent = $UpdateComponent | Where-Object {$_.KBTitle -like "*Server*"}
        } else {
            $UpdateComponent = $UpdateComponent | Where-Object {$_.KBTitle -notlike "*Server*"}
        }
        $UpdateComponent = $UpdateComponent | Sort-Object -Property KBTitle
        #======================================================================================
        # Cumulative Updates
        #======================================================================================
        $UpdateCumulative = $Catalog | Where-Object {$_.Category -eq 'Cumulative'}
        $UpdateCumulative = $UpdateCumulative | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
        $UpdateCumulative = $UpdateCumulative | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
        if ($OSInstallationType -like "*Server*") {
            $UpdateCumulative = $UpdateCumulative | Where-Object {$_.KBTitle -like "*Server*"}
        } else {
            $UpdateCumulative = $UpdateCumulative | Where-Object {$_.KBTitle -notlike "*Server*"}
        }
        $UpdateCumulative = $UpdateCumulative | Sort-Object -Property DatePosted
        #======================================================================================
        # Update Servicing Path
        #======================================================================================
        $UpdateServicing = $Catalog | Where-Object {$_.Category -eq 'Servicing'}
        $UpdateServicing = $UpdateServicing | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
        $UpdateServicing = $UpdateServicing | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
        if ($OSInstallationType -like "*Server*") {
            $UpdateServicing = $UpdateServicing | Where-Object {$_.KBTitle -like "*Server*"}
        } else {
            $UpdateServicing = $UpdateServicing | Where-Object {$_.KBTitle -notlike "*Server*"}
        }
        #======================================================================================
        # Update Setup Path
        #======================================================================================
        $UpdateSetup = $Catalog | Where-Object {$_.Category -eq 'Setup'}
        $UpdateSetup = $UpdateSetup | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
        $UpdateSetup = $UpdateSetup | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
        if ($OSInstallationType -like "*Server*") {
            $UpdateSetup = $UpdateSetup | Where-Object {$_.KBTitle -like "*Server*"}
        } else {
            $UpdateSetup = $UpdateSetup | Where-Object {$_.KBTitle -notlike "*Server*"}
        }
        #======================================================================================
        # Update Validation 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Updates to Apply" -ForegroundColor Yellow
        if ($DownloadUpdates.IsPresent) {Get-OSBuilderUpdates -CatalogUpdate -HideDetails}

        foreach ($Update in $UpdateAdobe) {
            $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
            if (!(Test-Path "$UpdatesPath\*\$($Update.KBTitle)\$($Update.FileName)")) {
                if ($DownloadUpdates.IsPresent) {
                    Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                    Get-OSBuilderUpdates -KBTitle "$($Update.KBTitle)" -Download -HideDetails
                } else {
                    Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                    $Execute = $false
                }
            }
        }
        foreach ($Update in $UpdateComponent) {
            $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
            if (!(Test-Path "$UpdatesPath\*\$($Update.KBTitle)\$($Update.FileName)")) {
                if ($DownloadUpdates.IsPresent) {
                    Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                    Get-OSBuilderUpdates -KBTitle "$($Update.KBTitle)" -Download -HideDetails
                } else {
                    Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                    $Execute = $false
                }
            }
        }
        foreach ($Update in $UpdateCumulative) {
            $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
            if (!(Test-Path "$UpdatesPath\*\$($Update.KBTitle)\$($Update.FileName)")) {
                if ($DownloadUpdates.IsPresent) {
                    Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                    Get-OSBuilderUpdates -KBTitle "$($Update.KBTitle)" -Download -HideDetails
                } else {
                    Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                    $Execute = $false
                }
            }
        }
        foreach ($Update in $UpdateServicing) {
            $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
            if (!(Test-Path "$UpdatesPath\*\$($Update.KBTitle)\$($Update.FileName)")) {
                if ($DownloadUpdates.IsPresent) {
                    Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                    Get-OSBuilderUpdates -KBTitle "$($Update.KBTitle)" -Download -HideDetails
                } else {
                    Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                    $Execute = $false
                }
            }
        }
        foreach ($Update in $UpdateSetup) {
            $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
            if (!(Test-Path "$UpdatesPath\*\$($Update.KBTitle)\$($Update.FileName)")) {
                if ($DownloadUpdates.IsPresent) {
                    Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                    Get-OSBuilderUpdates -KBTitle "$($Update.KBTitle)" -Download -HideDetails
                } else {
                    Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                    $Execute = $false
                }
            }
        }
        #======================================================================================
        # Task Information
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Task Information" -ForegroundColor Yellow
        Write-Host "-TaskName: $TaskName" -ForegroundColor Cyan
        Write-Host "-TaskVersion: $TaskVersion" -ForegroundColor Cyan
        Write-Host "-TaskType: $TaskType" -ForegroundColor Cyan
        Write-Host "-Media Name: $MediaName" -ForegroundColor Cyan
        Write-Host "-Media Path: $MediaPath" -ForegroundColor Cyan
        Write-Host "-Build Name: $BuildName" -ForegroundColor Cyan
        Write-Host "-Build Path: $WorkingPath" -ForegroundColor Cyan
        #Write-Host "-Servicing Stack: $ServicingStack" -ForegroundColor Cyan
        #Write-Host "-Windows Update: $WindowsUpdate" -ForegroundColor Cyan
        Write-Host "-Enable NetFx3: $EnableNetFX3" -ForegroundColor Cyan
        Write-Host "-Remove Appx:" -ForegroundColor Cyan
        if ($RemoveAppx){foreach ($item in $RemoveAppx) {Write-Host $item}}
        Write-Host "-Remove Package:" -ForegroundColor Cyan
        if ($RemovePackages){foreach ($item in $RemovePackages) {Write-Host $item}}
        Write-Host "-Remove Capability:" -ForegroundColor Cyan
        if ($RemoveCapability){foreach ($item in $RemoveCapability) {Write-Host $item}}
        Write-Host "-Enable Feature:" -ForegroundColor Cyan
        if ($EnableFeature){foreach ($item in $EnableFeature) {Write-Host $item}}
        Write-Host "-Disable Feature:" -ForegroundColor Cyan
        if ($DisableFeature){foreach ($item in $DisableFeature) {Write-Host $item}}
        Write-Host "-Language Packs:" -ForegroundColor Cyan
        if ($LanguagePacks){foreach ($item in $LanguagePacks) {Write-Host $item}}
        Write-Host "-Drivers:" -ForegroundColor Cyan
        if ($Drivers){foreach ($item in $Drivers) {Write-Host $item}}
        Write-Host "-Packages:" -ForegroundColor Cyan
        if ($Packages){foreach ($item in $Packages) {Write-Host $item}}
        Write-Host "-Scripts:" -ForegroundColor Cyan
        if ($Scripts){foreach ($item in $Scripts) {Write-Host $item}}
        Write-Host "-Extra Files:" -ForegroundColor Cyan
        if ($ExtraFiles){foreach ($item in $ExtraFiles) {Write-Host $item}}
        Write-Host "-Start Layout: $StartLayout" -ForegroundColor Cyan
        Write-Host "-Unattend: $Unattend" -ForegroundColor Cyan
        Write-Host "-WinPE DaRT: $WinPEDaRT" -ForegroundColor Cyan
        Write-Host "-WinPE Drivers: $WinPEDrivers" -ForegroundColor Cyan
        Write-Host "-Setup Wim Pkgs:" -ForegroundColor Cyan
        if ($SetupWimADK){foreach ($item in $SetupWimADK) {Write-Host $item}}
        Write-Host "-WinPE Wim Pkgs:" -ForegroundColor Cyan
        if ($WinPEWimADK){foreach ($item in $WinPEWimADK) {Write-Host $item}}
        Write-Host "-WinRE Wim Pkgs:" -ForegroundColor Cyan
        if ($WinREWimADK){foreach ($item in $WinREWimADK) {Write-Host $item}}
        Write-Host "-Setup Wim Extra Files:" -ForegroundColor Cyan
        if ($SetupWimExtra){foreach ($item in $SetupWimExtra) {Write-Host $item}}
        Write-Host "-WinPE Wim Extra Files:" -ForegroundColor Cyan
        if ($WinPEWimExtra){foreach ($item in $WinPEWimExtra) {Write-Host $item}}
        Write-Host "-WinRE Wim Extra Files:" -ForegroundColor Cyan
        if ($WinREWimExtra){foreach ($item in $WinREWimExtra) {Write-Host $item}}
        Write-Host "-Setup Wim PowerShell Scripts:" -ForegroundColor Cyan
        if ($SetupWimScripts){foreach ($item in $SetupWimScripts) {Write-Host $item}}
        Write-Host "-WinPE Wim PowerShell Scripts:" -ForegroundColor Cyan
        if ($WinPEWimScripts){foreach ($item in $WinPEWimScripts) {Write-Host $item}}
        Write-Host "-WinRE Wim PowerShell Scripts:" -ForegroundColor Cyan
        if ($WinREWimScripts){foreach ($item in $WinREWimScripts) {Write-Host $item}}
        #======================================================================================
        # Execute
        #======================================================================================
        if ($Execute.IsPresent) {
            $Info = Join-Path $WorkingPath 'info'
            $LogsJS = Join-Path $Info 'json'
            $LogsXML = Join-Path $Info 'xml'
            $Logs =    Join-Path $Info "logs"
            if (!(Test-Path "$Info"))        {New-Item "$Info" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$LogsJS"))        {New-Item "$LogsJS" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$LogsXML"))    {New-Item "$LogsXML" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$Logs"))        {New-Item "$Logs" -ItemType Directory -Force | Out-Null}

            $OS = Join-Path $WorkingPath "OS"
            $WinPE = Join-Path $WorkingPath "WinPE"
            if (!(Test-Path "$OS"))            {New-Item "$OS" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$WinPE"))        {New-Item "$WinPE" -ItemType Directory -Force | Out-Null}

            $PEInfo = Join-Path $WinPE 'info'
            $PELogsJS = Join-Path $PEInfo 'json'
            $PELogsXML = Join-Path $PEInfo 'xml'
            $PELogs =    Join-Path $PEInfo "logs"

            if (!(Test-Path "$PEInfo"))        {New-Item "$PEInfo" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$PELogsJS"))    {New-Item "$PELogsJS" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$PELogsXML"))    {New-Item "$PELogsXML" -ItemType Directory -Force | Out-Null}
            if (!(Test-Path "$PELogs"))        {New-Item "$PELogs" -ItemType Directory -Force | Out-Null}

            $WimTemp = Join-Path $WorkingPath "WimTemp"
            if (!(Test-Path "$WimTemp"))    {New-Item "$WimTemp" -ItemType Directory -Force | Out-Null}
            #======================================================================================
            # Start the Transcript
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Starting Transcript" -ForegroundColor Yellow
            $ScriptName = $MyInvocation.MyCommand.Name
            $LogName = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-$ScriptName.log"
            Start-Transcript -Path (Join-Path $Logs $LogName)
            #======================================================================================
            # Display Build Paths 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Creating OSBuild $BuildName" -ForegroundColor Yellow
            Write-Host "Working Path: $WorkingPath" -ForegroundColor Yellow
            Write-Host "-Info: $Info" -ForegroundColor Cyan
            Write-Host "-Logs: $Logs" -ForegroundColor Cyan
            Write-Host "-OS: $OS" -ForegroundColor Cyan
            Write-Host "-WinPE: $WinPE" -ForegroundColor Cyan
            #======================================================================================
            # Create Mount Directories 18.9.10
            #======================================================================================
            $MountDirectory = Join-Path $MountPath "os$((Get-Date).ToString('mmss'))"
            if ( ! (Test-Path "$MountDirectory")) {New-Item "$MountDirectory" -ItemType Directory -Force | Out-Null}
            $MountWinPE = Join-Path $MountPath "winpe$((Get-Date).ToString('mmss'))"
            if ( ! (Test-Path "$MountWinPE")) {New-Item "$MountWinPE" -ItemType Directory -Force | Out-Null}
            $MountSetup = Join-Path $MountPath "setup$((Get-Date).ToString('mmss'))"
            if ( ! (Test-Path "$MountSetup")) {New-Item "$MountSetup" -ItemType Directory -Force | Out-Null}
            $MountWinRE = Join-Path $MountPath "winre$((Get-Date).ToString('mmss'))"
            if ( ! (Test-Path "$MountWinRE")) {New-Item "$MountWinRE" -ItemType Directory -Force | Out-Null}
            #======================================================================================
            # Copy OS 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Copying Operating System to $BuildName" -ForegroundColor Yellow
            Write-Host "Copying OS to $WorkingPath"
            Copy-Item -Path "$OSSourcePath\*" -Destination "$WorkingPath" -Exclude ('*.wim','*.iso') -Recurse -Force | Out-Null
            if (Test-Path "$WorkingPath\ISO"){Remove-Item -Path "$WorkingPath\ISO" -Force -Recurse | Out-Null}
            Write-Host "Copying install.wim to $WimTemp\install.wim"
            Copy-Item -Path "$OSSourcePath\OS\sources\install.wim" -Destination "$WimTemp\install.wim" -Force | Out-Null
            Write-Host "Copying WinPE to $WimTemp"
            Copy-Item -Path "$OSSourcePath\WinPE\*.wim" -Destination "$WimTemp" -Exclude boot.wim -Force | Out-Null
            #======================================================================================
            # Setup Update 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Setup Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateSetup)) {
                foreach ($Update in $UpdateSetup) {
                    $UpdateSetup = $(Get-ChildItem -Path $UpdatesPath -File -Recurse | Where-Object {$_.Name -eq $($Update.FileName)}).FullName
                    if (Test-Path "$UpdateSetup") {
                        Write-Host "expand.exe '$UpdateSetup' -F:*.* $OS\Sources"
                        expand.exe "$UpdateSetup" -F:*.* "$OS\Sources"
                    } else {
                        Write-Warning "Not Found: $UpdateSetup"
                    }
                }
            }
            #======================================================================================
            # WinPE Phase: Mount 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Mount Setup WIM" -ForegroundColor Yellow
            Mount-WindowsImage -ImagePath "$WimTemp\setup.wim" -Index 1 -Path "$MountSetup" -Optimize -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Mount-WindowsImage-setup.wim.log"
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Mount WinPE WIM" -ForegroundColor Yellow
            Mount-WindowsImage -ImagePath "$WimTemp\winpe.wim" -Index 1 -Path "$MountWinPE" -Optimize -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Mount-WindowsImage-winpe.wim.log"
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Mount WinRE WIM" -ForegroundColor Yellow
            Mount-WindowsImage -ImagePath "$WimTemp\winre.wim" -Index 1 -Path "$MountWinRE" -Optimize -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Mount-WindowsImage-winre.wim.log"
            #======================================================================================
            # WinPE Phase: Update Sources 18.9.10
            #======================================================================================
<# Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Updating Media Sources with Setup.wim" -ForegroundColor Yellow
            robocopy "$MountSetup" "$OS" setup.exe /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Setup-setup.wim.log"
            robocopy "$MountSetup\sources" "$OS\sources" *.* /e /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Sources-setup.wim.log" #>

            #======================================================================================
            # WinPE Phase: Servicing Update 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Servicing Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateServicing)) {
                foreach ($Update in $UpdateServicing) {
                    $UpdateSSU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateSSU") {
                        Write-Host "setup.wim: $UpdateSSU"
                        if (Get-WindowsPackage -Path "$MountSetup" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountSetup" -PackagePath "$UpdateSSU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateServicing-KB$($Update.KBNumber)-setup.wim.log"
                        }
                        Write-Host "winpe.wim: $UpdateSSU"
                        if (Get-WindowsPackage -Path "$MountWinPE" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountWinPE" -PackagePath "$UpdateSSU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateServicing-KB$($Update.KBNumber)-winpe.wim.log"
                        }
                        Write-Host "winre.wim: $UpdateSSU"
                        if (Get-WindowsPackage -Path "$MountWinRE" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountWinRE" -PackagePath "$UpdateSSU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateServicing-KB$($Update.KBNumber)-winre.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateSSU"
                    }
                }
            }
            #======================================================================================
            # WinPE Phase: Setup WIM ADK Optional Components 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Setup WIM ADK Optional Components" -ForegroundColor Yellow
            if ([string]::IsNullOrEmpty($SetupWimADK) -or [string]::IsNullOrWhiteSpace($SetupWimADK)) {
                # Do Nothing
            } else {
                foreach ($PackagePath in $SetupWimADK) {
                    if ($PackagePath -like "*WinPE-NetFx*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountSetup" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-setup.wim.log" | Out-Null
                    }
                }
                $SetupWimADK = $SetupWimADK | Where-Object {$_.Name -notlike "*WinPE-NetFx*"}
                foreach ($PackagePath in $SetupWimADK) {
                    if ($PackagePath -like "*WinPE-PowerShell*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountSetup" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-setup.wim.log" | Out-Null
                    }
                }
                $SetupWimADK = $SetupWimADK | Where-Object {$_.Name -notlike "*WinPE-PowerShell*"}
                foreach ($PackagePath in $SetupWimADK) {
                    Write-Host $PackagePath -ForegroundColor Cyan
                    Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountSetup" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-setup.wim.log" | Out-Null
                }
            }
            #======================================================================================
            # WinPE Phase: WinPE WIM ADK Optional Components 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinPE WIM ADK Optional Components" -ForegroundColor Yellow
            if ([string]::IsNullOrEmpty($WinPEWimADK) -or [string]::IsNullOrWhiteSpace($WinPEWimADK)) {
                # Do Nothing
            } else {
                foreach ($PackagePath in $WinPEWimADK) {
                    if ($PackagePath -like "*WinPE-NetFx*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinPE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winpe.wim.log" | Out-Null
                    }
                }
                $WinPEWimADK = $WinPEWimADK | Where-Object {$_.Name -notlike "*WinPE-NetFx*"}
                foreach ($PackagePath in $WinPEWimADK) {
                    if ($PackagePath -like "*WinPE-PowerShell*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinPE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winpe.wim.log" | Out-Null
                    }
                }
                $WinPEWimADK = $WinPEWimADK | Where-Object {$_.Name -notlike "*WinPE-PowerShell*"}
                foreach ($PackagePath in $WinPEWimADK) {
                    Write-Host $PackagePath -ForegroundColor Cyan
                    Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinPE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winpe.wim.log" | Out-Null
                }
            }
            #======================================================================================
            # WinPE Phase: WinRE WIM ADK Optional Components 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinRE WIM ADK Optional Components" -ForegroundColor Yellow
            if ([string]::IsNullOrEmpty($WinREWimADK) -or [string]::IsNullOrWhiteSpace($WinREWimADK)) {
                # Do Nothing
            } else {
                foreach ($PackagePath in $WinREWimADK) {
                    if ($PackagePath -like "*WinPE-NetFx*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinRE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winre.wim.log" | Out-Null
                    }
                }
                $WinREWimADK = $WinREWimADK | Where-Object {$_.Name -notlike "*WinPE-NetFx*"}
                foreach ($PackagePath in $WinREWimADK) {
                    if ($PackagePath -like "*WinPE-PowerShell*") {
                        Write-Host $PackagePath -ForegroundColor Cyan
                        Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinRE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winre.wim.log" | Out-Null
                    }
                }
                $WinREWimADK = $WinREWimADK | Where-Object {$_.Name -notlike "*WinPE-PowerShell*"}
                foreach ($PackagePath in $WinREWimADK) {
                    Write-Host $PackagePath -ForegroundColor Cyan
                    Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountWinRE" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage-winre.wim.log" | Out-Null
                }
            }
            #======================================================================================
            # WinPE Phase: Cumulative Update 18.9.13
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Cumulative Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateCumulative)) {
                foreach ($Update in $UpdateCumulative) {
                    $UpdateCU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateCU") {
                        Write-Host "setup.wim: $UpdateCU"
                        #if (Get-WindowsPackage -Path "$MountSetup" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {}
                        $SessionsXmlSetup = "$MountSetup\Windows\Servicing\Sessions\Sessions.xml"
                        if (Test-Path $SessionsXmlSetup) {
                            [xml]$XmlDocument = Get-Content -Path $SessionsXmlSetup
                            if ($XmlDocument.Sessions.Session.Tasks.Phase.package | Where-Object {$_.Name -like "*$($Update.KBNumber)*" -and $_.targetState -eq 'Installed'}) {
                                Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                            } else {
                                Add-WindowsPackage -Path "$MountSetup" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-setup.wim.log"
                                Dism /Image:"$MountSetup" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-setup.wim.log"
                            }
                        } else {
                            Add-WindowsPackage -Path "$MountSetup" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-setup.wim.log"
                            Dism /Image:"$MountSetup" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-setup.wim.log"
                        }
                        Write-Host "winpe.wim: $UpdateCU"
                        #if (Get-WindowsPackage -Path "$MountWinPE" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {}
                        $SessionsXmlWinPE = "$MountWinPE\Windows\Servicing\Sessions\Sessions.xml"
                        if (Test-Path $SessionsXmlWinPE) {
                            [xml]$XmlDocument = Get-Content -Path $SessionsXmlWinPE
                            if ($XmlDocument.Sessions.Session.Tasks.Phase.package | Where-Object {$_.Name -like "*$($Update.KBNumber)*" -and $_.targetState -eq 'Installed'}) {
                                Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                            } else {
                                Add-WindowsPackage -Path "$MountWinPE" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-winpe.wim.log"
                                Dism /Image:"$MountWinPE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winpe.wim.log"
                            }
                        } else {
                            Add-WindowsPackage -Path "$MountWinPE" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-winpe.wim.log"
                            Dism /Image:"$MountWinPE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winpe.wim.log"
                        }
                        Write-Host "winre.wim: $UpdateCU"
                        #if (Get-WindowsPackage -Path "$MountWinRE" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {}
                        $SessionsXmlWinRE = "$MountWinRE\Windows\Servicing\Sessions\Sessions.xml"
                        if (Test-Path $SessionsXmlWinRE) {
                            [xml]$XmlDocument = Get-Content -Path $SessionsXmlWinRE
                            if ($XmlDocument.Sessions.Session.Tasks.Phase.package | Where-Object {$_.Name -like "*$($Update.KBNumber)*" -and $_.targetState -eq 'Installed'}) {
                                Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                            } else {
                                Add-WindowsPackage -Path "$MountWinRE" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-winre.wim.log"
                                Dism /Image:"$MountWinRE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winre.wim.log"
                            }
                        } else {
                            Add-WindowsPackage -Path "$MountWinRE" -PackagePath "$UpdateCU" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-winre.wim.log"
                            Dism /Image:"$MountWinRE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winre.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateCU"
                    }
                }
            }
            #======================================================================================
            # WinPE Phase: WinPE DaRT 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Microsoft DaRT" -ForegroundColor Yellow
            if ($WinPEDaRT) {
                if ([string]::IsNullOrEmpty($WinPEDaRT) -or [string]::IsNullOrWhiteSpace($WinPEDaRT)) {Write-Warning "Skipping WinPE DaRT"}
                elseif (Test-Path $WinPEDaRT) {
                    #======================================================================================
                    if (Test-Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig.dat')) {
                        Write-Host "winpe.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountWinPE"
                        if (Test-Path "$MountWinPE\Windows\System32\winpeshl.ini") {Remove-Item -Path "$MountWinPE\Windows\System32\winpeshl.ini" -Force}
                        #======================================================================================
                        Write-Host "setup.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountSetup"
                        if (Test-Path "$MountSetup\Windows\System32\winpeshl.ini") {Remove-Item -Path "$MountSetup\Windows\System32\winpeshl.ini" -Force}
                        #======================================================================================
                        Write-Host "winre.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountWinRE"
                        (Get-Content "$MountWinRE\Windows\System32\winpeshl.ini") | ForEach-Object {$_ -replace '-prompt','-network'} | Out-File "$MountWinRE\Windows\System32\winpeshl.ini"
                        #======================================================================================
                        Write-Host "winpe.wim: Copying DartConfig.dat to $MountWinPE\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig.dat') -Destination "$MountWinPE\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                        Write-Host "setup.wim: Copying DartConfig.dat to $MountSetup\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig.dat') -Destination "$MountSetup\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                        Write-Host "winre.wim: Copying DartConfig.dat to $MountWinRE\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig.dat') -Destination "$MountWinRE\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                    } elseif (Test-Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig8.dat')) {
                        Write-Host "winpe.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountWinPE"
                        if (Test-Path "$MountSetup\Windows\System32\winpeshl.ini") {Remove-Item -Path "$MountSetup\Windows\System32\winpeshl.ini" -Force}
                        #======================================================================================
                        Write-Host "setup.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountSetup"
                        if (Test-Path "$MountSetup\Windows\System32\winpeshl.ini") {Remove-Item -Path "$MountSetup\Windows\System32\winpeshl.ini" -Force}
                        #======================================================================================
                        Write-Host "winre.wim: $WinPEDaRT"
                        expand.exe "$WinPEDaRT" -F:*.* "$MountWinRE"
                        (Get-Content "$MountWinRE\Windows\System32\winpeshl.ini") | ForEach-Object {$_ -replace '-prompt','-network'} | Out-File "$MountWinRE\Windows\System32\winpeshl.ini"
                        #======================================================================================
                        Write-Host "winpe.wim: Copying DartConfig8.dat to $MountWinPE\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig8.dat') -Destination "$MountWinPE\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                        Write-Host "winpe.wim: Copying DartConfig8.dat to $MountSetup\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig8.dat') -Destination "$MountSetup\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                        Write-Host "winre.wim: Copying DartConfig8.dat to $MountWinRE\Windows\System32\DartConfig.dat"
                        Copy-Item -Path $(Join-Path $(Split-Path $WinPEDart) 'DartConfig8.dat') -Destination "$MountWinRE\Windows\System32\DartConfig.dat" -Force | Out-Null
                        #======================================================================================
                    }
                    #======================================================================================
                } else {Write-Warning "WinPE DaRT do not exist in $WinPEDaRT"}
            }
            #======================================================================================
            # WinPE Phase: Extra Files 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Setup WIM Extra Files" -ForegroundColor Yellow
            foreach ($ExtraFile in $SetupWimExtra) {robocopy "$ExtraFile" "$MountSetup" *.* /e /ndl /xx /b /np /ts /tee /r:0 /w:0 /log:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-ExtraFiles-setup.wim.log"}
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinPE WIM Extra Files" -ForegroundColor Yellow
            foreach ($ExtraFile in $WinPEWimExtra) {robocopy "$ExtraFile" "$MountWinPE" *.* /e /ndl /xx /b /np /ts /tee /r:0 /w:0 /log:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-ExtraFiles-winpe.wim.log"}
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinRE WIM Extra Files" -ForegroundColor Yellow
            foreach ($ExtraFile in $WinREWimExtra) {robocopy "$ExtraFile" "$MountWinRE" *.* /e /ndl /xx /b /np /ts /tee /r:0 /w:0 /log:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-ExtraFiles-winre.wim.log"}
            #======================================================================================
            # WinPE Phase: Drivers 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Setup WIM Drivers" -ForegroundColor Yellow
            foreach ($WinPEDriver in $WinPEDrivers) {
                Write-Host "$WinPEPath\Drivers\$($WinPEDriver)" -ForegroundColor Cyan
                Add-WindowsDriver -Path "$MountSetup" -Driver "$WinPEPath\Drivers\$($WinPEDriver)" -Recurse -ForceUnsigned -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsDriver-setup.wim.log" | Out-Null
            }
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinPE WIM Drivers" -ForegroundColor Yellow
            foreach ($WinPEDriver in $WinPEDrivers) {
                Write-Host "$WinPEPath\Drivers\$($WinPEDriver)" -ForegroundColor Cyan
                Add-WindowsDriver -Path "$MountWinPE" -Driver "$WinPEPath\Drivers\$($WinPEDriver)" -Recurse -ForceUnsigned -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsDriver-winpe.wim.log" | Out-Null
            }
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinRE WIM Drivers" -ForegroundColor Yellow
            foreach ($WinPEDriver in $WinPEDrivers) {
                Write-Host "$WinPEPath\Drivers\$($WinPEDriver)" -ForegroundColor Cyan
                Add-WindowsDriver -Path "$MountWinRE" -Driver "$WinPEPath\Drivers\$($WinPEDriver)" -Recurse -ForceUnsigned -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsDriver-winre.wim.log" | Out-Null
            }
            #======================================================================================
            # WinPE Phase: Image Cleanup 18.9.10
            #======================================================================================
<# Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Image Cleanup" -ForegroundColor Yellow
            Dism /Image:"$MountSetup" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-setup.wim.log"
            Dism /Image:"$MountWinPE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winpe.wim.log"
            Dism /Image:"$MountWinRE" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image-winre.wim.log" #>

            #======================================================================================
            # WinPE Phase: WinPE Update Sources 18.9.10
            #======================================================================================
<# Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Updating OS Sources" -ForegroundColor Yellow
            robocopy "$MountSetup" "$OS" setup.exe /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Setup-setup.wim.log"
            robocopy "$MountSetup\sources" "$OS\sources" *.* /e /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Sources-setup.wim.log"
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Updating Setup.wim Sources" -ForegroundColor Yellow
            robocopy "$OS" "$MountSetup" setup.exe /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Setup-osmedia.log"
            robocopy "$OS\sources" "$MountSetup\sources" *.* /e /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Sources-osmedia.log" #>

            #======================================================================================
            # WinPE Phase: PowerShell Scripts 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Setup WIM PowerShell Scripts" -ForegroundColor Yellow
            foreach ($PSWimScript in $SetupWimScripts) {
                if (Test-Path $PSWimScript) {
                    Write-Host "Setup WIM: $PSWimScript" -ForegroundColor Cyan
                    Invoke-Expression "& '$PSWimScript'"
                }
            }
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinPE WIM PowerShell Scripts" -ForegroundColor Yellow
            foreach ($PSWimScript in $WinPEWimScripts) {
                if (Test-Path $PSWimScript) {
                    Write-Host "WinPE WIM: $PSWimScript" -ForegroundColor Cyan
                    Invoke-Expression "& '$PSWimScript'"
                }
            }
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: WinRE WIM PowerShell Scripts" -ForegroundColor Yellow
            foreach ($PSWimScript in $WinREWimScripts) {
                if (Test-Path $PSWimScript) {
                    Write-Host "WinRE WIM: $PSWimScript" -ForegroundColor Cyan
                    Invoke-Expression "& '$PSWimScript'"
                }
            }
            #======================================================================================
            # WinPE Mounted Package Inventory 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Exporting Package Inventory" -ForegroundColor Yellow
            Write-Host "$PEInfo\setup-WindowsPackage.txt"
            $GetWindowsPackage = Get-WindowsPackage -Path "$MountSetup"
            $GetWindowsPackage | Out-File "$PEInfo\setup-WindowsPackage.txt"
            $GetWindowsPackage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-setup.wim.txt"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\Get-WindowsPackage-setup.wim.xml"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-setup.wim.xml"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsPackage-setup.wim.json"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-setup.wim.json"

            Write-Host "$PEInfo\winpe-WindowsPackage.txt"
            $GetWindowsPackage = Get-WindowsPackage -Path "$MountWinPE"
            $GetWindowsPackage | Out-File "$PEInfo\winpe-WindowsPackage.txt"
            $GetWindowsPackage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winpe.wim.txt"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\Get-WindowsPackage-winpe.wim.xml"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winpe.wim.xml"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsPackage-winpe.wim.json"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winpe.wim.json"

            Write-Host "$PEInfo\winre-WindowsPackage.txt"
            $GetWindowsPackage = Get-WindowsPackage -Path "$MountWinRE"
            $GetWindowsPackage | Out-File "$PEInfo\winre-WindowsPackage.txt"
            $GetWindowsPackage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winre.wim.txt"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\Get-WindowsPackage-winre.wim.xml"
            $GetWindowsPackage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winre.wim.xml"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsPackage-winre.wim.json"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage-winre.wim.json"
            #======================================================================================
            # WinPE Dismount and Save 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Dismount and Save" -ForegroundColor Yellow
            Write-Host "setup.wim: Dismount and Save $MountSetup"
            Dismount-WindowsImage -Path "$MountSetup" -Save -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dismount-WindowsImage-setup.wim.log" | Out-Null
            Write-Host "winpe.wim: Dismount and Save $MountWinPE"
            Dismount-WindowsImage -Path "$MountWinPE" -Save -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dismount-WindowsImage-winpe.wim.log" | Out-Null
            Write-Host "winre.wim: Dismount and Save $MountWinRE"
            Dismount-WindowsImage -Path "$MountWinRE" -Save -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dismount-WindowsImage-winre.wim.log" | Out-Null
            #======================================================================================
            # Export WinPE 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Exporting WinPE WIMs" -ForegroundColor Yellow
            Write-Host "setup.wim: Exporting to $WinPE\setup.wim"
            Export-WindowsImage -SourceImagePath "$WimTemp\setup.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\setup.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-setup.wim.log" | Out-Null
            Write-Host "winpe.wim: Exporting to $WinPE\winpe.wim"
            Export-WindowsImage -SourceImagePath "$WimTemp\winpe.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\winpe.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-winpe.wim.log" | Out-Null
            Write-Host "winre.wim: Exporting to $WinPE\winre.wim"
            Export-WindowsImage -SourceImagePath "$WimTemp\winre.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\winre.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-winre.wim.log" | Out-Null
            #======================================================================================
            # Rebuild Boot.wim 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "WinPE Phase: Rebuilding Boot.wim" -ForegroundColor Yellow
            Write-Host "Rebuilding updated Boot.wim Index 1 at $WinPE\boot.wim"
            Export-WindowsImage -SourceImagePath "$WimTemp\winpe.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\boot.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-boot.wim.log" | Out-Null
            Write-Host "Rebuilding updated Boot.wim Index 2 at $WinPE\boot.wim Bootable"
            Export-WindowsImage -SourceImagePath "$WimTemp\setup.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\boot.wim" -Setbootable -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-boot.wim.log" | Out-Null
            Write-Host "Copying Boot.wim to $OS\sources\boot.wim"
            Copy-Item -Path "$WinPE\boot.wim" -Destination "$OS\sources\boot.wim" -Force | Out-Null
            #======================================================================================
            # Mount Install.wim 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Mounting Operating System" -ForegroundColor Yellow
            Write-Host $MountDirectory
            Mount-WindowsImage -ImagePath "$WimTemp\install.wim" -Index 1 -Path "$MountDirectory" -Optimize -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Mount-WindowsImage.log"
            #======================================================================================
            # Get Registry and UBR 18.9.20
            #======================================================================================
            reg LOAD 'HKLM\OSMedia' "$MountDirectory\Windows\System32\Config\SOFTWARE"
            $RegCurrentVersion = Get-ItemProperty -Path 'HKLM:\OSMedia\Microsoft\Windows NT\CurrentVersion'
            reg UNLOAD 'HKLM\OSMedia'

            $OSVersionNumber = $null
            $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
            $RegCurrentVersionUBR = $($RegCurrentVersion.UBR)
            $UBR = "$OSBuild.$RegCurrentVersionUBR"
            #======================================================================================
            # Export RegCurrentVersion 18.9.20
            #======================================================================================
            $RegCurrentVersion | Out-File "$Info\CurrentVersion.txt"
            $RegCurrentVersion | Out-File "$WorkingPath\CurrentVersion.txt"
            $RegCurrentVersion | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.txt"
            $RegCurrentVersion | Export-Clixml -Path "$LogsXML\CurrentVersion.xml"
            $RegCurrentVersion | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.xml"
            $RegCurrentVersion | ConvertTo-Json | Out-File "$LogsJS\CurrentVersion.json"
            $RegCurrentVersion | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.json"
            #======================================================================================
            # Replace WinRE 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Replacing WinRE.wim" -ForegroundColor Yellow
            Write-Host "Removing existing $MountDirectory\Windows\System32\Recovery\winre.wim"
            if (Test-Path "$MountDirectory\Windows\System32\Recovery\winre.wim") {
                Remove-Item -Path "$MountDirectory\Windows\System32\Recovery\winre.wim" -Force
            }
            #======================================================================================
            Write-Host "Copying WinRE.wim to $MountDirectory\Windows\System32\Recovery\winre.wim"
            Copy-Item -Path "$WinPE\winre.wim" -Destination "$MountDirectory\Windows\System32\Recovery\winre.wim" -Force | Out-Null
            #======================================================================================
            Write-Host "Generating WinRE.wim info"
            $GetWindowsImage = Get-WindowsImage -ImagePath "$WinPE\winre.wim" -Index 1 | Select-Object -Property *
            $GetWindowsImage | Out-File "$PEInfo\winre.txt"
            (Get-Content "$PEInfo\winre.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$PEInfo\winre.txt"
            $GetWindowsImage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winre.wim.txt"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\Get-WindowsImage-winre.wim.xml"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winre.wim.xml"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsImage-winre.wim.json"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winre.wim.json"
            #======================================================================================
            # Install.wim Servicing Update 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Servicing Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateServicing)) {
                foreach ($Update in $UpdateServicing) {
                    $UpdateSSU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateSSU") {
                        Write-Host "install.wim: $UpdateSSU"
                        if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateSSU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateServicing-KB$($Update.KBNumber)-install.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateSSU"
                    }
                }
            }
            #======================================================================================
            # Get UBR (Pre Windows Updates) 18.9.20
            #======================================================================================
<# $DismResults = Invoke-Expression "dism /Image:$MountDirectory /?"
            $UBRPre = "$((((($DismResults -match 'Image Version') -split ' ')[2]) -split '\.')[2]).$((((($DismResults -match 'Image Version') -split ' ')[2]) -split '\.')[3])" #>

            $UBRPre = $UBR
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Update Build Revision $UBRPre (Pre-Windows Updates)"    -ForegroundColor Yellow
            #======================================================================================
            # Install.wim Cumulative Update 18.9.13
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Cumulative Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateCumulative)) {
                foreach ($Update in $UpdateCumulative) {
                    $UpdateCU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateCU") {
                        Write-Host "install.wim: $UpdateCU"
                        $SessionsXmlInstall = "$MountDirectory\Windows\Servicing\Sessions\Sessions.xml"
                        if (Test-Path $SessionsXmlInstall) {
                            [xml]$XmlDocument = Get-Content -Path $SessionsXmlInstall
                            if ($XmlDocument.Sessions.Session.Tasks.Phase.package | Where-Object {$_.Name -like "*$($Update.KBNumber)*" -and $_.targetState -eq 'Installed'}) {
                                Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                            } else {
                                Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateCU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-install.wim.log"
                            }
                        } else {
                            Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateCU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-install.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateCU"
                    }
                }
            }
            #======================================================================================
            # Get Registry and UBR 18.9.20
            #======================================================================================
            reg LOAD 'HKLM\OSMedia' "$MountDirectory\Windows\System32\Config\SOFTWARE"
            $RegCurrentVersion = Get-ItemProperty -Path 'HKLM:\OSMedia\Microsoft\Windows NT\CurrentVersion'
            reg UNLOAD 'HKLM\OSMedia'

            $OSVersionNumber = $null
            $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
            $RegCurrentVersionUBR = $($RegCurrentVersion.UBR)
            $UBR = "$OSBuild.$RegCurrentVersionUBR"
            #======================================================================================
            # Export RegCurrentVersion 18.9.20
            #======================================================================================
            $RegCurrentVersion | Out-File "$Info\CurrentVersion.txt"
            $RegCurrentVersion | Out-File "$WorkingPath\CurrentVersion.txt"
            $RegCurrentVersion | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.txt"
            $RegCurrentVersion | Export-Clixml -Path "$LogsXML\CurrentVersion.xml"
            $RegCurrentVersion | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.xml"
            $RegCurrentVersion | ConvertTo-Json | Out-File "$LogsJS\CurrentVersion.json"
            $RegCurrentVersion | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-CurrentVersion.json"
            #======================================================================================
            # Get UBR (Post Windows Updates) 18.9.20
            #======================================================================================
<# $DismResults = Invoke-Expression "dism /Image:$MountDirectory /?"
            $UBR = "$((((($DismResults -match 'Image Version') -split ' ')[2]) -split '\.')[2]).$((((($DismResults -match 'Image Version') -split ' ')[2]) -split '\.')[3])" #>

            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Update Build Revision $UBR (Post-Windows Updates)"    -ForegroundColor Yellow
            #======================================================================================
            # Install.wim Component Update 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Component Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateComponent)) {
                foreach ($Update in $UpdateComponent) {
                    $UpdateComp = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateComp") {
                        Write-Host "install.wim: $UpdateComp"
                        if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateComp" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateComponent-KB$($Update.KBNumber)-install.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateComp"
                    }
                }
            }
            #======================================================================================
            # Install.wim Adobe Update 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Adobe Update" -ForegroundColor Yellow
            if (!($null -eq $UpdateAdobe)) {
                foreach ($Update in $UpdateAdobe) {
                    $UpdateA = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (Test-Path "$UpdateA") {
                        Write-Host "install.wim: $UpdateA"
                        if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                            Write-Warning "KB$($Update.KBNumber) Installed ... Skipping Update"
                        } else {
                            Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateA" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateAdobe-KB$($Update.KBNumber)-install.wim.log"
                        }
                    } else {
                        Write-Warning "Not Found: $UpdateA"
                    }
                }
            }
            #======================================================================================
            # Install.wim Image Cleanup 18.9.10
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Windows Image Cleanup" -ForegroundColor Yellow
            if ($(Get-WindowsCapability -Path $MountDirectory | Where-Object {$_.state -eq "*pending*"})) {
                Write-Warning "Cannot run WindowsImage Cleanup on a WIM with Pending Installations"
            } else {
                Write-Host "Performing Image Cleanup on $MountDirectory"
                Dism /Image:"$MountDirectory" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image.log"
            }
            #======================================================================================
            # Install.wim Update Media Sources 18.9.10
            #======================================================================================
<# Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Updating OS Sources" -ForegroundColor Yellow
            robocopy "$MountDirectory\Windows\System32" "$OS\sources" *.* /e /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Sources-install.wim.log" #>

            #======================================================================================
            # OSBuild Language Packs
            #======================================================================================
            if ($LanguagePacks) {
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Language Packs"    -ForegroundColor Yellow
                # https://docs.microsoft.com/en-us/powershell/module/dism/add-windowspackage?view=win10-ps
                foreach ($PackagePath in $LanguagePacks) {
                    Write-Host $PackagePath
                    Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage.log" | Out-Null
                }
                #======================================================================================
                # Install.wim Cumulative Update 18.9.10
                #======================================================================================
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Cumulative Update" -ForegroundColor Yellow
                if (!($null -eq $UpdateCumulative)) {
                    foreach ($Update in $UpdateCumulative) {
                        $UpdateCU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                        if (Test-Path "$UpdateCU") {
                            Write-Host "install.wim: $UpdateCU"
                            if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                                #Write-Warning "KB$($Update.KBNumber) previously installed"
                            } else {
                                Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateCU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-install.wim.log"
                            }
                        } else {
                            Write-Warning "Not Found: $UpdateCU"
                        }
                    }
                }
                #======================================================================================
                # Windows Image Cleanup
                #======================================================================================
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Windows Image Cleanup" -ForegroundColor Yellow
                if ($(Get-WindowsCapability -Path $MountDirectory | Where-Object {$_.state -like "*pending*"})) {
                    Write-Warning "Cannot run WindowsImage Cleanup on a WIM with Pending Installations"
                } else {
                    Write-Host "Performing Image Cleanup on $MountDirectory"
                    Dism /Image:"$MountDirectory" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image.log"
                }
            }
            #======================================================================================
            # OSBuild Windows Optional Features
            #======================================================================================
            if ($EnableFeature) {
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Enable Windows Optional Feature"    -ForegroundColor Yellow
                foreach ($FeatureName in $EnableFeature) {
                    Write-Host $FeatureName
                    Enable-WindowsOptionalFeature -FeatureName $FeatureName -Path "$MountDirectory" -All -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Enable-WindowsOptionalFeature.log" | Out-Null
                }
                #======================================================================================
                # Install.wim Cumulative Update 18.9.10
                #======================================================================================
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Cumulative Update" -ForegroundColor Yellow
                if (!($null -eq $UpdateCumulative)) {
                    foreach ($Update in $UpdateCumulative) {
                        $UpdateCU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                        if (Test-Path "$UpdateCU") {
                            Write-Host "install.wim: $UpdateCU"
                            if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                                #Write-Warning "KB$($Update.KBNumber) previously installed"
                            } else {
                                Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateCU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-install.wim.log"
                            }
                        } else {
                            Write-Warning "Not Found: $UpdateCU"
                        }
                    }
                }
                #======================================================================================
                # Windows Image Cleanup
                #======================================================================================
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Windows Image Cleanup" -ForegroundColor Yellow
                if ($(Get-WindowsCapability -Path $MountDirectory | Where-Object {$_.state -eq "*pending*"})) {
                    Write-Warning "Cannot run WindowsImage Cleanup on a WIM with Pending Installations"
                } else {
                    Write-Host "Performing Image Cleanup on $MountDirectory"
                    Dism /Image:"$MountDirectory" /Cleanup-Image /StartComponentCleanup /ResetBase /LogPath:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dism-Cleanup-Image.log"
                }
            }
            #======================================================================================
            # OSBuild EnableNetFX3
            #======================================================================================
            if ($EnableNetFX3 -eq 'True') {
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Enable NetFX 3.5"    -ForegroundColor Yellow
                Enable-WindowsOptionalFeature -Path "$MountDirectory" -FeatureName NetFX3 -All -LimitAccess -Source "$OS\sources\sxs" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-NetFX3.log"
                
                #======================================================================================
                # Install.wim Cumulative Update 18.9.10
                #======================================================================================
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Host "Install.wim Phase: Cumulative Update" -ForegroundColor Yellow
                if (!($null -eq $UpdateCumulative)) {
                    foreach ($Update in $UpdateCumulative) {
                        $UpdateCU = $(Get-ChildItem -Path $UpdatesPath -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                        if (Test-Path "$UpdateCU") {
                            Write-Host "install.wim: $UpdateCU"
                            if (Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*$($Update.KBNumber)*"}) {
                                #Write-Warning "KB$($Update.KBNumber) previously installed"
                            } else {
                                Add-WindowsPackage -Path "$MountDirectory" -PackagePath "$UpdateCU" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-UpdateCumulative-KB$($Update.KBNumber)-install.wim.log"
                            }
                        } else {
                            Write-Warning "Not Found: $UpdateCU"
                        }
                    }
                }
            }
            #======================================================================================
            # Update OSMedia Sources with Install.wim
            #======================================================================================
<# Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Updating OS Sources" -ForegroundColor Yellow
            robocopy "$MountDirectory\Windows\System32" "$OS\sources" *.* /e /ndl /xo /xx /xl /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Robocopy-Sources-install.wim.log" #>

            #======================================================================================
            # Remove Appx Packages
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Remove Appx Packages"    -ForegroundColor Yellow
            if ($RemoveAppx) {
                foreach ($item in $RemoveAppx) {
                    Write-Host $item
                    Remove-AppxProvisionedPackage -Path "$MountDirectory" -PackageName $item -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Remove-AppxProvisionedPackage.log" | Out-Null
                }
            }
            #======================================================================================
            # Remove Packages
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Remove Windows Packages"    -ForegroundColor Yellow
            if ($RemovePackages) {
                foreach ($PackageName in $RemovePackages) {
                    Write-Host $item
                    Remove-WindowsPackage -Path "$MountDirectory" -PackageName $PackageName -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Remove-WindowsPackage.log" | Out-Null
                }
            }
            #======================================================================================
            # Remove Capability
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Remove Windows Capability"    -ForegroundColor Yellow
            if ($RemoveCapability) {
                foreach ($Name in $RemoveCapability) {
                    Write-Host $Name
                    Remove-WindowsCapability -Path "$MountDirectory" -Name $Name -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Remove-WindowsCapability.log" | Out-Null
                }
            }
            #======================================================================================
            # Disable Windows Optional Feature
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Disable Windows Optional Feature" -ForegroundColor Yellow
            if ($DisableFeature) {
                foreach ($FeatureName in $DisableFeature) {
                    Write-Host $FeatureName
                    Disable-WindowsOptionalFeature -FeatureName $FeatureName -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Disable-WindowsOptionalFeature.log" | Out-Null
                }
            }
            #======================================================================================
            # Add Packages
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Add Packages"    -ForegroundColor Yellow
            if ($Packages) {
                foreach ($PackagePath in $Packages) {
                    Write-Host $PackagePath
                    Add-WindowsPackage -PackagePath "$PackagePath" -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsPackage.log" | Out-Null
                }
            }
            #======================================================================================
            # Add Drivers
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Add Drivers"    -ForegroundColor Yellow
            if ($Drivers) {
                foreach ($Driver in $Drivers) {
                    Write-Host $DriversPath\$Driver
                    Add-WindowsDriver -Driver "$DriversPath\$Driver" -Recurse -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Add-WindowsDriver.log" | Out-Null
                }
            }
            #======================================================================================
            # Extra Files
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Extra Files"    -ForegroundColor Yellow
            if ($ExtraFiles) {
                foreach ($ExtraFile in $ExtraFiles) {
                    Write-Host $ExtraFile
                    robocopy "$ExtraFile" "$MountDirectory" *.* /e /ndl /xx /b /np /ts /tee /r:0 /w:0 /log:"$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-ExtraFiles.log"
                }
            }
            #======================================================================================
            # Start Layout
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Start Layout"    -ForegroundColor Yellow
            if ($StartLayout) {
                Write-Host $StartLayout
                Copy-Item -Path "$StartLayout" -Destination "$MountDirectory\Users\Default\AppData\Local\Microsoft\Windows\Shell\LayoutModification.xml" -Recurse -Force | Out-Null
            }
            #======================================================================================
            # Unattend.xml
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Unattend.xml"    -ForegroundColor Yellow
            if ($Unattend) {
                Write-Host "$Unattend"
                Use-WindowsUnattend -UnattendPath "$Unattend" -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Use-WindowsUnattend.log" | Out-Null
            }
            #======================================================================================
            # PowerShell Scripts
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: PowerShell Scripts" -ForegroundColor Yellow
            foreach ($PSScript in $Scripts) {
                if (Test-Path $PSScript) {
                    Write-Host "Install WIM: $PSScript" -ForegroundColor Cyan
                    Invoke-Expression "& '$PSScript'"
                }
            }
            #======================================================================================
            # Saving Mounted Windows Image Configuration
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Saving Mounted Windows Image Configuration" -ForegroundColor Yellow
            #======================================================================================
            if ($MediaName -notlike "*server*") {
                Write-Host "$WorkingPath\AppxProvisionedPackage.txt"
                $GetAppxProvisionedPackage = Get-AppxProvisionedPackage -Path "$MountDirectory"
                $GetAppxProvisionedPackage | Out-File "$Info\Get-AppxProvisionedPackage.txt"
                $GetAppxProvisionedPackage | Out-File "$WorkingPath\AppxProvisionedPackage.txt"
                $GetAppxProvisionedPackage | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-AppxProvisionedPackage.txt"
                $GetAppxProvisionedPackage | Export-Clixml -Path "$LogsXML\Get-AppxProvisionedPackage.xml"
                $GetAppxProvisionedPackage | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-AppxProvisionedPackage.xml"
                $GetAppxProvisionedPackage | ConvertTo-Json | Out-File "$LogsJS\Get-AppxProvisionedPackage.json"
                $GetAppxProvisionedPackage | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-AppxProvisionedPackage.json"
            }
            Write-Host "$WorkingPath\WindowsOptionalFeature.txt"
            $GetWindowsOptionalFeature = Get-WindowsOptionalFeature -Path "$MountDirectory"
            $GetWindowsOptionalFeature | Out-File "$Info\Get-WindowsOptionalFeature.txt"
            $GetWindowsOptionalFeature | Out-File "$WorkingPath\WindowsOptionalFeature.txt"
            $GetWindowsOptionalFeature | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsOptionalFeature.txt"
            $GetWindowsOptionalFeature | Export-Clixml -Path "$LogsXML\Get-WindowsOptionalFeature.xml"
            $GetWindowsOptionalFeature | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsOptionalFeature.xml"
            $GetWindowsOptionalFeature | ConvertTo-Json | Out-File "$LogsJS\Get-WindowsOptionalFeature.json"
            $GetWindowsOptionalFeature | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsOptionalFeature.json"

            Write-Host "$WorkingPath\WindowsCapability.txt"
            $GetWindowsCapability = Get-WindowsCapability -Path "$MountDirectory"
            $GetWindowsCapability | Out-File "$Info\Get-WindowsCapability.txt"
            $GetWindowsCapability | Out-File "$WorkingPath\WindowsCapability.txt"
            $GetWindowsCapability | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsCapability.txt"
            $GetWindowsCapability | Export-Clixml -Path "$LogsXML\Get-WindowsCapability.xml"
            $GetWindowsCapability | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsCapability.xml"
            $GetWindowsCapability | ConvertTo-Json | Out-File "$LogsJS\Get-WindowsCapability.json"
            $GetWindowsCapability | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsCapability.json"

            Write-Host "$WorkingPath\WindowsPackage.txt"
            $GetWindowsPackage = Get-WindowsPackage -Path "$MountDirectory"
            $GetWindowsPackage | Out-File "$Info\Get-WindowsPackage.txt"
            $GetWindowsPackage | Out-File "$WorkingPath\WindowsPackage.txt"
            $GetWindowsPackage | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage.txt"
            $GetWindowsPackage | Export-Clixml -Path "$LogsXML\Get-WindowsPackage.xml"
            $GetWindowsPackage | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage.xml"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$LogsJS\Get-WindowsPackage.json"
            $GetWindowsPackage | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsPackage.json"
            #======================================================================================
            # Dismount and Save
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Dismount and Save" -ForegroundColor Yellow
            Write-Host "Dismount and Save $MountDirectory"
            Dismount-WindowsImage -Path "$MountDirectory" -Save -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dismount-WindowsImage.log"
            #======================================================================================
            # Export Install.wim
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Install.wim Phase: Export Install.wim" -ForegroundColor Yellow
            Write-Host "Exporting $WimTemp\install.wim"
            Export-WindowsImage -SourceImagePath "$WimTemp\install.wim" -SourceIndex 1 -DestinationImagePath "$OS\sources\install.wim" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage.log"
            #======================================================================================
            # Saving WinPE Image Configuration
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Inventory Phase: Saving WinPE Image Configuration" -ForegroundColor Yellow
            #======================================================================================
            # Get-WindowsImage Boot.wim
            #======================================================================================
            Write-Host "$PEInfo\boot.txt"
            $GetWindowsImage = Get-WindowsImage -ImagePath "$OS\sources\boot.wim"
            $GetWindowsImage | Out-File "$PEInfo\boot.txt"
            (Get-Content "$PEInfo\boot.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$PEInfo\boot.txt"
            $GetWindowsImage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-boot.wim.txt"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\Get-WindowsImage-boot.wim.xml"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-boot.wim.xml"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsImage-boot.wim.json"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-boot.wim.json"
            #======================================================================================
            # Get-WindowsImage WinPE
            #======================================================================================
            Write-Host "$PEInfo\winpe.txt"
            $GetWindowsImage = Get-WindowsImage -ImagePath "$OS\sources\boot.wim" -Index 1 | Select-Object -Property *
            $GetWindowsImage | Out-File "$PEInfo\winpe.txt"
            (Get-Content "$PEInfo\winpe.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$PEInfo\winpe.txt"
            $GetWindowsImage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winpe.wim.txt"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\Get-WindowsImage-winpe.wim.xml"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winpe.wim.xml"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsImage-winpe.wim.json"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-winpe.wim.json"
            #======================================================================================
            # Get-WindowsImage Setup
            #======================================================================================
            Write-Host "$PEInfo\setup.txt"
            $GetWindowsImage = Get-WindowsImage -ImagePath "$OS\sources\boot.wim" -Index 2 | Select-Object -Property *
            $GetWindowsImage | Out-File "$PEInfo\setup.txt"
            (Get-Content "$PEInfo\setup.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$PEInfo\setup.txt"
            $GetWindowsImage | Out-File "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-setup.wim.txt"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\Get-WindowsImage-setup.wim.xml"
            $GetWindowsImage | Export-Clixml -Path "$PELogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-setup.wim.xml"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\Get-WindowsImage-setup.wim.json"
            $GetWindowsImage | ConvertTo-Json | Out-File "$PELogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage-setup.wim.json"
            #======================================================================================
            # Saving Windows Image Configuration
            #======================================================================================
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Inventory Phase: Saving Windows Image Configuration" -ForegroundColor Yellow
            Write-Host "$WorkingPath\WindowsImage.txt"
            $GetWindowsImage = Get-WindowsImage -ImagePath "$OS\sources\install.wim" -Index 1 | Select-Object -Property *
            $GetWindowsImage | Add-Member -Type NoteProperty -Name "UBR" -Value $UBR
            $GetWindowsImage | Out-File "$WorkingPath\WindowsImage.txt"
            $GetWindowsImage | Out-File "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.txt"
            $GetWindowsImage | Export-Clixml -Path "$LogsXML\Get-WindowsImage.xml"
            $GetWindowsImage | Export-Clixml -Path "$LogsXML\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.xml"
            $GetWindowsImage | ConvertTo-Json | Out-File "$LogsJS\Get-WindowsImage.json"
            $GetWindowsImage | ConvertTo-Json | Out-File "$LogsJS\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.json"
            (Get-Content "$WorkingPath\WindowsImage.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$WorkingPath\WindowsImage.txt"
            #======================================================================================
            # Get-WindowsImageContent
            #======================================================================================
            Write-Host "$Info\Get-WindowsImageContent.txt"  
            Get-WindowsImageContent -ImagePath "$OS\Sources\install.wim" -Index 1 | Out-File "$Info\Get-WindowsImageContent.txt"
            #======================================================================================
            # Display OS Information
            #======================================================================================
            Show-OSInfo $WorkingPath
            #======================================================================================
            # Remove Temporary Files 18.9.13
            #======================================================================================
            if (Test-Path "$WimTemp") {Remove-Item -Path "$WimTemp" -Force -Recurse | Out-Null}
            if (Test-Path "$MountDirectory") {Remove-Item -Path "$MountDirectory" -Force -Recurse | Out-Null}
            if (Test-Path "$MountWinRE") {Remove-Item -Path "$MountWinRE" -Force -Recurse | Out-Null}
            if (Test-Path "$MountWinPE") {Remove-Item -Path "$MountWinPE" -Force -Recurse | Out-Null}
            if (Test-Path "$MountSetup") {Remove-Item -Path "$MountSetup" -Force -Recurse | Out-Null}
            #======================================================================================
            # UBR Validation 18.9.12
            #======================================================================================
            if ($UBRPre -eq $UBR) {
                Write-Host "===========================================================================" -ForegroundColor Yellow
                Write-Warning "The Update Build Revision did not change after Windows Updates"
                Write-Warning "There may have been an issue applying the Cumulative Update if this was not expected"
            }
            if (!($UBR)) {$UBR = $((Get-Date).ToString('mmss'))}
            #======================================================================================
            # Set New Name 18.9.12
            #======================================================================================
            $OSImageName = $($GetWindowsImage.ImageName)
            $OSImageName = $OSImageName -replace "Windows 10", "Win10"
            $OSImageName = $OSImageName -replace "Enterprise", "Ent"
            $OSImageName = $OSImageName -replace "Education", "Edu"
            $OSImageName = $OSImageName -replace " for ", " "
            $OSImageName = $OSImageName -replace "Workstations", "Wks"
            $OSImageName = $OSImageName -replace "Windows Server 2016", "Svr2016"
            $OSImageName = $OSImageName -replace "ServerStandardACore", "Std Core"
            $OSImageName = $OSImageName -replace "ServerDatacenterACore", "DC Core"
            $OSImageName = $OSImageName -replace "ServerStandardCore", "Std Core"
            $OSImageName = $OSImageName -replace "ServerDatacenterCore", "DC Core"
            $OSImageName = $OSImageName -replace "ServerStandard", "Std"
            $OSImageName = $OSImageName -replace "ServerDatacenter", "DC"
            $OSImageName = $OSImageName -replace "Standard", "Std"
            $OSImageName = $OSImageName -replace "Datacenter", "DC"
            $OSImageName = $OSImageName -replace 'Desktop Experience', 'DTE'
            $OSImageName = $OSImageName -replace '\(', ''
            $OSImageName = $OSImageName -replace '\)', ''

            $OSArchitecture = $($GetWindowsImage.Architecture)
            if ($OSArchitecture -eq 0) {$OSArchitecture = 'x86'}
            elseif ($OSArchitecture -eq 1) {$OSArchitecture = 'MIPS'}
            elseif ($OSArchitecture -eq 2) {$OSArchitecture = 'Alpha'}
            elseif ($OSArchitecture -eq 3) {$OSArchitecture = 'PowerPC'}
            elseif ($OSArchitecture -eq 6) {$OSArchitecture = 'ia64'}
            elseif ($OSArchitecture -eq 9) {$OSArchitecture = 'x64'}

            $OSBuild = $($GetWindowsImage.Build)
            $OSVersionNumber = $null
            if (Test-Path "$LogsXML\CurrentVersion.xml") {
                $RegCurrentVersion = Import-Clixml -Path "$LogsXML\CurrentVersion.xml"
                $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
            } else {
                if ($OSBuild -eq 17134) {$OSVersionNumber = 1803}
                if ($OSBuild -eq 16299) {$OSVersionNumber = 1709}
                if ($OSBuild -eq 15063) {$OSVersionNumber = 1703}
                if ($OSBuild -eq 14393) {$OSVersionNumber = 1607}
                if ($OSBuild -eq 10240) {$OSVersionNumber = 1507}
            }

            $OSLanguages = $($GetWindowsImage.Languages)
            if ($null -eq $OSVersionNumber ) {
                Write-Host ""
                Write-Warning "OS Build $OSVersionNumber is not automatically recognized"
                Write-Warning "Check for an updated version of OSBuilder"
                Write-Host ""
                if ($BuildName -like "build*") {$BuildName = "$OSImageName $OSArchitecture"}
            } else {
                if ($BuildName -like "build*") {$BuildName = "$OSImageName $OSArchitecture $OSVersionNumber"}
                
            }
            $BuildName = "$BuildName $OSLanguages"
            if ($($OSLanguages.count) -eq 1) {$BuildName = $BuildName.replace(' en-US', '')}
            if ($CustomBuildName) {$BuildName = "$CustomBuildName"}
            $NewWorkingPathName = "$BuildName $UBR"
            $NewWorkingPath = "$Script:OSBuildsPath\$NewWorkingPathName"
            #======================================================================================
            # Rename Build Directory
            #======================================================================================
            if (Test-Path $NewWorkingPath) {
                Write-Host ""
                Write-Warning "Trying to rename the Build directory, but it already exists"
                Write-Warning "Appending the HHmm to the directory name"
                $NewWorkingPathName = "$NewWorkingPathName $((Get-Date).ToString('mmss'))"
            }
            Write-Host "===========================================================================" -ForegroundColor Yellow
            Write-Host "Closing Phase: Renaming ""$WorkingPath"" to ""$NewWorkingPathName""" -ForegroundColor Yellow
            Stop-Transcript
            Rename-Item -Path "$WorkingPath" -NewName "$NewWorkingPathName" -ErrorAction Stop
        }
        Write-Host "===========================================================================" -ForegroundColor Green
        Write-Host "Complete!" -ForegroundColor Green
        Write-Host "===========================================================================" -ForegroundColor Green
    }
}