Import-OSMedia.ps1

function Import-OSMedia {
    [CmdletBinding()]
    Param (
        [switch]$UpdateOSMedia
    )
    #======================================================================================
    # Start 18.9.13
    #======================================================================================
    Write-Host "===========================================================================" -ForegroundColor Green
    Write-Host "Import-OSMedia" -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
    #======================================================================================
    # Check Drives for Images 18.9.13
    #======================================================================================
    $OSSourcePath = Get-WmiObject -Class Win32_LogicalDisk | Select-Object -Property DeviceID, Description, VolumeName
    $OSSourcePath = $OSSourcePath | Where-Object {Test-Path $(Join-Path $_.DeviceID (Join-Path "sources" "install.wim"))}
    if ($null -eq $OSSourcePath) {
        Write-Warning "Windows Image could not be found on any CD or DVD Drives . . . Exiting!"
        Return
    }
    #======================================================================================
    # Scan Drives 18.9.13
    #======================================================================================
    Write-Host "Scanning Image Information ... Please Wait!" -ForegroundColor Yellow
    Write-Host "===========================================================================" -ForegroundColor Yellow
    #======================================================================================
    $WindowsImages = $OSSourcePath.DeviceID | ForEach-Object {Get-WindowsImage -ImagePath "$_\sources\install.wim"} | ForEach-Object {Get-WindowsImage -ImagePath "$($_.ImagePath)" -Index $($_.ImageIndex) | Select-Object -Property *}

    $WindowsImages = $WindowsImages | Select-Object -Property ImagePath, ImageIndex, Languages, ImageName, Architecture, EditionID, Version, MajorVersion, MinorVersion, Build, SPBuild, SPLevel, CreatedTime, ModifiedTime
    #$WindowsImages = $WindowsImages | Where-Object {$_.ImageName -like "*Windows 10*"}
    foreach ($Image in $WindowsImages) {
        $Image.Architecture = $Image.Architecture -replace "1", "MIPS"
        $Image.Architecture = $Image.Architecture -replace "2", "Alpha"
        $Image.Architecture = $Image.Architecture -replace "3", "PowerPC"
        $Image.Architecture = $Image.Architecture -replace "6", "ia64"
        $Image.Architecture = $Image.Architecture -replace "9", "x64"
        $Image.Architecture = $Image.Architecture -replace "0", "x86"
        $Image.ImageName = $Image.ImageName -replace "ServerStandardACore", "Standard Core"
        $Image.ImageName = $Image.ImageName -replace "ServerDatacenterACore", "Datacenter Core"
        $Image.ImageName = $Image.ImageName -replace "ServerStandardCore", "Standard Core"
        $Image.ImageName = $Image.ImageName -replace "ServerDatacenterCore", "Datacenter Core"
        $Image.ImageName = $Image.ImageName -replace "ServerStandard", "Standard"
        $Image.ImageName = $Image.ImageName -replace "ServerDatacenter", "Datacenter"
    }
    #======================================================================================
    # Check Images 18.9.13
    #======================================================================================
    if (@($WindowsImages).Count -gt 0) {
        $WindowsImages = $WindowsImages | Out-GridView -Title "Import-OSMedia: Select OSMedia to Import and press OK (Cancel to Exit)" -PassThru
        if($null -eq $WindowsImages) {
            Write-Warning "OSMedia was not selected . . . Exiting!"
            Return
        }
    } else {
        Write-Warning "OSMedia was not found . . . Exiting!"
        Return
    }
    #======================================================================================
    # Import Images 18.9.13
    #======================================================================================
    foreach ($WindowsImage in $WindowsImages) {
        #Get-WindowsImage -ImagePath "$($WindowsImage.ImagePath)" -Index $($WindowsImage.ImageIndex) | Select-Object -Property *
        $OSImagePath = $($WindowsImage.ImagePath)
        $OSImageIndex = $($WindowsImage.ImageIndex)
        $OSSourcePath = Split-Path -Path "$OSImagePath" -Qualifier
        $WindowsImage = Get-WindowsImage -ImagePath "$OSImagePath" -Index $OSImageIndex | Select-Object -Property *

        $OSImageName = $($WindowsImage.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 '\)', ''

        $OSImageDescription = $($WindowsImage.ImageDescription)
        $OSArchitecture = $($WindowsImage.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'}
        $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)
        #======================================================================================
        # Mount Install.wim 18.9.13
        #======================================================================================
        Write-Host "Mounting Install.wim" -ForegroundColor Yellow
        #======================================================================================
        $MountDirectory = Join-Path $MountPath "os$((Get-Date).ToString('HHmmss'))"
        if ( ! (Test-Path "$MountDirectory")) {New-Item "$MountDirectory" -ItemType Directory -Force | Out-Null}
        # https://docs.microsoft.com/en-us/powershell/module/dism/mount-windowsimage?view=win10-ps
        Write-Host "$OSImagePath"
        Mount-WindowsImage -ImagePath "$OSImagePath" -Index $OSImageIndex -Path "$MountDirectory" -Optimize -ReadOnly
        #======================================================================================
        # Get UBR Method 1 18.9.18
        # Inconsistent Results
        #======================================================================================
<# $ntoskrnl = $(Get-Item "$MountDirectory\windows\system32\ntoskrnl.exe").VersionInfo | Select-Object -Property ProductBuildPart,ProductPrivatePart
        $UBR = "$($ntoskrnl.ProductBuildPart).$($ntoskrnl.ProductPrivatePart)" #>

        #======================================================================================
        # Get UBR Method 2 18.9.19
        # Doesn't work on LTSB media without an installed CU Rollup
        #======================================================================================
<# $InstalledRollup = $(Get-WindowsPackage -Path "$MountDirectory" | Where-Object {$_.PackageName -like "*RollupFix*" -and $_.PackageState -eq 'Installed'}).PackageName
        $RollupOSFullVersion = ($InstalledRollup -split '~~')[1]
        $RollupOSVersion = ($RollupOSFullVersion -split '\.')[0]
        $RollupOSBuild = ($RollupOSFullVersion -split '\.')[1]
        $UBR = "$($RollupOSVersion).$($RollupOSBuild)" #>

        #======================================================================================
        # Get UBR Method 3 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])" #>

        #======================================================================================
        # 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
        $RegCurrentVersionUBR = $null

        $OSVersionNumber = $($RegCurrentVersion.ReleaseId)
        $RegCurrentVersionUBR = $($RegCurrentVersion.UBR)
        $UBR = "$OSBuild.$RegCurrentVersionUBR"
        #======================================================================================
        # Set OS Main Information 18.9.20
        #======================================================================================
        $OSMediaName = "$OSImageName $OSArchitecture $OSVersionNumber $OSLanguages $UBR"

<# if ($OSBuild -eq 10240) {$OSVersionNumber = 1507}
        elseif ($OSBuild -eq 10586) {$OSVersionNumber = 1511}
        elseif ($OSBuild -eq 14393) {$OSVersionNumber = 1607}
        elseif ($OSBuild -eq 15063) {$OSVersionNumber = 1703}
        elseif ($OSBuild -eq 16299) {$OSVersionNumber = 1709}
        elseif ($OSBuild -eq 17134) {$OSVersionNumber = 1803}
        if ($null -eq $OSVersionNumber ) {
            Write-Warning "OS Build $OSVersionNumber is not automatically recognized"
            Write-Warning "Check for an updated version of OSBuilder"
            Write-Host ""
            $OSMediaName = "$OSImageName $OSArchitecture $OSLanguages $UBR"
        } else {
            $OSMediaName = "$OSImageName $OSArchitecture $OSVersionNumber $OSLanguages $UBR"
        } #>

        
        if ($($OSLanguages.count) -eq 1) {$OSMediaName = $OSMediaName.replace(' en-US', '')}
        #======================================================================================
        # Set WorkingPath 18.9.13
        #======================================================================================
        $WorkingPath = Join-Path $OSMediaPath $OSMediaName
        #======================================================================================
        # Remove Existing Content 18.9.13
        #======================================================================================
        if (Test-Path $WorkingPath) {
            Write-Warning "$WorkingPath exists. Contents will be replaced!"
            Remove-Item -Path "$WorkingPath" -Force -Recurse
            Write-Host ""
        }
        #======================================================================================
        # Working Directories 18.9.13
        #======================================================================================
        $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}
        #======================================================================================
        # 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"
        #======================================================================================
        # Start the Transcript 18.9.13
        #======================================================================================
        $ScriptName = $MyInvocation.MyCommand.Name
        $LogName = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-$ScriptName.log"
        Start-Transcript -Path (Join-Path $Logs $LogName)
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "OSMedia Information" -ForegroundColor Yellow
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Source Path: $OSSourcePath" -ForegroundColor Yellow
        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
        Write-Host "-UBR: $UBR" -ForegroundColor Cyan
        Write-Host ""
        Write-Host "Working Path: $WorkingPath" -ForegroundColor Yellow
        Write-Host "-OSMedia Name: $OSMediaName" -ForegroundColor Cyan
        Write-Host "-Info: $Info" -ForegroundColor Cyan
        Write-Host "-Logs: $Logs" -ForegroundColor Cyan
        Write-Host "-OS: $OS" -ForegroundColor Cyan
        Write-Host "-WinPE: $WinPE" -ForegroundColor Cyan
        #======================================================================================
        # Import Operating System 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Importing Operating System to OSMedia" -ForegroundColor Yellow
        #======================================================================================
        Write-Host "Copying OS to $OS"

        Copy-Item -Path "$OSSourcePath\*" -Destination "$OS" -Exclude install.wim -Recurse -Force | Out-Null
        Write-Host "Removing the Read Only file flag in $OS"
        Get-ChildItem -Recurse -Path "$OS\*" | Set-ItemProperty -Name IsReadOnly -Value $false -ErrorAction SilentlyContinue | Out-Null

        Write-Host "Exporting Index $OSImageIndex to $OS\sources\install.wim"
        Export-WindowsImage -SourceImagePath "$OSImagePath" -SourceIndex $OSImageIndex -DestinationImagePath "$OS\sources\install.wim" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage.log"
        #======================================================================================
        # Save Mounted Windows Image Configuration 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Saving Mounted Windows Image Configuration" -ForegroundColor Yellow
        #======================================================================================
        if ($OSImageName -notlike "*server*") {
            $GetAppxProvisionedPackage = Get-AppxProvisionedPackage -Path "$MountDirectory"
            Write-Host "$WorkingPath\AppxProvisionedPackage.txt"
            $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"
        #======================================================================================
        # Export WinPE Wims 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Exporting WinPE WIMs" -ForegroundColor Yellow
        #======================================================================================
        Write-Host "$WinPE\boot.wim"
        Copy-Item -Path "$OS\sources\boot.wim" -Destination "$WinPE\boot.wim" -Force
        #======================================================================================
        Write-Host "$WinPE\winpe.wim"
        Export-WindowsImage -SourceImagePath "$OS\sources\boot.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\winpe.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-winpe.wim.log"
        #======================================================================================
        Write-Host "$WinPE\setup.wim"
        Export-WindowsImage -SourceImagePath "$OS\sources\boot.wim" -SourceIndex 2 -DestinationImagePath "$WinPE\setup.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-setup.wim.log"
        #======================================================================================
        Write-Host "$WinPE\winre.wim"
        Export-WindowsImage -SourceImagePath "$MountDirectory\Windows\System32\Recovery\winre.wim" -SourceIndex 1 -DestinationImagePath "$WinPE\winre.wim" -LogPath "$PELogs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Export-WindowsImage-winre.wim.log"
        #======================================================================================
        # Export Install.wim 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "Dismounting Install.wim from $MountDirectory" -ForegroundColor Yellow
        Dismount-WindowsImage -Discard -Path "$MountDirectory" -LogPath "$Logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Dismount-WindowsImage.log"
        #======================================================================================
        # Save WinPE Image Configuration 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "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 18.9.13
        #======================================================================================
        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 18.9.13
        #======================================================================================
        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"
        #======================================================================================
        # Get-WindowsImage WinRE 18.9.13
        #======================================================================================
        Write-Host "$PEInfo\winre.txt"
        $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"
        #======================================================================================
        # Save Windows Image Configuration 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Yellow
        Write-Host "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"
        #======================================================================================
        # Save Get-WindowsImageContent 18.9.13
        #======================================================================================
        Write-Host "$Info\Get-WindowsImageContent.txt"
        Get-WindowsImageContent -ImagePath "$OS\Sources\install.wim" -Index 1 | Out-File "$Info\Get-WindowsImageContent.txt"
        #======================================================================================
        # Display OS Information 18.9.13
        #======================================================================================
        Show-OSInfo $WorkingPath
        #======================================================================================
        # Remove Mount Directory 18.9.13
        #======================================================================================
        if (Test-Path "$MountDirectory") {Remove-Item "$MountDirectory" -Force -Recurse | Out-Null}
        #======================================================================================
        # Stop the Transcript 18.9.13
        #======================================================================================
        Stop-Transcript
        #======================================================================================
        # Complete 18.9.13
        #======================================================================================
        Write-Host "===========================================================================" -ForegroundColor Green
        Write-Host "Complete!" -ForegroundColor Green
        Write-Host "===========================================================================" -ForegroundColor Green
        #======================================================================================
        # Update-OSMedia 18.9.13
        #======================================================================================
        if ($UpdateOSMedia.IsPresent) {
            Update-OSMedia -ByName "$OSMediaName" -DownloadUpdates -Execute
        }
    }
}