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
    )
    #===================================================================================================
    # Check if OSMedia is installed and prompt for removal 18.10.2
    #===================================================================================================
    Get-OSMediaModule
    #===================================================================================================
    # OSBuilder Version 19.1.28
    #===================================================================================================
    $global:OSBuilderVersion = $(Get-Module -Name OSBuilder).Version
    if ($HideDetails -eq $false) {
        Write-Host "Version $OSBuilderVersion" -ForegroundColor Cyan
        Write-Host ""
    }
    #===================================================================================================
    # Create Empty Registry Key and Values 18.10.1
    #===================================================================================================
    if (!(Test-Path HKCU:\Software\OSDeploy\OSBuilder)) {New-Item HKCU:\Software\OSDeploy -Name OSBuilder -Force | Out-Null}
    #===================================================================================================
    # Set Global OSBuilder Path 18.10.1
    #===================================================================================================
    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"}
    #===================================================================================================
    # Set Primary Paths 18.10.1
    #===================================================================================================
    $global:OSBuilderContent = "$OSBuilderPath\Content"
    $global:OSBuilderOSBuilds = "$OSBuilderPath\OSBuilds"
    $global:OSBuilderOSMedia = "$OSBuilderPath\OSMedia"
    $global:OSBuilderPEBuilds = "$OSBuilderPath\PEBuilds"
    $global:OSBuilderTasks = "$OSBuilderPath\Tasks"
    $global:OSBuilderTemplates = "$OSBuilderPath\Templates"
    #===================================================================================================
    # Set Local Catalog 18.10.1
    #===================================================================================================
    $Script:CatalogLocal = "$OSBuilderContent\Updates\Cat.json"
    if (Test-Path "$CatalogLocal") {$OSBuilderCatalogVersion = $(Get-Content $CatalogLocal | ConvertFrom-Json).Version}
    #===================================================================================================
    # OSBuilder URLs 18.10.2
    #===================================================================================================
    $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"
    #===================================================================================================
    # Create Paths 18.10.1
    #===================================================================================================
    if ($CreatePaths.IsPresent) {
        if (!(Test-Path "$OSBuilderPath"))                                  {New-Item "$OSBuilderPath" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderOSBuilds"))                              {New-Item "$OSBuilderOSBuilds" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderOSMedia"))                               {New-Item "$OSBuilderOSMedia" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderPEBuilds"))                              {New-Item "$OSBuilderPEBuilds" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderTasks"))                                 {New-Item "$OSBuilderTasks" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderTemplates"))                             {New-Item "$OSBuilderTemplates" -ItemType Directory -Force | Out-Null}

        if (!(Test-Path "$OSBuilderContent"))                               {New-Item "$OSBuilderContent" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Drivers"))                       {New-Item "$OSBuilderContent\Drivers" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\ExtraFiles"))                    {New-Item "$OSBuilderContent\ExtraFiles" -ItemType Directory -Force | Out-Null}
        #if (!(Test-Path "$OSBuilderContent\ExtraFiles\Win10 x64 1809")) {New-Item "$OSBuilderContent\ExtraFiles\Win10 x64 1809" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Mount"))                         {New-Item "$OSBuilderContent\Mount" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\IsoExtract"))                    {New-Item "$OSBuilderContent\IsoExtract" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\LanguagePacks"))                 {New-Item "$OSBuilderContent\LanguagePacks" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Packages"))                      {New-Item "$OSBuilderContent\Packages" -ItemType Directory -Force | Out-Null}
        #if (!(Test-Path "$OSBuilderContent\Packages\Win10 x64 1809")) {New-Item "$OSBuilderContent\Packages\Win10 x64 1809" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Provisioning"))                  {New-Item "$OSBuilderContent\Provisioning" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Registry"))                   {New-Item "$OSBuilderContent\Registry" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Scripts"))                       {New-Item "$OSBuilderContent\Scripts" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\StartLayout"))                   {New-Item "$OSBuilderContent\StartLayout" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Unattend"))                      {New-Item "$OSBuilderContent\Unattend" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\Updates"))                       {New-Item "$OSBuilderContent\Updates" -ItemType Directory -Force | Out-Null}
        #if (!(Test-Path "$OSBuilderContent\Updates\Custom")) {New-Item "$OSBuilderContent\Updates\Custom" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE"))                         {New-Item "$OSBuilderContent\WinPE" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE\ADK\Win10 x64 1809"))      {New-Item "$OSBuilderContent\WinPE\ADK\Win10 x64 1809" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE\DaRT\DaRT 10"))            {New-Item "$OSBuilderContent\WinPE\DaRT\DaRT 10" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE\Drivers"))                 {New-Item "$OSBuilderContent\WinPE\Drivers" -ItemType Directory -Force | Out-Null}
        #if (!(Test-Path "$OSBuilderContent\WinPE\Drivers\WinPE 10 x64")) {New-Item "$OSBuilderContent\WinPE\Drivers\WinPE 10 x64" -ItemType Directory -Force | Out-Null}
        #if (!(Test-Path "$OSBuilderContent\WinPE\Drivers\WinPE 10 x86")) {New-Item "$OSBuilderContent\WinPE\Drivers\WinPE 10 x86" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE\ExtraFiles"))              {New-Item "$OSBuilderContent\WinPE\ExtraFiles" -ItemType Directory -Force | Out-Null}
        if (!(Test-Path "$OSBuilderContent\WinPE\Scripts"))                 {New-Item "$OSBuilderContent\WinPE\Scripts" -ItemType Directory -Force | Out-Null}
    }
    #===================================================================================================
    # Remove Old Updates 18.10.1
    #===================================================================================================
    if (Test-Path "$OSBuilderContent\UpdateStacks")     {Write-Warning "Directory '$OSBuilderContent\UpdateStacks' is no longer required and can be removed"}
    if (Test-Path "$OSBuilderContent\UpdateWindows")    {Write-Warning "Directory '$OSBuilderContent\UpdateWindows' is no longer required and can be removed"}
    #===================================================================================================
    # Write Map 18.10.1
    #===================================================================================================
    if ($HideDetails -eq $false) {
        if (Test-Path $OSBuilderPath)                   {Write-Host "OSBuilder Home: $OSBuilderPath" -ForegroundColor White}
            else                                        {Write-Host "OSBuilder Home: $OSBuilderPath (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderOSBuilds)               {Write-Host "OSBuilds: $OSBuilderOSBuilds" -ForegroundColor White}
            else                                        {Write-Host "OSBuilds: $OSBuilderOSBuilds (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderOSMedia)                {Write-Host "OSMedia: $OSBuilderOSMedia" -ForegroundColor White}
            else                                        {Write-Host "OSMedia: $OSBuilderOSMedia (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderPEBuilds)               {Write-Host "PEBuilds: $OSBuilderPEBuilds" -ForegroundColor White}
            else                                        {Write-Host "PEBuilds: $OSBuilderPEBuilds (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderTasks)                  {Write-Host "Tasks: $OSBuilderTasks" -ForegroundColor White}
            else                                        {Write-Host "Tasks: $OSBuilderTasks (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderTemplates)              {Write-Host "Templates: $OSBuilderTemplates" -ForegroundColor White}
            else                                        {Write-Host "Templates: $OSBuilderTemplates (does not exist)" -ForegroundColor White}
        if (Test-Path $OSBuilderContent)                {Write-Host "Content: $OSBuilderContent" -ForegroundColor White}
            else                                        {Write-Host "Content: $OSBuilderContent (does not exist)" -ForegroundColor White}
        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.1.25 Important Links
    #===================================================================================================
    if ($HideDetails -eq $false) {
        #Write-Host "OSBuilder Links:" -ForegroundColor Cyan
        Write-Host "https://www.osdeploy.com/osbuilder/release-information.html"
        Write-Host "https://www.osdeploy.com/osbuilder/docs/functions"
        Write-Host ""
    }

    #===================================================================================================
    # 19.1.25 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
            foreach ($line in $($LatestModuleVersion.News)) {Write-Host $line -ForegroundColor Cyan}
            Write-Host ""

            if ([System.Version]$($LatestModuleVersion.Version) -eq [System.Version]$OSBuilderVersion) {
                Write-Host "OSBuilder Module: OK" -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 "Newer OSBuilder Module on PowerShell Gallery"
                Write-Host "Uninstall-Module -Name OSBuilder -AllVersions -Force" -ForegroundColor Green
                Write-Host "Install-Module -Name OSBuilder -Scope CurrentUser -Force" -ForegroundColor Green
                Write-Host "Import-Module -Name OSBuilder -Force" -ForegroundColor Green
                Write-Host "PowerShell Gallery: $($LatestModuleVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Local Version: $OSBuilderVersion" -ForegroundColor DarkGray
                foreach ($line in $($LatestModuleVersion.Info)) {Write-Host $line -ForegroundColor Cyan}
                Write-Host ""
            }
        }
    }
    #===================================================================================================
    # 19.1.25 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: OK" -ForegroundColor Green
            } else {
                Write-Warning "Newer OSBuilder Update Catalog on GitHub"
                Write-Host "Get-OSBUpdate -UpdateCatalogs" -ForegroundColor Green
                Write-Host "GitHub Version: $($LatestUpdateVersion.Version)" -ForegroundColor DarkGray
                Write-Host "Local Version: $OSBuilderCatalogVersion" -ForegroundColor DarkGray
                foreach ($line in $($LatestUpdateVersion.Info)) {Write-Host $line -ForegroundColor Cyan}
                Write-Host ""
            }
        }
    }
}