Public/Get-OSMedia.ps1

<#
.SYNOPSIS
Returns all Operating Systems in OSBuilder\OSMedia
 
.DESCRIPTION
Returns all Operating Systems in OSBuilder\OSMedia in a PowerShell Custom Object
 
.LINK
https://www.osdeploy.com/osbuilder/docs/functions/osmedia/get-osmedia
 
.PARAMETER GridView
Displays results in PowerShell ISE GridView with an added PassThru Parameter. This can also be displayed with the following command
Get-OSMedia | Out-GridView
#>

function Get-OSMedia {
    [CmdletBinding()]
    PARAM (
        [switch]$GridView
    )

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

        #===================================================================================================
        Write-Verbose '19.1.1 Initialize OSBuilder'
        #===================================================================================================
        Get-OSBuilder -CreatePaths -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 *

        #===================================================================================================
        Write-Verbose '19.1.1 Gather All OSMedia'
        #===================================================================================================
        $AllOSMedia = @()
        $AllOSMedia = Get-ChildItem -Path "$OSBuilderOSMedia" -Directory | Select-Object -Property * | Where-Object {Test-Path $(Join-Path $_.FullName "info\xml\Get-WindowsImage.xml")}
    }

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

        $OSMedia = foreach ($Item in $AllOSMedia) {
            #===================================================================================================
            #Write-Verbose '19.1.1 Get Windows Image Information'
            #===================================================================================================
            $OSMediaPath = $($Item.FullName)
            Write-Verbose "OSMedia Full Path: $($Item.FullName)"
            
            $OSMWindowsImage = @()
            $OSMWindowsImage = Import-Clixml -Path "$($Item.FullName)\info\xml\Get-WindowsImage.xml"
            
            $OSMVersion = $($OSMWindowsImage.Version)
            Write-Verbose "Version: $OSMVersion"

            $OSMImageName = $($OSMWindowsImage.ImageName)
            Write-Verbose "ImageName: $OSMImageName"

            $OSMArch = $OSMWindowsImage.Architecture
            if ($OSMArch -eq '0') {$OSMArch = 'x86'}
            if ($OSMArch -eq '6') {$OSMArch = 'ia64'}
            if ($OSMArch -eq '9') {$OSMArch = 'x64'}
            if ($OSMArch -eq '12') {$OSMArch = 'x64 ARM'}
            Write-Verbose "Arch: $OSMArch"

            $OSMEditionId = $($OSMWindowsImage.EditionId)
            Write-Verbose "EditionId: $OSMEditionId"

            $OSMInstallationType = $($OSMWindowsImage.InstallationType)
            Write-Verbose "InstallationType: $OSMInstallationType"

            $OSMMajorVersion = $($OSMWindowsImage.MajorVersion)
            Write-Verbose "MajorVersion: $OSMMajorVersion"

            $OSMMinorVersion = $($OSMWindowsImage.MinorVersion)
            Write-Verbose "MinorVersion: $OSMMinorVersion"

            $OSMBuild = $($OSMWindowsImage.Build)
            Write-Verbose "Build: $OSMBuild"

            $OSMLanguages = $($OSMWindowsImage.Languages)
            Write-Verbose "Languages: $OSMLanguages"

            $OperatingSystem = ''
            if ($OSMMajorVersion -eq 6) {
                if ($OSMInstallationType -eq 'Client') {
                    $OperatingSystem = 'Windows 7'
                } else {
                    if ($OSMBuild -eq 9600) {$OperatingSystem = 'Server 2012 R2'}
                }
            }
            if ($OSMMajorVersion -eq 10) {
                if ($OSMInstallationType -eq 'Client') {$OperatingSystem = 'Windows 10'}
                if ($OSMInstallationType -eq 'Server' -and $OSMImageName -like "*2016*") {$OperatingSystem = 'Server 2016'}
                if ($OSMInstallationType -eq 'Server' -and $OSMImageName -like "*2019*") {$OperatingSystem = 'Server 2019'}
            }
            $OSMUBR = $($OSMWindowsImage.UBR)
            Write-Verbose "UBR: $OSMUBR"

            # OSMFamily V1
            $OSMFamilyV1 = $(Get-Date -Date $($OSMWindowsImage.CreatedTime)).ToString("yyyyMMddHHmmss") + $OSMEditionID
            # OSMFamily V2
            $OSMFamily = $OSMInstallationType + " " + $OSMEditionId + " " + $OSMArch + " " + [string]$OSMBuild + " " + $OSMLanguages
            Write-Verbose "OSMFamily: $OSMFamily"

            # $OSMWindowsImage | ForEach {$_.PSObject.Properties.Remove('Guid')}
            $OSMGuid = $($OSMWindowsImage.OSMGuid)
            if (-not ($OSMGuid)) {
                $OSMGuid = $(New-Guid)
                $OSMWindowsImage | Add-Member -Type NoteProperty -Name "OSMGuid" -Value $OSMGuid
                $OSMWindowsImage | Out-File "$($Item.FullName)\WindowsImage.txt"
                $OSMWindowsImage | Out-File "$($Item.FullName)\info\logs\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.txt"
                $OSMWindowsImage | Export-Clixml -Path "$($Item.FullName)\info\xml\Get-WindowsImage.xml"
                $OSMWindowsImage | Export-Clixml -Path "$($Item.FullName)\info\xml\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.xml"
                $OSMWindowsImage | ConvertTo-Json | Out-File "$($Item.FullName)\info\json\Get-WindowsImage.json"
                $OSMWindowsImage | ConvertTo-Json | Out-File "$($Item.FullName)\info\json\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Get-WindowsImage.json"
                (Get-Content "$($Item.FullName)\WindowsImage.txt") | Where-Object {$_.Trim(" `t")} | Set-Content "$($Item.FullName)\WindowsImage.txt"
                Write-Verbose "Guid (New): $OSMGuid"
            } else {
                Write-Verbose "Guid: $OSMGuid"
            }

            #===================================================================================================
            #Write-Verbose '19.1.1 Gather Registry Information'
            #===================================================================================================
            $OSMRegistry = @()
            if (Test-Path "$($Item.FullName)\info\xml\CurrentVersion.xml") {
                Write-Verbose "Registry: $($Item.FullName)\info\xml\CurrentVersion.xml"
                $OSMRegistry = Import-Clixml -Path "$($Item.FullName)\info\xml\CurrentVersion.xml"
            } else {
                Write-Verbose "Registry: $($Item.FullName)\info\xml\CurrentVersion.xml (Not Found)"
            }
            [string]$OSMReleaseId = $($OSMRegistry.ReleaseId)

            if ($OSMBuild -eq 7600) {$OSMReleaseId = 7600}
            if ($OSMBuild -eq 7601) {$OSMReleaseId = 7601}
            if ($OSMBuild -eq 9600) {$OSMReleaseId = 9600}
            if ($OSMBuild -eq 10240) {$OSMReleaseId = 1507}
            if ($OSMBuild -eq 14393) {$OSMReleaseId = 1607}
            if ($OSMBuild -eq 15063) {$OSMReleaseId = 1703}
            if ($OSMBuild -eq 16299) {$OSMReleaseId = 1709}
            if ($OSMBuild -eq 17134) {$OSMReleaseId = 1803}
            if ($OSMBuild -eq 17763) {$OSMReleaseId = 1809}

            Write-Verbose "ReleaseId: $OSMReleaseId"

            #===================================================================================================
            #Write-Verbose '19.2.4 WinPE_Drivers'
            #===================================================================================================
<# if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 3")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 3" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 3 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 3 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE Server")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE Server" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Drivers\AutoApply\WinPE Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            } #>


            #===================================================================================================
            #Write-Verbose '19.2.4 WinPE_ExtraFiles'
            #===================================================================================================
<# if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 3")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 3" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 3 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 3 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE Server")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE Server" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_ExtraFiles\AutoApply\WinPE Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            } #>


            #===================================================================================================
            #Write-Verbose '19.2.4 WinPE_Registry'
            #===================================================================================================
<# if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 3")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 3" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 3 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 3 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE Server")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE Server" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Registry\AutoApply\WinPE Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            } #>


            #===================================================================================================
            #Write-Verbose '19.2.4 WinPE_Scripts'
            #===================================================================================================
<# if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 3")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 3" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 3 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 3 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE Server")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE Server" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\WinPE_Scripts\AutoApply\WinPE Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            } #>


            #===================================================================================================
            #Write-Verbose '19.2.1 Drivers'
            #===================================================================================================
            if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 7")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 7" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 7 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 7 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows Server")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows Server" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\Drivers\AutoApply\Windows Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Drivers\AutoApply\Windows Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            }

            #===================================================================================================
            #Write-Verbose '19.2.1 ExtraFiles'
            #===================================================================================================
            if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 7")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 7" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 7 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 7 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows Server")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows Server" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\ExtraFiles\AutoApply\Windows Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            }

            #===================================================================================================
            #Write-Verbose '19.2.2 Registry'
            #===================================================================================================
            if (!(Test-Path "$OSBuilderTemplates\Registry")) {New-Item "$OSBuilderTemplates\Registry" -ItemType Directory -Force | Out-Null}
            if (Test-Path "$OSBuilderContent\Registry\AutoApply") {
                Move-Item -Path "$OSBuilderContent\Registry\AutoApply" -Destination "$OSBuilderTemplates\Registry" -Force | Out-Null
            }
            if (Test-Path "$OSBuilderContent\Registry\Filters") {
                Move-Item -Path "$OSBuilderContent\Registry\Filters" -Destination "$OSBuilderTemplates\Registry" -Force | Out-Null
                Rename-Item -Path "$OSBuilderTemplates\Registry\Filters" -NewName AutoApply -Force | Out-Null
            }

            if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows 7")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows 7" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows 7 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows 7 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows Server")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows Server" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\Registry\AutoApply\Windows Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Registry\AutoApply\Windows Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            }

            #===================================================================================================
            #Write-Verbose '19.2.1 Scripts'
            #===================================================================================================
            if ($OSMReleaseId -eq 7601) {
                if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 7")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 7" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 7 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 7 $OSMArch" -ItemType Directory -Force | Out-Null}
            } else {
                if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Global")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Global" -ItemType Directory -Force | Out-Null}
                if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Global $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Global $OSMArch" -ItemType Directory -Force | Out-Null}
                if ($OSMInstallationType -eq "Client") {
                    if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10" -ItemType Directory -Force | Out-Null}
                    if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10 $OSMArch")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10 $OSMArch" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10 $OSMArch $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows 10 $OSMArch $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
                if ($OSMInstallationType -like "*Server*") {
                    if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows Server")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows Server" -ItemType Directory -Force | Out-Null}
                    #if (!(Test-Path "$OSBuilderTemplates\Scripts\AutoApply\Windows Server $OSMReleaseId")) {New-Item -Path "$OSBuilderTemplates\Scripts\AutoApply\Windows Server $OSMReleaseId" -ItemType Directory -Force | Out-Null}
                }
            }

            #===================================================================================================
            #Write-Verbose '19.1.1 Gather Package Information'
            #===================================================================================================
            $OSMPackage = @()
            if (Test-Path "$($Item.FullName)\info\xml\Get-WindowsPackage.xml") {
                Write-Verbose "Packages: $($Item.FullName)\info\xml\Get-WindowsPackage.xml"
                $OSMPackage = Import-Clixml -Path "$($Item.FullName)\info\xml\Get-WindowsPackage.xml"
            } else {
                Write-Verbose "Packages: $($Item.FullName)\info\xml\Get-WindowsPackage.xml (Not Found)"
            }

            #===================================================================================================
            #Write-Verbose '19.1.3 Gather Sessions Information'
            #===================================================================================================
            $OSMSessions = @()
            if (Test-Path "$($Item.FullName)\Sessions.xml") {
                OSB-SessionsConvert -OSMediaPath "$($Item.FullName)"
            }

            if (Test-Path "$($Item.FullName)\info\xml\Sessions.xml") {
                $OSMSessions = Import-Clixml -Path "$($Item.FullName)\info\xml\Sessions.xml" | Where-Object {$_.targetState -eq 'Installed'} | Sort-Object id
            } else {
                Write-Verbose "Sessions: $($Item.FullName)\info\xml\Sessions.xml (Not Found)"
            }
            
            #===================================================================================================
            #Write-Verbose '19.1.1 Get Latest Adobe Security Update'
            #===================================================================================================
            $LatestAdobe = @()
            $LatestAdobe = $AllUpdates | Sort-Object -Property DatePosted -Descending
            $LatestAdobe = $LatestAdobe | Where-Object {$_.Category -eq 'Adobe'}
            $LatestAdobe = $LatestAdobe | Where-Object {$_.KBTitle -like "*$OSMReleaseId*"}
            $LatestAdobe = $LatestAdobe | Where-Object {$_.KBTitle -like "*$($Arch)*"}
            $LatestAdobe = $LatestAdobe | Where-Object {$_.KBTitle -like "*$OperatingSystem*"}
            $LatestAdobe = $LatestAdobe | Select-Object -First 1

            $InstalledAdobe = $OSMPackage | Where-Object {$_.PackageName -like "*$($LatestAdobe.KBNumber)*"}
            if ($null -eq $InstalledAdobe) {
                Write-Verbose "LatestAdobe: $($LatestAdobe.KBTitle) (Not Installed)"
                #$LatestAdobe = "KB$($LatestAdobe.KBNumber)"
                $LatestAdobe = ''
            } else {
                Write-Verbose "LatestAdobe: $($LatestAdobe.KBTitle) (Installed)"
                $LatestAdobe = 'Latest'
            }

            if ($OSMMajorVersion -eq 6) {$LatestAdobe = 'None'}
            #===================================================================================================
            #Write-Verbose '19.1.1 Get Latest Servicing Stack Update'
            #===================================================================================================
            $LatestSSU = @()
            $LatestSSU = $AllUpdates | Sort-Object -Property DatePosted -Descending
            $LatestSSU = $LatestSSU | Where-Object {$_.Category -eq 'Servicing'}
            $LatestSSU = $LatestSSU | Where-Object {$_.KBTitle -like "*$OSMReleaseId*"}
            $LatestSSU = $LatestSSU | Where-Object {$_.KBTitle -like "*$($Arch)*"}
            $LatestSSU = $LatestSSU | Where-Object {$_.KBTitle -like "*$OperatingSystem*"}
            $LatestSSU = $LatestSSU | Select-Object -First 1

            $InstalledSSU = $OSMPackage | Where-Object {$_.PackageName -like "*$($LatestSSU.KBNumber)*"}
            if ($null -eq $InstalledSSU) {
                Write-Verbose "LatestSSU: $($LatestSSU.KBTitle) (Not Installed)"
            # $LatestSSU = "KB$($LatestSSU.KBNumber)"
                $LatestSSU = ''
            } else {
                Write-Verbose "LatestSSU: $($LatestSSU.KBTitle) (Installed)"
                $LatestSSU = 'Latest'
            }

            #===================================================================================================
            #Write-Verbose '19.1.3 Get Latest Cumulative Update'
            #===================================================================================================
            $LatestLCU = @()
            $LatestLCU = $AllUpdates | Sort-Object -Property DatePosted -Descending
            $LatestLCU = $LatestLCU | Where-Object {$_.Category -eq 'Cumulative'}
            $LatestLCU = $LatestLCU | Where-Object {$_.KBTitle -like "*$OSMReleaseId*"}
            $LatestLCU = $LatestLCU | Where-Object {$_.KBTitle -like "*$($Arch)*"}
            $LatestLCU = $LatestLCU | Where-Object {$_.KBTitle -like "*$OperatingSystem*"}
            $LatestLCU = $LatestLCU | Select-Object -First 1

            $InstalledLCU = $OSMSessions | Where-Object {$_.KBNumber -like "*$($LatestLCU.KBNumber)*"}

            if (!(Test-Path "$($Item.FullName)\info\Sessions.xml")) {
                Write-Verbose "LatestLCU: Sessions.xml required for validation"
                $LatestLCU = 'Repair'
            } elseif ($null -eq $InstalledLCU) {
                Write-Verbose "LatestLCU: $($LatestLCU.KBTitle) (Not Installed)"
                $LatestLCU = ''
            } else {
                Write-Verbose "LatestLCU: $($LatestLCU.KBTitle) (Installed)"
                $LatestLCU = 'Latest'
            }

            #===================================================================================================
            #Write-Verbose '19.1.11 Modify OSMedia Content'
            #===================================================================================================
            if (Test-Path "$($Item.FullName)\WinPE\setup.wim") {Rename-Item "$($Item.FullName)\WinPE\setup.wim" 'winse.wim' -Force | Out-Null}

            if (Test-Path "$($Item.FullName)\WinPE\info\boot.txt") {Rename-Item "$($Item.FullName)\WinPE\info\boot.txt" 'Get-WindowsImage-Boot.txt' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\winpe.txt") {Rename-Item "$($Item.FullName)\WinPE\info\winpe.txt" 'Get-WindowsImage-WinPE.txt' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\winre.txt") {Rename-Item "$($Item.FullName)\WinPE\info\winre.txt" 'Get-WindowsImage-WinRE.txt' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\setup.txt") {Rename-Item "$($Item.FullName)\WinPE\info\setup.txt" 'Get-WindowsImage-WinSE.txt' -Force | Out-Null}
            
            if (Test-Path "$($Item.FullName)\WinPE\info\winpe-WindowsPackage.txt") {Rename-Item "$($Item.FullName)\WinPE\info\winpe-WindowsPackage.txt" 'Get-WindowsPackage-WinPE.txt' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\winre-WindowsPackage.txt") {Rename-Item "$($Item.FullName)\WinPE\info\winre-WindowsPackage.txt" 'Get-WindowsPackage-WinRE.txt' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\setup-WindowsPackage.txt") {Rename-Item "$($Item.FullName)\WinPE\info\setup-WindowsPackage.txt" 'Get-WindowsPackage-WinSE.txt' -Force | Out-Null}

            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-boot.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-boot.wim.json" 'Get-WindowsImage-Boot.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-winpe.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-winpe.wim.json" 'Get-WindowsImage-WinPE.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-winre.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-winre.wim.json" 'Get-WindowsImage-WinRE.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-setup.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsImage-setup.wim.json" 'Get-WindowsImage-WinSE.json' -Force | Out-Null}

            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-boot.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-boot.wim.json" 'Get-WindowsPackage-Boot.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-winpe.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-winpe.wim.json" 'Get-WindowsPackage-WinPE.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-winre.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-winre.wim.json" 'Get-WindowsPackage-WinRE.json' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-setup.wim.json") {Rename-Item "$($Item.FullName)\WinPE\info\json\Get-WindowsPackage-setup.wim.json" 'Get-WindowsPackage-WinSE.json' -Force | Out-Null}
            
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-boot.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-boot.wim.xml" 'Get-WindowsImage-Boot.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-winpe.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-winpe.wim.xml" 'Get-WindowsImage-WinPE.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-winre.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-winre.wim.xml" 'Get-WindowsImage-WinRE.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-setup.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsImage-setup.wim.xml" 'Get-WindowsImage-WinSE.xml' -Force | Out-Null}
            
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-boot.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-boot.wim.xml" 'Get-WindowsPackage-Boot.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-winpe.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-winpe.wim.xml" 'Get-WindowsPackage-WinPE.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-winre.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-winre.wim.xml" 'Get-WindowsPackage-WinRE.xml' -Force | Out-Null}
            if (Test-Path "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-setup.wim.xml") {Rename-Item "$($Item.FullName)\WinPE\info\xml\Get-WindowsPackage-setup.wim.xml" 'Get-WindowsPackage-WinSE.xml' -Force | Out-Null}


            #===================================================================================================
            #Write-Verbose '19.1.1 Object Properties'
            #===================================================================================================
            $ObjectProperties = @{
                #MediaType = $Item.Parent
                MediaType           = 'OSMedia'
                OSMFamily           = $OSMFamily
                Name                = $Item.Name
                ImageName           = $OSMImageName
                Arch                = $OSMArch
                ReleaseId           = $OSMReleaseId
                Build               = [string]$OSMBuild
                UBR                 = [version]$OSMUBR
                Languages           = $OSMWindowsImage.Languages
                EditionId           = $OSMEditionId
                InstallationType    = $OSMInstallationType
                MajorVersion        = $OSMMajorVersion
                Servicing           = $LatestSSU
                Cumulative          = $LatestLCU
                Adobe               = $LatestAdobe
                FullName            = $Item.FullName
                OSMGuid             = $OSMGuid
                CreatedTime         = [datetime]$OSMWindowsImage.CreatedTime
                ModifiedTime        = [datetime]$OSMWindowsImage.ModifiedTime
                OSMFamilyV1         = $OSMFamilyV1
            }
            New-Object -TypeName PSObject -Property $ObjectProperties
            Write-Verbose ""
        }

        #===================================================================================================
        #Write-Verbose '19.1.3 Output'
        #===================================================================================================
        if ($GridView.IsPresent) {$OSMedia | Select-Object MediaType,OSMFamily,Name,ImageName,Arch,ReleaseId,Build,UBR,Languages,EditionId,InstallationType,MajorVersion,Servicing,Cumulative,Adobe,FullName,OSMGuid,CreatedTime,ModifiedTime | Sort-Object -Property Name | Out-GridView -PassThru -Title 'OSMedia'}
        else {$OSMedia | Select-Object MediaType,OSMFamily,Name,ImageName,Arch,ReleaseId,Build,UBR,Languages,EditionId,InstallationType,MajorVersion,Servicing,Cumulative,Adobe,FullName,OSMGuid,CreatedTime,ModifiedTime,OSMFamilyV1 | Sort-Object -Property Name}
    }

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