Public/Get-OSDBuilder.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
<#
.SYNOPSIS
Offline Servicing for Windows 7, Windows 10, Windows Server 2012 R2, Windows Server 2016 and Windows Server 2019
 
.DESCRIPTION
Offline Servicing for Windows 7, Windows 10, Windows Server 2012 R2, Windows Server 2016 and Windows Server 2019
 
.LINK
http://osdbuilder.com/docs/functions/get-osdbuilder
 
.PARAMETER CreatePaths
Creates empty directories used by OSDBuilder
 
.PARAMETER HideDetails
Hides Write-Host output. Used when called from other functions
 
.PARAMETER SetPath
Changes the path from the default of C:\OSDBuilder to the path specified
 
.PARAMETER Update
Updates the OSDBuilder Module
 
.EXAMPLE
Get-OSDBuilder -SetPath D:\OSDBuilder
Sets the OSDBuilder Main directory to D:\OSDBuilder
 
.EXAMPLE
Get-OSDBuilder -CreatePaths
Creates empty directories used by OSDBuilder
 
.EXAMPLE
Get-OSDBuilder -HideDetails
Method for refreshing all OSDBuilder Variables. Used by other OSDBuilder Functions
#>

function Get-OSDBuilder {
    [CmdletBinding()]
    PARAM (
        [switch]$CreatePaths,
        [switch]$HideDetails,
        [string]$SetPath,
        [switch]$Update
    )
    #===================================================================================================
    # Verify Single Version of OSDBuilder
    #===================================================================================================
    if ((Get-Module -Name OSDBuilder).Count -gt 1) {
        Write-Warning "Multiple OSDBuilder Modules are loaded"
        Write-Warning "Close all open PowerShell sessions before using OSDBuilder"
        Exit
    }
    #===================================================================================================
    # 19.3.9 OSDBuilder URLs
    #===================================================================================================
    $global:OSDBuilderURL = "https://raw.githubusercontent.com/OSDeploy/OSDBuilder.Public/master/OSDBuilder.json"
    #===================================================================================================
    # 19.3.9 Remove Existing Modules
    #===================================================================================================
    #OSD-Remove-ModuleOSBuilder
    #OSD-Remove-ModuleOSMedia
    #===================================================================================================
    # 19.2.8 Get OSDBuilder Version
    #===================================================================================================
    $global:OSDBuilderVersion = $(Get-Module -Name OSDBuilder | Sort-Object Version | Select-Object Version -Last 1).Version
    if ($HideDetails -eq $false) {
        Write-Host "OSDBuilder $OSDBuilderVersion" -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 OSDBuilder
    #===================================================================================================
    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:OSDBuilderPath = $(Get-ItemProperty "HKCU:\Software\OSDeploy").OSBuilderPath
    if (!($OSDBuilderPath)) {$global:OSDBuilderPath = "$Env:SystemDrive\OSDBuilder"}
    #===================================================================================================
    # 19.3.15 Set Primary Paths
    #===================================================================================================
    $global:OSDBuilderContent =      "$OSDBuilderPath\Content"
    $global:OSDBuilderOSBuilds =     "$OSDBuilderPath\OSBuilds"
    $global:OSDBuilderOSMedia =      "$OSDBuilderPath\OSMedia"
    $global:OSDBuilderPEBuilds =     "$OSDBuilderPath\PEBuilds"
    $global:OSDBuilderTasks =        "$OSDBuilderPath\Tasks"
    $global:OSDBuilderTemplates =    "$OSDBuilderPath\Templates"

    $global:OSBuilderContent =      "$OSDBuilderPath\Content"
    $global:OSBuilderOSBuilds =     "$OSDBuilderPath\OSBuilds"
    $global:OSBuilderOSMedia =      "$OSDBuilderPath\OSMedia"
    $global:OSBuilderPEBuilds =     "$OSDBuilderPath\PEBuilds"
    $global:OSBuilderTasks =        "$OSDBuilderPath\Tasks"
    $global:OSBuilderTemplates =    "$OSDBuilderPath\Templates"
    #===================================================================================================
    # 19.3.4 New Directories
    #===================================================================================================
    $OSDBuilderNewDirectories = @(
        $OSDBuilderPath
        $OSDBuilderOSBuilds
        $OSDBuilderOSMedia
        $OSDBuilderPEBuilds
        $OSDBuilderTasks
        $OSDBuilderTemplates
        $OSDBuilderContent
        "$OSDBuilderContent\ADK"
        "$OSDBuilderContent\ADK\Windows 10 1809\Windows Preinstallation Environment"
        "$OSDBuilderContent\DaRT"
        "$OSDBuilderContent\DaRT\DaRT 10"
        "$OSDBuilderContent\Drivers"
        "$OSDBuilderContent\ExtraFiles"
        "$OSDBuilderContent\IsoExtract"
        "$OSDBuilderContent\IsoExtract\Windows 10 1809 FOD x64"
        "$OSDBuilderContent\IsoExtract\Windows 10 1809 Language"
        "$OSDBuilderContent\IsoExtract\Windows Server 2019 1809 FOD x64"
        "$OSDBuilderContent\IsoExtract\Windows Server 2019 1809 Language"
        #"$OSDBuilderContent\LanguagePacks"
        "$OSDBuilderContent\Mount"
        "$OSDBuilderContent\OneDrive"
        "$OSDBuilderContent\OSDUpdate"
        "$OSDBuilderContent\Packages"
        #"$OSDBuilderContent\Packages\Win10 x64 1809"
        #"$OSDBuilderContent\Provisioning"
        #"$OSDBuilderContent\Registry"
        "$OSDBuilderContent\Scripts"
        "$OSDBuilderContent\StartLayout"
        "$OSDBuilderContent\Unattend"
        #"$OSDBuilderContent\Updates"
        #"$OSDBuilderContent\Updates\Custom"
        #"$OSDBuilderContent\WinPE"
        #"$OSDBuilderContent\WinPE\ADK\Win10 x64 1809"
        #"$OSDBuilderContent\WinPE\DaRT\DaRT 10"
        #"$OSDBuilderContent\WinPE\Drivers"
        #"$OSDBuilderContent\WinPE\Drivers\WinPE 10 x64"
        #"$OSDBuilderContent\WinPE\Drivers\WinPE 10 x86"
        #"$OSDBuilderContent\WinPE\ExtraFiles"
        #"$OSDBuilderContent\WinPE\Scripts"
    )

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

    #===================================================================================================
    # 19.2.8 Old Directories
    #===================================================================================================
    $OSDBuilderOldDirectories = @(
        "$OSDBuilderContent\UpdateStacks"
        "$OSDBuilderContent\UpdateWindows"
    )

    foreach ($OSDBuilderDir in $OSDBuilderOldDirectories) {
        if (Test-Path "$OSDBuilderDir") {
            Write-Warning "'$OSDBuilderDir' is no longer required and should be removed"
        }
    }
    #===================================================================================================
    # 19.3.4 Write Map
    #===================================================================================================
    if ($HideDetails -eq $false) {
        if (Test-Path "$OSDBuilderPath")                {Write-Host "Home: $OSDBuilderPath" -ForegroundColor White}
            else                                        {Write-Host "Home: $OSDBuilderPath (does not exist)" -ForegroundColor White}
        if (Test-Path "$OSDBuilderOSMedia")             {Write-Host "OSMedia: $OSDBuilderOSMedia" -ForegroundColor Gray}
            else                                        {Write-Host "OSMedia: $OSDBuilderOSMedia (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderOSBuilds")            {Write-Host "OSBuilds: $OSDBuilderOSBuilds" -ForegroundColor Gray}
            else                                        {Write-Host "OSBuilds: $OSDBuilderOSBuilds (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderPEBuilds")            {Write-Host "PEBuilds: $OSDBuilderPEBuilds" -ForegroundColor Gray}
            else                                        {Write-Host "PEBuilds: $OSDBuilderPEBuilds (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderTasks")               {Write-Host "Tasks: $OSDBuilderTasks" -ForegroundColor Gray}
            else                                        {Write-Host "Tasks: $OSDBuilderTasks (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderTemplates")           {Write-Host "Templates: $OSDBuilderTemplates" -ForegroundColor Gray}
            else                                        {Write-Host "Templates: $OSDBuilderTemplates (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderContent")             {Write-Host "Content: $OSDBuilderContent" -ForegroundColor Gray}
            else                                        {Write-Host "Content: $OSDBuilderContent (does not exist)" -ForegroundColor Gray}
<# if (Test-Path "$OSDBuilderContent\Drivers") {Write-Host "-Drivers: $OSDBuilderContent\Drivers" -ForegroundColor DarkGray}
            else {Write-Host "-Drivers: $OSDBuilderContent\Drivers (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\ExtraFiles") {Write-Host "-Extra Files: $OSDBuilderContent\ExtraFiles" -ForegroundColor DarkGray}
            else {Write-Host "-Extra Files: $OSDBuilderContent\ExtraFiles (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\IsoExtract") {Write-Host "-IsoExtract: $OSDBuilderContent\IsoExtract" -ForegroundColor DarkGray}
            else {Write-Host "-IsoExtract: $OSDBuilderContent\IsoExtract (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\Mount") {Write-Host "-MountPath: $OSDBuilderContent\Mount" -ForegroundColor DarkGray}
            else {Write-Host "-MountPath: $OSDBuilderContent\Mount (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\ADK") {Write-Host "-ADK: $OSDBuilderContent\ADK" -ForegroundColor DarkGray}
            else {Write-Host "-ADK: $OSDBuilderContent\ADK (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\DaRT") {Write-Host "-DaRT: $OSDBuilderContent\DaRT" -ForegroundColor DarkGray}
            else {Write-Host "-DaRT: $OSDBuilderContent\DaRT (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\OneDrive") {Write-Host "-OneDrive: $OSDBuilderContent\OneDrive" -ForegroundColor DarkGray}
            else {Write-Host "-OneDrive: $OSDBuilderContent\OneDrive (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\OSDUpdate") {Write-Host "-OSDUpdate: $OSDBuilderContent\OSDUpdate" -ForegroundColor Gray}
            else {Write-Host "-OSDUpdate: $OSDBuilderContent\OSDUpdate (does not exist)" -ForegroundColor Gray}
        if (Test-Path "$OSDBuilderContent\Packages") {Write-Host "-Packages: $OSDBuilderContent\Packages" -ForegroundColor DarkGray}
            else {Write-Host "-Packages: $OSDBuilderContent\Packages (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\Scripts") {Write-Host "-Scripts: $OSDBuilderContent\Scripts" -ForegroundColor DarkGray}
            else {Write-Host "-Scripts: $OSDBuilderContent\Scripts (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\StartLayout") {Write-Host "-Start Layouts: $OSDBuilderContent\StartLayout" -ForegroundColor DarkGray}
            else {Write-Host "-Start Layouts: $OSDBuilderContent\StartLayout (does not exist)" -ForegroundColor DarkGray}
        if (Test-Path "$OSDBuilderContent\Unattend") {Write-Host "-Unattend XML: $OSDBuilderContent\Unattend" -ForegroundColor DarkGray}
            else {Write-Host "-Unattend XML: $OSDBuilderContent\Unattend (does not exist)" -ForegroundColor DarkGray} #>

        Write-Host ""
    }
    #===================================================================================================
    # 19.3.9 OSDBuilder Online
    #===================================================================================================
    if ($HideDetails -eq $false) {
        $statuscode = try {(Invoke-WebRequest -Uri $OSDBuilderURL -UseBasicParsing -DisableKeepAlive).StatusCode}
        catch [Net.WebException]{[int]$_.Exception.Response.StatusCode}
        if (!($statuscode -eq "200")) {
        } else {
            $LatestModuleVersion = @()
            $LatestModuleVersion = Invoke-RestMethod -Uri $OSDBuilderURL
            
            if ([System.Version]$($LatestModuleVersion.Version) -eq [System.Version]$OSDBuilderVersion) {
                Write-Host "OSDBuilder Module $OSDBuilderVersion" -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]$OSDBuilderVersion) {
                Write-Host "OSDBuilder Module $OSDBuilderVersion" -ForegroundColor Green
                Write-Warning "You are running a Pre-Release version of OSDBuilder"
                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: $OSDBuilderVersion" -ForegroundColor DarkGray
                Write-Warning "Updated OSDBuilder Module on PowerShell Gallery"
                foreach ($line in $($LatestModuleVersion.PSGallery)) {Write-Host $line -ForegroundColor DarkGray}
                Write-Host "Update Module Command: OSDBuilder -Update" -ForegroundColor Cyan
            }
        }
    }
    #===================================================================================================
    # 19.3.10 Update Module
    #===================================================================================================
    if ($Update.IsPresent) {Update-ModuleOSDBuilder}
}