Public/Update-OSMedia.ps1

<#
.SYNOPSIS
Applies Adobe, Component, Cumulative, Servicing Stack, and Setup Updates to Windows 7, Windows 10, Windows Server 2016, and Windows Server 2019 using Offline Servicing

.DESCRIPTION
Updates are gathered from the OSBuilder Update Catalogs

.LINK
https://www.osdeploy.com/osbuilder/docs/functions/osmedia/update-osmedia

.PARAMETER Name
Enter the name of the existing OSMedia to update

.EXAMPLE
Update-OSMedia -Name 'Win10 Ent x64 1803 17134.345'

.PARAMETER DownloadUpdates
Automatically download the required updates if they are not present in the Content\Updates directory

.EXAMPLE
Update-OSMedia -Name 'Win10 Ent x64 1803 17134.345' -DownloadUpdates

.PARAMETER Execute
Execute the Update

.EXAMPLE
Update-OSMedia -Name 'Win10 Ent x64 1803 17134.345' -DownloadUpdates -Execute

.PARAMETER MediaISO
Creates an ISO of the Updated Media. Requires ADK oscdimg.exe

.EXAMPLE
Update-OSMedia -Name 'Win10 Ent x64 1803 17134.345' -DownloadUpdates -Execute -ISO

.PARAMETER WaitDismount
Adds a 'Press Enter to Continue' prompt before the Install.wim is dismounted

.PARAMETER WaitDismountWinPE
Adds a 'Press Enter to Continue' prompt before WinPE is dismounted

.PARAMETER SkipUpdates
Execute an OSMedia Update without Updates. Useful for Testing
#>

function Update-OSMedia {
    [CmdletBinding(DefaultParameterSetName='Basic')]
    PARAM (
        #==========================================================
        [Parameter(ValueFromPipelineByPropertyName)]
        [string[]]$Name,
        #==========================================================
        [Parameter(ParameterSetName='Basic')]
        [Parameter(ParameterSetName='Advanced')]
        [switch]$DownloadUpdates,

        [Parameter(ParameterSetName='Basic')]
        [Parameter(ParameterSetName='Advanced')]
        [switch]$Execute,

        [Parameter(ParameterSetName='Basic')]
        [Parameter(ParameterSetName='Advanced')]
        [switch]$MediaINFO,
        
        [Parameter(ParameterSetName='Basic')]
        [Parameter(ParameterSetName='Advanced')]
        [switch]$MediaISO,
        #==========================================================
        [Parameter(ParameterSetName='Advanced')]
        [switch]$SkipUpdates,
        [Parameter(ParameterSetName='Advanced')]
        [switch]$WaitDismount,
        [Parameter(ParameterSetName='Advanced')]
        [switch]$WaitDismountWinPE
        #==========================================================
    )

    BEGIN {
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
        #Write-Host "$($MyInvocation.MyCommand.Name) BEGIN" -ForegroundColor Green

        #===================================================================================================
        Write-Verbose '19.1.1 Validate Administrator Rights'
        #===================================================================================================
        if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Warning 'OSBuilder: This function needs to be run as Administrator'
            Return
        }

        #===================================================================================================
        Write-Verbose '19.1.1 Initialize OSBuilder'
        #===================================================================================================
        Get-OSBuilder -CreatePaths -HideDetails

        #===================================================================================================
        Write-Verbose '19.1.1 Update Catalogs'
        #===================================================================================================
        if (!(Test-Path $CatalogLocal)) {$DownloadUpdates = $true}
        if ($DownloadUpdates.IsPresent) {Get-OSBUpdate -UpdateCatalogs -HideDetails}

        #===================================================================================================
        Write-Verbose '19.1.1 Gather All Updates'
        #===================================================================================================
        $CatalogsXmls = @()
        $ImportCatalog = @()
        $AllUpdates = @()

        $CatalogsXmls = Get-ChildItem "$OSBuilderContent\Updates" Cat*.xml -Recurse
        foreach ($CatalogsXml in $CatalogsXmls) {
            $ImportCatalog = Import-Clixml -Path "$($CatalogsXml.FullName)"
            $AllUpdates += $ImportCatalog
        }
        $AllUpdates = $AllUpdates | Select-Object -Property *

        if ($SkipUpdates.IsPresent) {$AllUpdates = @()}
    }

    PROCESS {
        Write-Host '========================================================================================' -ForegroundColor DarkGray
        Write-Host "$($MyInvocation.MyCommand.Name) PROCESS" -ForegroundColor Green
        
        #===================================================================================================
        # OSBuild
        Write-Verbose '19.1.8 Get-OSBuildTask'
        #===================================================================================================
        if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
            if ($PSCmdlet.ParameterSetName -eq 'Taskless') {
                if ($Name) {
                    $BirdBox = foreach ($Task in $Name) {
                        Write-Verbose '========== Checking $Task'
                        $ObjectProperties = @{
                            Name = $Task
                        }
                        New-Object -TypeName PSObject -Property $ObjectProperties
                    }
                } else {
                    $BirdBox = @()
                    $BirdBox = Get-OSMedia
                    $BirdBox = $BirdBox | Where-Object {$_.MajorVersion -eq 10 -or $_.UBR -eq 7601.17514}
                    $BirdBox = $BirdBox | Out-GridView -PassThru -Title "Select one or more OSMedia to Update (Cancel to Exit) and press OK"
                }
                if ($null -eq $BirdBox) {
                    Write-Warning "Could not find a matching OSMedia . . . Exiting!"
                    Return
                }
            } else {
                if ($ByTaskName) {
                    $BirdBox = @()
                    $BirdBox = Get-OSBuildTask | Where-Object {$_.TaskName -eq "$ByTaskName"}
                } else {
                    $BirdBox = @()
                    $BirdBox = Get-OSBuildTask | Out-GridView -PassThru -Title "OSBuild Tasks: Select one or more Tasks to execute and press OK (Cancel to Exit)"
                }

                if ($null -eq $BirdBox) {
                    Write-Warning "OSBuild Task was not selected or found . . . Exiting!"
                    Return
                }
            }
        }

        #===================================================================================================
        Write-Verbose '19.1.26 Get-OSMedia'
        #===================================================================================================
        if ($MyInvocation.MyCommand.Name -eq 'Update-OSMedia') {
            if ($Name) {
                $BirdBox = foreach ($Item in $Name) {
                    Write-Verbose '========== Checking $Item'
                    $ObjectProperties = @{
                        Name = $Item
                    }
                    New-Object -TypeName PSObject -Property $ObjectProperties
                }
            } else {
                $BirdBox = @()
                $BirdBox = Get-OSMedia
                $BirdBox = $BirdBox | Where-Object {$_.MajorVersion -eq 10 -or $_.UBR -eq 7601.17514}
                $BirdBox = $BirdBox | Out-GridView -PassThru -Title "Select one or more OSMedia to Update (Cancel to Exit) and press OK"
            }
        }

        foreach ($Bird in $BirdBox) {
            #===================================================================================================
            # OSBuild
            Write-Verbose '19.1.1 Read Task Contents'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                if ($PSCmdlet.ParameterSetName -eq 'Taskless') {
                    $Task = Get-OSMedia | Where-Object {$_.Name -eq $Bird.Name}

                    $TaskType = 'OSBuild'
                    $TaskName = 'Taskless'
                } else {
                    (Get-Content "$($Bird.FullName)").replace('WinPEAddDaRT', 'WinPEDaRT') | Set-Content "$($Bird.FullName)"
                    $Task = Get-Content "$($Bird.FullName)" | ConvertFrom-Json

                    $TaskType = $Task.TaskType
                    $TaskName = $Task.TaskName
                }
                $TaskVersion = $Task.TaskVersion
                $CustomName = $Task.CustomName

                $TaskOSMFamily = $Task.OSMFamily
                $TaskOSMGuid = $Task.OSMGuid
                $OSMediaName = $Task.Name
                $OSMediaPath = "$OSBuilderOSMedia\$OSMediaName"

                $EnableNetFX3 = $Task.EnableNetFX3
                $StartLayoutXML = $Task.StartLayoutXML
                $UnattendXML = $Task.UnattendXML
                $WinPEAutoExtraFiles = $Task.WinPEAutoExtraFiles
                $WinPEDaRT = $Task.WinPEDart
                
                $ExtraFiles = $Task.ExtraFiles
                $Scripts = $Task.Scripts
                $Drivers = $Task.Drivers

                $Packages = $Task.AddWindowsPackage
                $RemovePackage = $Task.RemoveWindowsPackage
                $FeaturesOnDemand = $Task.AddFeatureOnDemand
                $EnableFeature = $Task.EnableWindowsOptionalFeature
                $DisableFeature = $Task.DisableWindowsOptionalFeature
                $RemoveAppx = $Task.RemoveAppxProvisionedPackage
                $RemoveCapability = $Task.RemoveWindowsCapability

                $WinPEDrivers = $Task.WinPEDrivers
                $WinPEScriptsPE = $Task.WinPEScriptsPE
                $WinPEScriptsRE = $Task.WinPEScriptsRE
                $WinPEScriptsSE = $Task.WinPEScriptsSE
                $WinPEExtraFilesPE = $Task.WinPEExtraFilesPE
                $WinPEExtraFilesRE = $Task.WinPEExtraFilesRE
                $WinPEExtraFilesSE = $Task.WinPEExtraFilesSE
                $WinPEADKPE = $Task.WinPEADKPE
                $WinPEADKRE = $Task.WinPEADKRE
                $WinPEADKSE = $Task.WinPEADKSE
                
                $SetAllIntl = $Task.LangSetAllIntl
                $SetInputLocale = $Task.LangSetInputLocale
                $SetSKUIntlDefaults = $Task.LangSetSKUIntlDefaults
                $SetSetupUILang = $Task.LangSetSetupUILang
                $SetSysLocale = $Task.LangSetSysLocale
                $SetUILang = $Task.LangSetUILang
                $SetUILangFallback = $Task.LangSetUILangFallback
                $SetUserLocale = $Task.LangSetUserLocale
                $LanguageFeatures = $Task.LanguageFeature
                $LanguagePacks = $Task.LanguagePack
                $LanguageInterfacePacks = $Task.LanguageInterfacePack
                $LocalExperiencePacks = $Task.LocalExperiencePacks
                
                if (!($TaskName -eq 'Taskless')) {OSB-Info-TaskInformation}
            }
        
        
            #===================================================================================================
            # OSBuild
            Write-Verbose '19.1.1 Validate Proper TaskVersion'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild' -and (!($TaskName -eq 'Taskless'))) {
                if ([System.Version]$TaskVersion -lt [System.Version]"19.1.4.0") {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Warning "OSBuilder Tasks need to be version 19.1.4.0 or newer"
                    Write-Warning "Recreate this Task using New-OSBuildTask or Repair-OSBuildTask"
                    Return
                }
            }

            #===================================================================================================
            # OSBuild
            Write-Verbose '19.1.8 Select Latest OSMedia'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild' -and (!($DontUseNewestMedia))) {
                if ($TaskName -eq 'Taskless') {
                    $TaskOSMedia = Get-OSMedia | Where-Object {$_.Name -eq $OSMediaName}
                } else {
                    $TaskOSMedia = Get-OSMedia | Where-Object {$_.OSMGuid -eq $TaskOSMGuid}
                }
                
                if ($TaskOSMedia) {
                    $OSMediaName = $TaskOSMedia.Name
                    $OSMediaPath = $TaskOSMedia.FullName
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Host "Task Source OSMedia" -ForegroundColor Green
                    Write-Host "-OSMedia Name: $OSMediaName"
                    Write-Host "-OSMedia Path: $OSMediaPath"
                    Write-Host "-OSMedia Family: $TaskOSMFamily"
                    Write-Host "-OSMedia Guid: $TaskOSMGuid"
                }
                $LatestOSMedia = Get-OSMedia | Where-Object {$_.OSMFamily -eq $TaskOSMFamily} | Sort-Object UBR | Select-Object -Last 1
                if ($LatestOSMedia) {
                    $OSMediaName = $LatestOSMedia.Name
                    $OSMediaPath = $LatestOSMedia.FullName
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Host "Latest Source OSMedia" -ForegroundColor Green
                    Write-Host "-OSMedia Name: $OSMediaName"
                    Write-Host "-OSMedia Path: $OSMediaPath"
                    Write-Host "-OSMedia Family: $($LatestOSMedia.OSMFamily)"
                    Write-Host "-OSMedia Guid: $($LatestOSMedia.OSMGuid)"
                } else {
                    Write-Warning "Unable to find a matching OSMFamily $TaskOSMFamily"
                    Return
                }
            }
            
            #===================================================================================================
            # OSBuild
            Write-Verbose '19.1.22 Templates'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild' -and (Test-Path "$OSBuilderTemplates") -and (!($SkipTemplates.IsPresent))) {
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                Write-Host "OSBuild Templates" -ForegroundColor Green
                Write-Warning "Templates are under Development and may change at any time until finalized"
                Get-ChildItem -Path "$OSBuilderTemplates" *.json | foreach {(Get-Content "$($_.FullName)").replace('WinPEAddDaRT', 'WinPEDaRT') | Set-Content "$($_.FullName)"}
                $Templates = Get-ChildItem -Path "$OSBuilderTemplates" OSBuild*.json | ForEach-Object {Get-Content -Path $_.FullName | ConvertFrom-Json | Select-Object -Property *}

                foreach ($Task in $Templates) {
                    if ($Task.TaskName -like "*Global*") {
                        Write-Host "Global: $($Task.TaskName)"
                    } elseif ($Task.OSMFamily -eq $TaskOSMFamily) {
                        Write-Host "OSMedia Family: $($Task.TaskName)"
                    } else {
                        Write-Host "Skipping: $($Task.TaskName)" -ForegroundColor DarkGray
                        Continue
                    }

                    if (!($Task.EnableNetFX3 -eq $False)) {$EnableNetFX3 = $Task.EnableNetFX3}
                    if ($Task.StartLayoutXML) {$StartLayoutXML = $Task.StartLayoutXML}
                    if ($Task.UnattendXML) {$UnattendXML = $Task.UnattendXML}
                    if (!($Task.WinPEAutoExtraFiles -eq $False)) {$WinPEAutoExtraFiles = $Task.WinPEAutoExtraFiles}
                    if ($Task.WinPEDaRT) {$WinPEDaRT = $Task.WinPEDaRT}
                    
                    $ExtraFiles += @($Task.ExtraFiles | Where-Object {$_})
                    $Scripts += @($Task.Scripts | Where-Object {$_})
                    $Drivers += @($Task.Drivers | Where-Object {$_})
    
                    $Packages += @($Task.AddWindowsPackage | Where-Object {$_})
                    $RemovePackage += @($Task.RemoveWindowsPackage | Where-Object {$_})
                    $FeaturesOnDemand += @($Task.AddFeatureOnDemand | Where-Object {$_})
                    $EnableFeature += @($Task.EnableWindowsOptionalFeature | Where-Object {$_})
                    $DisableFeature += @($Task.DisableWindowsOptionalFeature | Where-Object {$_})
                    $RemoveAppx += @($Task.RemoveAppxProvisionedPackage | Where-Object {$_})
                    $RemoveCapability += @($Task.RemoveWindowsCapability | Where-Object {$_})
    
                    $WinPEDrivers += @($Task.WinPEDrivers | Where-Object {$_})
                    $WinPEScriptsPE += @($Task.WinPEScriptsPE | Where-Object {$_})
                    $WinPEScriptsRE += @($Task.WinPEScriptsRE | Where-Object {$_})
                    $WinPEScriptsSE += @($Task.WinPEScriptsSE | Where-Object {$_})
                    $WinPEExtraFilesPE += @($Task.WinPEExtraFilesPE | Where-Object {$_})
                    $WinPEExtraFilesRE += @($Task.WinPEExtraFilesRE | Where-Object {$_})
                    $WinPEExtraFilesSE += @($Task.WinPEExtraFilesSE | Where-Object {$_})
                    $WinPEADKPE += @($Task.WinPEADKPE | Where-Object {$_})
                    $WinPEADKRE += @($Task.WinPEADKRE | Where-Object {$_})
                    $WinPEADKSE += @($Task.WinPEADKSE | Where-Object {$_})
                    
                    if ($Task.SetAllIntl) {$SetAllIntl = $Task.SetAllIntl}
                    if ($Task.LangSetInputLocale) {$SetInputLocale = $Task.LangSetInputLocale}
                    if ($Task.LangSetSKUIntlDefaults) {$SetSKUIntlDefaults = $Task.LangSetSKUIntlDefaults}
                    if ($Task.LangSetSetupUILang) {$SetSetupUILang = $Task.LangSetSetupUILang}
                    if ($Task.LangSetSysLocale) {$SetSysLocale = $Task.LangSetSysLocale}
                    if ($Task.LangSetUILang) {$SetUILang = $Task.LangSetUILang}
                    if ($Task.LangSetUILangFallback) {$SetUILangFallback = $Task.LangSetUILangFallback}
                    if ($Task.LangSetUserLocale) {$SetUserLocale = $Task.LangSetUserLocale}
                    $LanguageFeatures += @($Task.LanguageFeature | Where-Object {$_})
                    $LanguagePacks += @($Task.LanguagePack | Where-Object {$_})
                    $LanguageInterfacePacks += @($Task.LanguageInterfacePack | Where-Object {$_})
                    $LocalExperiencePacks += @($Task.LocalExperiencePacks | Where-Object {$_})
                }
            }
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                if (!($SkipTemplates.IsPresent)) {OSB-Info-TaskInformation}
            }
            #===================================================================================================
            Write-Verbose '19.1.1 Set Proper Paths'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'Update-OSMedia') {
                $OSMediaPath = "$OSBuilderOSMedia\$($Bird.Name)"
            }
            $OSImagePath = "$OSMediaPath\OS\sources\install.wim"

            if (!(Test-Path "$OSMediaPath\WindowsImage.txt")) {
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                Write-Warning "$OSMediaPath is not a valid OSMedia Directory"
                Return
            }

            if (!(Test-Path "$OSImagePath")) {
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                Write-Warning "$OSImagePath is not a valid Windows Image"
                Return
            }

            #===================================================================================================
            Write-Verbose '19.1.1 Get Windows Image Information'
            #===================================================================================================
            $OSImageIndex = 1
            $WindowsImage = Get-WindowsImage -ImagePath "$OSImagePath" -Index $OSImageIndex | Select-Object -Property *

            $OSImageName = $($WindowsImage.ImageName)
            $OSImageName = $OSImageName -replace '\(', ''
            $OSImageName = $OSImageName -replace '\)', ''

            $OSImageDescription = $($WindowsImage.ImageDescription)

            $OSArchitecture = $($WindowsImage.Architecture)
            if ($OSArchitecture -eq '0') {$OSArchitecture = 'x86'}
            if ($OSArchitecture -eq '6') {$OSArchitecture = 'ia64'}
            if ($OSArchitecture -eq '9') {$OSArchitecture = 'x64'}
            if ($OSArchitecture -eq '12') {$OSArchitecture = 'x64 ARM'}

            $OSEditionID =          $($WindowsImage.EditionId)
            $OSInstallationType =   $($WindowsImage.InstallationType)
            $OSLanguages =          $($WindowsImage.Languages)
            $OSMajorVersion =       $($WindowsImage.MajorVersion)
            $OSBuild =              $($WindowsImage.Build)
            $OSVersion =            $($WindowsImage.Version)
            $OSSPBuild =            $($WindowsImage.SPBuild)
            $OSSPLevel =            $($WindowsImage.SPLevel)
            $OSImageBootable =      $($WindowsImage.ImageBootable)
            $OSWIMBoot =            $($WindowsImage.WIMBoot)
            $OSCreatedTime =        $($WindowsImage.CreatedTime)
            $OSModifiedTime =       $($WindowsImage.ModifiedTime)

            OSB-Info-SourceOSMedia

            #===================================================================================================
            Write-Verbose '19.1.1 Validate Registry CurrentVersion.xml'
            #===================================================================================================
            if (Test-Path "$OSMediaPath\info\xml\CurrentVersion.xml") {
                $RegCurrentVersion = Import-Clixml -Path "$OSMediaPath\info\xml\CurrentVersion.xml"
                $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
                if ($OSVersionNumber -gt 1809) {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Warning "OSBuilder does not currently support this version of Windows ... Check for an updated version"
                }
            }

            #===================================================================================================
            Write-Verbose '19.1.1 Set OSVersionNumber'
            #===================================================================================================
            if ($null -eq $OSVersionNumber) {
                if ($OSBuild -eq 7601) {$OSVersionNumber = 7601}
                if ($OSBuild -eq 10240) {$OSVersionNumber = 1507}
                if ($OSBuild -eq 14393) {$OSVersionNumber = 1607}
                if ($OSBuild -eq 15063) {$OSVersionNumber = 1703}
                if ($OSBuild -eq 16299) {$OSVersionNumber = 1709}
                if ($OSBuild -eq 17134) {$OSVersionNumber = 1803}
                if ($OSBuild -eq 17763) {$OSVersionNumber = 1809}
            }

            #===================================================================================================
            Write-Verbose '19.1.1 WorkingName and WorkingPath'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                $WorkingName = "build$((Get-Date).ToString('mmss'))"
                $WorkingPath = "$OSBuilderOSBuilds\$WorkingName"
            } else {
                $WorkingName = "build$((Get-Date).ToString('mmss'))"
                $WorkingPath = "$OSBuilderOSMedia\$WorkingName"
            }

            #===================================================================================================
            Write-Verbose '19.1.1 Remove Existing OSMedia'
            #===================================================================================================
            if (Test-Path $WorkingPath) {
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                Write-Warning "$WorkingPath will be replaced!"
            }

            #===================================================================================================
            # OSBuild
            Write-Verbose '19.1.25 Disable Windows 7 Updates'
            #===================================================================================================
            if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild' -and $OSVersionNumber -eq 7601) {
                $AllUpdates = @()
            }
            #===================================================================================================
            Write-Verbose '19.1.1 Setup Updates'
            #===================================================================================================
            $UpdateCatSetup = @()
            $UpdateCatSetup = $AllUpdates | Where-Object {$_.Category -eq 'Setup'}
            $UpdateCatSetup = $UpdateCatSetup | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatSetup = $UpdateCatSetup | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatSetup = $UpdateCatSetup | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatSetup = $UpdateCatSetup | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatSetup = $UpdateCatSetup | Sort-Object -Property DatePosted

            #===================================================================================================
            Write-Verbose '19.1.1 Servicing Stack Updates'
            #===================================================================================================
            $UpdateCatServicing = @()
            $UpdateCatServicing = $AllUpdates | Where-Object {$_.Category -eq 'Servicing'}
            $UpdateCatServicing = $UpdateCatServicing | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatServicing = $UpdateCatServicing | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatServicing = $UpdateCatServicing | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatServicing = $UpdateCatServicing | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatServicing = $UpdateCatServicing | Sort-Object -Property DatePosted

            #===================================================================================================
            Write-Verbose '19.1.1 Latest Cumulative Updates'
            #===================================================================================================
            $UpdateCatCumulative = @()
            $UpdateCatCumulative = $AllUpdates | Where-Object {$_.Category -eq 'Cumulative'}
            $UpdateCatCumulative = $UpdateCatCumulative | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatCumulative = $UpdateCatCumulative | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatCumulative = $UpdateCatCumulative | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatCumulative = $UpdateCatCumulative | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatCumulative = $UpdateCatCumulative | Sort-Object -Property DatePosted | Select-Object -Last 1
            #$UpdateCatCumulative = $UpdateCatCumulative | Sort-Object -Property DatePosted

            #===================================================================================================
            Write-Verbose '19.1.24 DotNet Framework Updates'
            #===================================================================================================
            $UpdateCatDotNet = @()
            $UpdateCatDotNet = $AllUpdates | Where-Object {$_.Category -eq 'DotNet'}
            $UpdateCatDotNet = $UpdateCatDotNet | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatDotNet = $UpdateCatDotNet | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatDotNet = $UpdateCatDotNet | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatDotNet = $UpdateCatDotNet | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatDotNet = $UpdateCatDotNet | Sort-Object -Property DatePosted

            #===================================================================================================
            Write-Verbose '19.1.1 Adobe Flash Security Updates'
            #===================================================================================================
            $UpdateCatAdobe = @()
            $UpdateCatAdobe = $AllUpdates | Where-Object {$_.Category -eq 'Adobe'}
            $UpdateCatAdobe = $UpdateCatAdobe | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatAdobe = $UpdateCatAdobe | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatAdobe = $UpdateCatAdobe | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatAdobe = $UpdateCatAdobe | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatAdobe = $UpdateCatAdobe | Sort-Object -Property DatePosted | Select-Object -Last 1

            #===================================================================================================
            Write-Verbose '19.1.1 Component Updates'
            #===================================================================================================
            $UpdateCatComponent = @()
            $UpdateCatComponent = $AllUpdates | Where-Object {$_.Category -eq 'Component'}
            $UpdateCatComponent = $UpdateCatComponent | Where-Object {$_.KBTitle -like "*$OSVersionNumber*"}
            $UpdateCatComponent = $UpdateCatComponent | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
            if ($OSInstallationType -like "*Server*") {
                $UpdateCatComponent = $UpdateCatComponent | Where-Object {$_.KBTitle -like "*Server*"}
            } else {
                $UpdateCatComponent = $UpdateCatComponent | Where-Object {$_.KBTitle -notlike "*Server*"}
            }
            $UpdateCatComponent = $UpdateCatComponent | Sort-Object -Property DatePosted

            #===================================================================================================
            Write-Verbose '19.1.1 Windows 7'
            #===================================================================================================
            $CatalogWindows7 = @()
            $CatalogWindows7WinPE = @()
            if ($OSVersion -like "6.1.7601.*" -and $OSInstallationType -eq 'Client') {
                $CatalogWindows7 = $AllUpdates | Where-Object {$_.Category -like "*Windows 7*"}
                $CatalogWindows7 = $CatalogWindows7 | Where-Object {$_.KBTitle -like "*$OSArchitecture*"}
                $CatalogWindows7 = $CatalogWindows7 | Sort-Object -Property DatePosted

                $CatalogWindows7 = $CatalogWindows7 | Sort-Object -Property DatePosted

                $CatalogWindows7WinPE = $CatalogWindows7 | Where-Object {$_.Severity -eq 'WinPE'}
            }

            #===================================================================================================
            Write-Verbose '19.1.1 Update Validation'
            #===================================================================================================
            Write-Host '========================================================================================' -ForegroundColor DarkGray
            Write-Host "Operating System Updates" -ForegroundColor Green

            foreach ($Update in $UpdateCatSetup) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            foreach ($Update in $UpdateCatServicing) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            foreach ($Update in $UpdateCatCumulative) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            foreach ($Update in $UpdateCatDotNet) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            foreach ($Update in $UpdateCatAdobe) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            foreach ($Update in $UpdateCatComponent) {
                $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                    if ($DownloadUpdates.IsPresent) {
                        Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                        Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                    } else {
                        Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                        $Execute = $false
                    }
                }
            }

            if ($OSVersion -like "6.1.7601.*") {
                foreach ($Update in $CatalogWindows7) {
                    $(Get-ChildItem -Path $OSBuilderContent\Updates -Directory -Recurse | Where-Object {$_.Name -eq $($Update.KBTitle)}).FullName
                    if (!(Test-Path "$OSBuilderContent\Updates\*\$($Update.KBTitle)\$($Update.FileName)")) {
                        if ($DownloadUpdates.IsPresent) {
                            Write-Warning "Missing $($Update.KBTitle) ... Downloading"
                            Get-OSBUpdate -FilterKBTitle "$($Update.KBTitle)" -Download -HideDetails
                        } else {
                            Write-Warning "Missing $($Update.KBTitle) ... Execution will be Disabled"
                            $Execute = $false
                        }
                    }
                }
            }

            #===================================================================================================
            Write-Verbose '19.1.25 Execute'
            #===================================================================================================
            if ($Execute.IsPresent) {
                #===================================================================================================
                Write-Verbose '19.1.25 Remove Existing WorkingPath'
                #===================================================================================================
                if (Test-Path $WorkingPath) {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Remove-Item -Path "$WorkingPath" -Force -Recurse
                }

                #===================================================================================================
                Write-Verbose '19.1.25 Set Variables'
                #===================================================================================================
                $MountDirectory = Join-Path $OSBuilderContent\Mount "os$((Get-Date).ToString('mmss'))"
                $MountWinPE = Join-Path $OSBuilderContent\Mount "winpe$((Get-Date).ToString('mmss'))"
                $MountWinRE = Join-Path $OSBuilderContent\Mount "winre$((Get-Date).ToString('mmss'))"
                $MountWinSE = Join-Path $OSBuilderContent\Mount "setup$((Get-Date).ToString('mmss'))"
                $Info = Join-Path $WorkingPath 'info'
                $OS = Join-Path $WorkingPath 'OS'
                $WinPE = Join-Path $WorkingPath 'WinPE'
                $PEInfo = Join-Path $WinPE 'info'
                $Logs = Join-Path $Info 'logs'
                $PELogs = Join-Path $PEInfo 'logs'
                $WimTemp = Join-Path $WorkingPath "WimTemp"
                
                #===================================================================================================
                Write-Verbose '19.1.1 Start Transcript'
                #===================================================================================================
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                $ScriptName = $($MyInvocation.MyCommand.Name)
                $LogName = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-$ScriptName.log"
                Start-Transcript -Path (Join-Path "$Info\logs" $LogName)

                OSB-OS-CreateDirectories
                OSB-Info-WorkingInformation
                OSB-OS-CopyOS
                OSB-UpdatesPE-Setup
                OSB-WinPE-Mount -OSMediaPath "$WorkingPath"
                OSB-UpdatesPE-SSU
                if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {OSB-WinPE-ADK}
                OSB-UpdatesPE-LCU
                OSB-UpdatesPE-WinSeven
                if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                    OSB-WinPE-DaRT
                    OSB-WinPE-ExtraFiles
                    OSB-WinPE-AutoExtraFiles
                    OSB-WinPE-Drivers
                    OSB-WinPE-Scripts
                }
                OSB-WinPE-Sources -OSMediaPath "$WorkingPath"
                OSB-WinPE-PackageInventory -OSMediaPath "$WorkingPath"
                if ($WaitDismountWinPE.IsPresent){[void](Read-Host 'Press Enter to Continue')}
                OSB-WinPE-Dismount -OSMediaPath "$WorkingPath"
                OSB-WinPE-Export -OSMediaPath "$WorkingPath"
                OSB-WinPE-ExportBootWim -OSMediaPath "$WorkingPath"
                OSB-WinPE-ExportInventory -OSMediaPath "$WorkingPath"
                OSB-OS-MountWindowsImage

                #===================================================================================================
                Write-Verbose '19.1.25 Install.wim: Mount Registry for UBR Information'
                #===================================================================================================
                Write-Host "Install.wim: Mount Registry for UBR Information" -ForegroundColor Green
                OSB-OS-RegMountUBR

                $OSVersionNumber = $null
                $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
                $RegCurrentVersionUBR = $($RegCurrentVersion.UBR)
                $UBR = "$OSBuild.$RegCurrentVersionUBR"

                OSB-OS-RegExportCurrentVersion
                OSB-OS-ReplaceWinRE
                OSB-Updates-SSU
                OSB-Updates-Component
                $UBRPre = $UBR
                Write-Host "Install.wim: Update Build Revision $UBRPre (Pre-LCU)" -ForegroundColor Green
                OSB-Updates-LCU
                #===================================================================================================
                Write-Verbose '19.1.25 Install.wim: Registry and UBR PostInstall'
                #===================================================================================================
                OSB-OS-RegMountUBR
                $OSVersionNumber = $null
                $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
                $RegCurrentVersionUBR = $($RegCurrentVersion.UBR)
                $UBR = "$OSBuild.$RegCurrentVersionUBR"
                OSB-OS-RegExportCurrentVersion
                #===================================================================================================
                Write-Host "Install.wim: Update Build Revision $UBR (Post-LCU)" -ForegroundColor Green
                #===================================================================================================
                OSB-Updates-DotNet
                OSB-Updates-Adobe
                OSB-Updates-DismImageCleanup
                OSB-Updates-WinSeven
                #===================================================================================================
                # OSBuild Only
                #===================================================================================================
                if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                    if ($LanguagePacks) {OSB-Lang-LanguagePacks}
                    if ($LanguageInterfacePacks) {OSB-Lang-LanguageInterfacePacks}
                    if ($LocalExperiencePacks) {OSB-Lang-LocalExperiencePacks}
                    if ($LanguageFeatures) {OSB-Lang-LanguageFeatures}
                    if ($LanguagePacks -or $LanguageInterfacePacks -or $LanguageFeatures -or $LocalExperiencePacks) {
                        OSB-Lang-LanguageSettings
                        OSB-Updates-LCUForce
                        OSB-Updates-DismImageCleanup
                    }

                    if ($FeaturesOnDemand) {
                        OSB-OSBuild-FeaturesOnDemand
                        OSB-Updates-LCUForce
                        OSB-Updates-DismImageCleanup
                    }

                    if ($EnableFeature) {
                        OSB-OSBuild-EnableWindowsOptionalFeature
                        OSB-Updates-LCUForce
                        OSB-Updates-DismImageCleanup
                    }

                    if ($EnableNetFX3 -eq 'True') {
                        OSB-OSBuild-EnableNetFX
                        OSB-Updates-DotNet
                        OSB-Updates-LCUForce
                    }

                    if ($RemoveAppx) {OSB-OSBuild-RemoveAppx}
                    if ($RemovePackage) {OSB-OSBuild-RemovePackage}
                    if ($RemoveCapability) {OSB-OSBuild-RemoveCapability}
                    if ($DisableFeature) {OSB-OSBuild-DisableWindowsOptionalFeature}
                    if ($Packages) {OSB-OSBuild-Packages}
                    if ($Drivers) {OSB-OSBuild-Drivers}
                    if ($ExtraFiles) {OSB-OSBuild-ExtraFiles}
                    if ($StartLayoutXML) {OSB-OSBuild-StartLayout}
                    if ($UnattendXML) {OSB-OSBuild-Unattend}
                    if ($Scripts) {OSB-OSBuild-Scripts}

                    OSB-RegistryXML
                    OSB-Updates-SSUForce
                }
                #===================================================================================================
                # Mirror OSMedia and OSBuild
                #===================================================================================================
                OSB-AutoExtraFilesBackup -OSMediaPath "$WorkingPath"
                OSB-SessionsCopy -OSMediaPath "$WorkingPath"
                OSB-OS-Inventory -OSMediaPath "$WorkingPath"
                OSB-OS-DismountWindowsImage
                OSB-OS-ExportWindowsImage

                #===================================================================================================
                Write-Verbose '19.1.1 Install.wim: Export Configuration'
                #===================================================================================================
                Write-Host "Install.wim: Export Configuration to $WorkingPath\WindowsImage.txt" -ForegroundColor Green
                $GetWindowsImage = @()
                $GetWindowsImage = Get-WindowsImage -ImagePath "$OS\sources\install.wim" -Index 1 | Select-Object -Property *

                Write-Verbose "========== SPBuild: $($GetWindowsImage.Build).$($GetWindowsImage.SPBuild)"
                if ($OSVersion -like "6.1*") {
                    Write-Verbose '========== Windows 6.1'
                    $UBR = "$($GetWindowsImage.Build).$($GetWindowsImage.SPBuild)"
                }
                Write-Verbose "========== UBR: $UBR"

                $GetWindowsImage | Add-Member -Type NoteProperty -Name "UBR" -Value $UBR
                $GetWindowsImage | Out-File "$WorkingPath\WindowsImage.txt"
                $GetWindowsImage | Out-File "$Info\logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.txt"
                $GetWindowsImage | Export-Clixml -Path "$Info\xml\Get-WindowsImage.xml"
                $GetWindowsImage | Export-Clixml -Path "$Info\xml\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.xml"
                $GetWindowsImage | ConvertTo-Json | Out-File "$Info\json\Get-WindowsImage.json"
                $GetWindowsImage | ConvertTo-Json | Out-File "$Info\json\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.json"
                (Get-Content "$WorkingPath\WindowsImage.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$WorkingPath\WindowsImage.txt"
                
                #===================================================================================================
                Write-Verbose '19.1.1 Install.wim: Export Content'
                #===================================================================================================
                #Write-Host '========================================================================================' -ForegroundColor DarkGray
                Write-Host "Install.wim: Export Content to $Info\Get-WindowsImageContent.txt" -ForegroundColor Green
                Get-WindowsImageContent -ImagePath "$OS\Sources\install.wim" -Index 1 | Out-File "$Info\Get-WindowsImageContent.txt"

                #===================================================================================================
                Write-Verbose '19.1.1 UBR Validation'
                #===================================================================================================
                if ($UBRPre -eq $UBR) {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Warning 'The Update Build Revision did not change after Windows Updates'
                    Write-Warning 'There may have been an issue applying the Latest Cumulative Update if this was not expected'
                }
                if (!($UBR)) {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    $UBR = $((Get-Date).ToString('mmss'))
                    Write-Warning 'Could not determine a UBR'
                }

                #===================================================================================================
                Write-Verbose '19.1.1 Remove Temporary Files'
                #===================================================================================================
                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 "$MountWinSE") {Remove-Item -Path "$MountWinSE" -Force -Recurse | Out-Null}

                #===================================================================================================
                Write-Verbose '19.1.1 Set New Name'
                #===================================================================================================
                $OSImageName = $($GetWindowsImage.ImageName)
                $OSImageName = $OSImageName -replace '\(', ''
                $OSImageName = $OSImageName -replace '\)', ''

                $OSArchitecture = $($GetWindowsImage.Architecture)
                if ($OSArchitecture -eq '0') {$OSArchitecture = 'x86'}
                if ($OSArchitecture -eq '6') {$OSArchitecture = 'ia64'}
                if ($OSArchitecture -eq '9') {$OSArchitecture = 'x64'}
                if ($OSArchitecture -eq '12') {$OSArchitecture = 'x64 ARM'}

                $OSBuild = $($GetWindowsImage.Build)
                $OSVersionNumber = $null
                if (Test-Path "$Info\xml\CurrentVersion.xml") {
                    $RegCurrentVersion = Import-Clixml -Path "$Info\xml\CurrentVersion.xml"
                    $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
                }

                if ($OSBuild -eq 7601) {$OSVersionNumber = 7601}
                if ($OSBuild -eq 10240) {$OSVersionNumber = 1507}
                if ($OSBuild -eq 14393) {$OSVersionNumber = 1607}
                if ($OSBuild -eq 15063) {$OSVersionNumber = 1703}
                if ($OSBuild -eq 16299) {$OSVersionNumber = 1709}
                if ($OSBuild -eq 17134) {$OSVersionNumber = 1803}
                if ($OSBuild -eq 17763) {$OSVersionNumber = 1809}

                if ($null -eq $OSVersionNumber) {
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Warning "Windows Image Build $OSBuild is not automatically recognized"
                    Write-Warning "Check for an updated version of OSBuilder"
                    if ($WorkingName -like "build*") {$NewOSMediaName = "$OSImageName $OSArchitecture $UBR"}
                } elseif ($OSBuild -eq 7601) {
                    if ($WorkingName -like "build*") {$NewOSMediaName = "$OSImageName $OSArchitecture SP1 $UBR"}
                } else {
                    if ($WorkingName -like "build*") {$NewOSMediaName = "$OSImageName $OSArchitecture $OSVersionNumber $UBR"}
                }

                $OSLanguages = $($GetWindowsImage.Languages)

                $NewOSMediaName = "$NewOSMediaName $OSLanguages"
                if ($($OSLanguages.count) -eq 1) {$NewOSMediaName = $NewOSMediaName.replace(' en-US','')}

                if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {
                    if ($CustomName) {$NewOSMediaName = "$CustomName $UBR"}
                }
                
                if ($MyInvocation.MyCommand.Name -eq 'Update-OSMedia') {$NewOSMediaPath = "$OSBuilderOSMedia\$NewOSMediaName"}
                if ($MyInvocation.MyCommand.Name -eq 'New-OSBuild') {$NewOSMediaPath = "$OSBuilderOSBuilds\$NewOSMediaName"}

                #===================================================================================================
                Write-Verbose '19.1.1 Rename Build Directory'
                #===================================================================================================
                if (Test-Path $NewOSMediaPath) {
                    $mmss = $((Get-Date).ToString('mmss'))
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    Write-Warning 'Trying to rename the Build directory, but it already exists'
                    Write-Warning "Appending $mmss to the directory Name"
                    Write-Host '========================================================================================' -ForegroundColor DarkGray
                    $NewOSMediaName = "$NewOSMediaName $mmss"
                    $NewOSMediaPath = "$OSBuilderOSMedia\$NewOSMediaName"
                }

                #===================================================================================================
                Write-Verbose '19.1.1 Export Variables.xml'
                #===================================================================================================
                Get-Variable | Select-Object -Property Name, Value | Export-Clixml "$Info\xml\Variables.xml"
                Get-Variable | Select-Object -Property Name, Value | ConvertTo-Json | Out-File "$Info\json\Variables.json"

                #===================================================================================================
                Write-Verbose '19.1.25 OSBMedia*'
                #===================================================================================================
                if ($MediaISO.IsPresent) {New-OSBMediaISO -FullName "$WorkingPath"}
                if ($MediaINFO.IsPresent) {Show-OSBMediaINFO -FullName "$WorkingPath"}

                #===================================================================================================
                Write-Verbose '19.1.1 Stop Transcript'
                #===================================================================================================
                Write-Host "Media: Renaming ""$WorkingPath"" to ""$NewOSMediaName""" -ForegroundColor Green
                Write-Host '========================================================================================' -ForegroundColor DarkGray
                Stop-Transcript
                Rename-Item -Path "$WorkingPath" -NewName "$NewOSMediaName" -ErrorAction Stop
            }
        }
    }

    END {
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
        #Write-Host "$($MyInvocation.MyCommand.Name) END" -ForegroundColor Green
    }
}