Public/Get-OSBuilder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<#
.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
    )
    Write-Warning "OSBuilder Module has been replaced by OSDBuilder"
    Write-Warning "Follow these commands:"
    Write-Host "Uninstall-Module OSBuilder -AllVersions -Force" -ForegroundColor Cyan
    Write-Host "Remove-Module OSBuilder -Force" -ForegroundColor Cyan
    Write-Host "Install-Module OSDBuilder" -ForegroundColor Cyan
    Write-Host "Import-Module OSDBuilder" -ForegroundColor Cyan
    Pause
    #===================================================================================================
    # 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}
}