Public/Get-OSBuilder.ps1

<#
.SYNOPSIS
Offline Servicing for Windows 10, Windows Server 2016 and Windows Server 2019

.DESCRIPTION
OSBuilder is used to Update and Configure Windows 10, Windows Server 2016, and Windows Server 2019 using Offline Servicing

.LINK
https://www.osdeploy.com/osbuilder/docs/functions/main/get-osbuilder

.PARAMETER CreatePaths
Creates empty directories used by OSBuilder

.PARAMETER HideDetails
Hides Write-Host output. Used when called from other functions

.PARAMETER SetPath
Changes the path from the default of C:\OSBuilder to the path specified

.PARAMETER Update
Updates the OSBuilder Module

.EXAMPLE
Get-OSBuilder -SetPath D:\OSBuilder
Sets the OSBuilder Main directory to D:\OSBuilder

.EXAMPLE
Get-OSBuilder -CreatePaths
Creates empty directories used by OSBuilder

.EXAMPLE
Get-OSBuilder -HideDetails
Method for refreshing all OSBuilder Variables. Used by other OSBuilder Functions
#>


function Get-OSBuilder {
    [CmdletBinding()]
    PARAM (
        [switch]$CreatePaths,
        [switch]$HideDetails,
        [string]$SetPath,
        [switch]$Update
    )
    #===================================================================================================
    # 18.10.2 Remove OSMedia Module
    #===================================================================================================
    Get-OSMediaModule
    #===================================================================================================
    # 19.2.8 Get OSBuilder Version
    #===================================================================================================
    $global:OSBuilderVersion = $(Get-Module -Name OSBuilder | Sort-Object Version | Select-Object Version -Last 1).Version
    if ($HideDetails -eq $false) {
        Write-Host "OSBuilder $OSBuilderVersion" -ForegroundColor Cyan
    }
    #===================================================================================================
    # 18.10.1 Create Empty Registry Key and Values
    #===================================================================================================
    if (!(Test-Path HKCU:\Software\OSDeploy\OSBuilder)) {New-Item HKCU:\Software\OSDeploy -Name OSBuilder -Force | Out-Null}
    #===================================================================================================
    # 18.10.1 Set Global OSBuilder
    #===================================================================================================
    if (!(Get-ItemProperty -Path 'HKCU:\Software\OSDeploy' -Name OSBuilderPath -ErrorAction SilentlyContinue)) {New-ItemProperty -Path "HKCU:\Software\OSDeploy" -Name OSBuilderPath -Force | Out-Null}
    if ($SetPath) {Set-ItemProperty -Path "HKCU:\Software\OSDeploy" -Name "OSBuilderPath" -Value "$SetPath" -Force}
    $global:OSBuilderPath = $(Get-ItemProperty "HKCU:\Software\OSDeploy").OSBuilderPath
    if (!($OSBuilderPath)) {$global:OSBuilderPath = "$Env:SystemDrive\OSBuilder"}
    #===================================================================================================
    # 19.2.8 Set Primary Paths
    #===================================================================================================
    $global:OSBuilderContent =      "$OSBuilderPath\Content"
    $global:OSBuilderOSBuilds =     "$OSBuilderPath\OSBuilds"
    $global:OSBuilderOSMedia =      "$OSBuilderPath\OSMedia"
    $global:OSBuilderPEBuilds =     "$OSBuilderPath\PEBuilds"
    $global:OSBuilderTasks =        "$OSBuilderPath\Tasks"
    $global:OSBuilderTemplates =    "$OSBuilderPath\Templates"
    #===================================================================================================
    # 19.2.8 Set Local Catalog
    #===================================================================================================
    $Script:CatalogLocal = "$OSBuilderContent\Updates\OSBuilder.json"
    if (Test-Path "$CatalogLocal") {$OSBuilderCatalogVersion = $(Get-Content $CatalogLocal | ConvertFrom-Json).Version}
    #===================================================================================================
    # 19.2.8 OSBuilder URLs
    #===================================================================================================
    $global:OSBuilderURL = "https://raw.githubusercontent.com/OSDeploy/OSBuilder.Public/master/OSBuilder.json"
    $global:OSBuilderCatalogURL = "https://raw.githubusercontent.com/OSDeploy/OSBuilder.Public/master/Content/Updates/OSBuilder.json"
    #===================================================================================================
    # 19.2.8 New Directories
    #===================================================================================================
    $OSBuilderNewDirectories = @(
        $OSBuilderPath
        $OSBuilderOSBuilds
        $OSBuilderOSMedia
        $OSBuilderPEBuilds
        $OSBuilderTasks
        $OSBuilderTemplates
        $OSBuilderContent
        "$OSBuilderContent\Drivers"
        "$OSBuilderContent\ExtraFiles"
        #"$OSBuilderContent\ExtraFiles\Win10 x64 1809"
        "$OSBuilderContent\Mount"
        "$OSBuilderContent\IsoExtract"
        "$OSBuilderContent\LanguagePacks"
        "$OSBuilderContent\OneDrive"
        "$OSBuilderContent\Packages"
        #"$OSBuilderContent\Packages\Win10 x64 1809"
        "$OSBuilderContent\Provisioning"
        #"$OSBuilderContent\Registry"
        "$OSBuilderContent\Scripts"
        "$OSBuilderContent\StartLayout"
        "$OSBuilderContent\Unattend"
        "$OSBuilderContent\Updates"
        #"$OSBuilderContent\Updates\Custom"
        "$OSBuilderContent\WinPE"
        "$OSBuilderContent\WinPE\ADK\Win10 x64 1809"
        "$OSBuilderContent\WinPE\DaRT\DaRT 10"
        "$OSBuilderContent\WinPE\Drivers"
        #"$OSBuilderContent\WinPE\Drivers\WinPE 10 x64"
        #"$OSBuilderContent\WinPE\Drivers\WinPE 10 x86"
        "$OSBuilderContent\WinPE\ExtraFiles"
        "$OSBuilderContent\WinPE\Scripts"
    )

    if ($CreatePaths.IsPresent) {
        foreach ($OSBuilderDir in $OSBuilderNewDirectories) {
            if (!(Test-Path "$OSBuilderDir")) {
                New-Item "$OSBuilderDir" -ItemType Directory -Force | Out-Null
            }
        }
    }

    #===================================================================================================
    # 19.2.8 Old Directories
    #===================================================================================================
    $OSBuilderOldDirectories = @(
        "$OSBuilderContent\UpdateStacks"
        "$OSBuilderContent\UpdateWindows"
    )

    foreach ($OSBuilderDir in $OSBuilderOldDirectories) {
        if (Test-Path "$OSBuilderDir") {
            Write-Warning "'$OSBuilderDir' is no longer required and should be removed"
        }
    }
    #===================================================================================================
    # 19.2.8 Write Map
    #===================================================================================================
    if ($HideDetails -eq $false) {
        if (Test-Path $OSBuilderPath)                   {Write-Host "Home: $OSBuilderPath" -ForegroundColor White}
            else                                        {Write-Host "Home: $OSBuilderPath (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderOSMedia)                {Write-Host "OSMedia: $OSBuilderOSMedia" -ForegroundColor Gray}
            else                                        {Write-Host "OSMedia: $OSBuilderOSMedia (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderOSBuilds)               {Write-Host "OSBuilds: $OSBuilderOSBuilds" -ForegroundColor Gray}
            else                                        {Write-Host "OSBuilds: $OSBuilderOSBuilds (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderPEBuilds)               {Write-Host "PEBuilds: $OSBuilderPEBuilds" -ForegroundColor Gray}
            else                                        {Write-Host "PEBuilds: $OSBuilderPEBuilds (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderTasks)                  {Write-Host "Tasks: $OSBuilderTasks" -ForegroundColor Gray}
            else                                        {Write-Host "Tasks: $OSBuilderTasks (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderTemplates)              {Write-Host "Templates: $OSBuilderTemplates" -ForegroundColor Gray}
            else                                        {Write-Host "Templates: $OSBuilderTemplates (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderContent)                {Write-Host "Content: $OSBuilderContent" -ForegroundColor Gray}
            else                                        {Write-Host "Content: $OSBuilderContent (does not exist)" -ForegroundColor Gray}
        if (Test-Path $OSBuilderContent\Drivers)        {Write-Host "-Drivers: $OSBuilderContent\Drivers" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Drivers: $OSBuilderContent\Drivers (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\ExtraFiles)     {Write-Host "-Extra Files: $OSBuilderContent\ExtraFiles" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Extra Files: $OSBuilderContent\ExtraFiles (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\Mount)          {Write-Host "-MountPath: $OSBuilderContent\Mount" -ForegroundColor DarkGray}
            else                                        {Write-Host "-MountPath: $OSBuilderContent\Mount (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\IsoExtract)     {Write-Host "-IsoExtract: $OSBuilderContent\IsoExtract" -ForegroundColor DarkGray}
            else                                        {Write-Host "-IsoExtract: $OSBuilderContent\IsoExtract (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\Packages)       {Write-Host "-Packages: $OSBuilderContent\Packages" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Packages: $OSBuilderContent\Packages (does not exist)" -ForegroundColor DarkGray}
        #if (Test-Path $OSBuilderContent\Provisioning) {Write-Host "-Provisioning: $OSBuilderContent\Provisioning" -ForegroundColor DarkGray}
        # else {Write-Host "-Provisioning: $OSBuilderContent\Provisioning (does not exist)" -ForegroundColor DarkGray}
        #if (Test-Path $OSBuilderContent\Registry) {Write-Host "-Registry: $OSBuilderContent\Registry" -ForegroundColor DarkGray}
        # else {Write-Host "-Registry: $OSBuilderContent\Registry (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\Scripts)        {Write-Host "-Scripts: $OSBuilderContent\Scripts" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Scripts: $OSBuilderContent\Scripts (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\StartLayout)    {Write-Host "-Start Layouts: $OSBuilderContent\StartLayout" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Start Layouts: $OSBuilderContent\StartLayout (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\Unattend)       {Write-Host "-Unattend XML: $OSBuilderContent\Unattend" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Unattend XML: $OSBuilderContent\Unattend (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\Updates)        {Write-Host "-Updates: $OSBuilderContent\Updates" -ForegroundColor DarkGray}
            else                                        {Write-Host "-Updates: $OSBuilderContent\Updates (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path $OSBuilderContent\WinPE)          {Write-Host "-WinPE Content: $OSBuilderContent\WinPE" -ForegroundColor DarkGray}
            else                                        {Write-Host "-WinPE Content: $OSBuilderContent\WinPE (does not exist)" -ForegroundColor DarkGray}
        Write-Host ""
    }

    #===================================================================================================
    # 19.2.13 OSBuilder Catalog Updates
    #===================================================================================================
    if ($HideDetails -eq $false) {
        $statuscode = try {(Invoke-WebRequest -Uri $OSBuilderCatalogURL -UseBasicParsing -DisableKeepAlive).StatusCode}
        catch [Net.WebException]{[int]$_.Exception.Response.StatusCode}
        if (!($statuscode -eq "200")) {
        } else {
            $LatestUpdateVersion = @()
            $LatestUpdateVersion = Invoke-RestMethod -Uri $OSBuilderCatalogURL

            if ([System.Version]$($LatestUpdateVersion.Version) -eq [System.Version]$OSBuilderCatalogVersion) {
                Write-Host "OSBuilder Update Catalogs $OSBuilderCatalogVersion" -ForegroundColor Green
            } else {
                if (Test-Path "$OSBuilderContent\Updates") {
                    Write-Warning "Updating $OSBuilderCatalogURL"
                    $statuscode = try {(Invoke-WebRequest -Uri $OSBuilderCatalogURL -UseBasicParsing -DisableKeepAlive).StatusCode}
                    catch [Net.WebException]{[int]$_.Exception.Response.StatusCode}
                    if (!($statuscode -eq "200")) {
                        Write-Warning "Could not connect to $OSBuilderCatalogURL (Status Code: $statuscode) ..."
                    } else {
                        #===================================================================================================
                        # 19.2.11 Auto Download Catalogs
                        #===================================================================================================
                        Invoke-WebRequest -Uri "$OSBuilderCatalogURL" -OutFile "$CatalogLocal"

                        if (Test-Path "$CatalogLocal") {
                            $OSBuilderCatalogVersion = $(Get-Content $CatalogLocal | ConvertFrom-Json).Version
                            Write-Host "OSBuilder Update Catalogs $OSBuilderCatalogVersion" -ForegroundColor Green
                            $CatalogJson = Get-Content -Path "$CatalogLocal" | ConvertFrom-Json
                            foreach ($item in $($CatalogJson.CatalogsDownload)) {
                                if (!(Test-Path "$OSBuilderContent\Updates\$(Split-Path "$($item)" -Leaf)")) {
                                    $statuscode = try {(Invoke-WebRequest -Uri $OSBuilderCatalogURL -UseBasicParsing -DisableKeepAlive).StatusCode}
                                    catch [Net.WebException]{[int]$_.Exception.Response.StatusCode}
                                    if ($statuscode -eq "200") {
                                        Write-Warning "Adding $OSBuilderContent\Updates\$(Split-Path $($item) -Leaf)"
                                        Invoke-WebRequest -Uri "$($item)" -OutFile "$OSBuilderContent\Updates\$(Split-Path "$($item)" -Leaf)"
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (Test-Path "$OSBuilderContent\Updates") {
                #Write-Host "Latest Mirosoft Updates:" -ForegroundColor Cyan
                foreach ($line in $($LatestUpdateVersion.Info)) {Write-Host $line -ForegroundColor DarkGray}
                Write-Host ""
            }
        }
    }
    #===================================================================================================
    # 19.2.13 OSBuilder Module
    #===================================================================================================
    if ($HideDetails -eq $false) {
        $statuscode = try {(Invoke-WebRequest -Uri $OSBuilderURL -UseBasicParsing -DisableKeepAlive).StatusCode}
        catch [Net.WebException]{[int]$_.Exception.Response.StatusCode}
        if (!($statuscode -eq "200")) {
        } else {
            $LatestModuleVersion = @()
            $LatestModuleVersion = Invoke-RestMethod -Uri $OSBuilderURL
            
            if ([System.Version]$($LatestModuleVersion.Version) -eq [System.Version]$OSBuilderVersion) {
                Write-Host "OSBuilder Module $OSBuilderVersion" -ForegroundColor Green
                foreach ($line in $($LatestModuleVersion.LatestUpdates)) {Write-Host $line -ForegroundColor DarkGray}
                Write-Host ""
                Write-Host "New Links:" -ForegroundColor Cyan
                foreach ($line in $($LatestModuleVersion.NewLinks)) {Write-Host $line -ForegroundColor Gray}
                Write-Host ""
                Write-Host "Helpful Links:" -ForegroundColor Cyan
                foreach ($line in $($LatestModuleVersion.HelpfulLinks)) {Write-Host $line -ForegroundColor Gray}
            } elseif ([System.Version]$($LatestModuleVersion.Version) -lt [System.Version]$OSBuilderVersion) {
                Write-Host "OSBuilder Module $OSBuilderVersion" -ForegroundColor Green
                Write-Warning "You are running a Pre-Release version of OSBuilder"
                Write-Host "Public Version: $($LatestModuleVersion.Version)" -ForegroundColor DarkGray
                foreach ($line in $($LatestModuleVersion.LatestUpdates)) {Write-Host $line -ForegroundColor DarkGray}
                Write-Host ""
                Write-Host "New Links:" -ForegroundColor Cyan
                foreach ($line in $($LatestModuleVersion.NewLinks)) {Write-Host $line -ForegroundColor Gray}
                Write-Host ""
                Write-Host "Helpful Links:" -ForegroundColor Cyan
                foreach ($line in $($LatestModuleVersion.HelpfulLinks)) {Write-Host $line -ForegroundColor Gray}
            } else {
                Write-Host "PowerShell Gallery: $($LatestModuleVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Installed Version: $OSBuilderVersion" -ForegroundColor DarkGray
                Write-Warning "Updated OSBuilder Module on PowerShell Gallery"
                foreach ($line in $($LatestModuleVersion.PSGallery)) {Write-Host $line -ForegroundColor DarkGray}
                Write-Host "Update Command Line: OSBuilder -Update" -ForegroundColor Cyan
            }
        }
    }
    #===================================================================================================
    # 19.2.15 Auto Remove Catalogs
    #===================================================================================================
    OSB-Upgrade-RemoveCatalogs
    #===================================================================================================
    # 19.2.15 Update Module
    #===================================================================================================
    if ($Update.IsPresent) {OSB-Upgrade-Module}
}