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

.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
    )
    #===================================================================================================
    # 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\Cat.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/Cat.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\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.9 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

            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}
            Write-Host ""
            
            if ([System.Version]$($LatestModuleVersion.Version) -eq [System.Version]$OSBuilderVersion) {
                Write-Host "OSBuilder Module $OSBuilderVersion" -ForegroundColor Green
            } elseif ([System.Version]$($LatestModuleVersion.Version) -lt [System.Version]$OSBuilderVersion) {
                Write-Warning "You are running a Pilot version of OSBuilder"
                Write-Host "PowerShell Gallery: $($LatestModuleVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Local Version: $OSBuilderVersion" -ForegroundColor DarkGray
                Write-Host ""
            } else {
                Write-Warning "Updated OSBuilder Module on PowerShell Gallery"
                foreach ($line in $($LatestModuleVersion.PSGallery)) {Write-Host $line -ForegroundColor Cyan}
                Write-Host "PowerShell Gallery: $($LatestModuleVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Local Version: $OSBuilderVersion" -ForegroundColor DarkGray
                Write-Host ""
                Write-Host "Update Steps:" -ForegroundColor Cyan
                Write-Host "Uninstall-Module -Name OSBuilder -AllVersions -Force" -ForegroundColor Gray
                Write-Host "Install-Module -Name OSBuilder -Force" -ForegroundColor Gray
                Write-Host "Import-Module -Name OSBuilder -Force" -ForegroundColor Gray
                Write-Host ""
            }
            #Write-Host "Latest Module Updates:" -ForegroundColor Cyan
            foreach ($line in $($LatestModuleVersion.LatestUpdates)) {Write-Host $line -ForegroundColor DarkGray}
            Write-Host ""
        }
    }
    #===================================================================================================
    # 19.2.9 OSBuilder 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 {
                Write-Warning "Updated OSBuilder Microsoft Update Catalogs on GitHub"
                Write-Host "GitHub Version: $($LatestUpdateVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Local Version: $OSBuilderCatalogVersion" -ForegroundColor DarkGray
                Write-Host ""
                Write-Host "Update Steps:" -ForegroundColor Cyan
                Write-Host "Get-OSBUpdate -UpdateCatalogs" -ForegroundColor Gray
                Write-Host ""
            }
            #Write-Host "Latest Mirosoft Updates:" -ForegroundColor Cyan
            foreach ($line in $($LatestUpdateVersion.Info)) {Write-Host $line -ForegroundColor DarkGray}
        }
    }
}