Public/New-OSBMediaISO.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
<#
.SYNOPSIS
Creates an ISO of any OSBuilder Media

.DESCRIPTION
Creates an ISO of any OSBuilder Media (OSMedia, OSBuilds, PEBuilds)

.LINK
https://www.osdeploy.com/osbuilder/docs/functions/media/new-osbmediaiso

.PARAMETER FullName
Full Path of the OSBuilder Media
#>

function New-OSBMediaISO {
    [CmdletBinding()]
    PARAM (
        [Parameter(ValueFromPipelineByPropertyName)]
        [string[]]$FullName
    )

    BEGIN {
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
        #Write-Host "$($MyInvocation.MyCommand.Name) BEGIN" -ForegroundColor Green

        #===================================================================================================
        Write-Verbose '19.1.1 Initialize OSBuilder'
        #===================================================================================================
        Get-OSBuilder -CreatePaths -HideDetails

        #===================================================================================================
        Write-Verbose '19.2.10 Gather All OS Media'
        #===================================================================================================
        $AllMyOSMedia = @()
        $AllMyOSMedia = Get-OSMedia

        $AllMyOSBuilds = @()
        $AllMyOSBuilds = Get-OSBuilds

        $AllMyPEBuilds = @()
        $AllMyPEBuilds = Get-PEBuilds

        $AllMyOSBMedia = @()
        $AllMyOSBMedia = [array]$AllMyOSMedia + [array]$AllMyOSBuilds + [array]$AllMyPEBuilds

        #===================================================================================================
        Write-Verbose '19.1.1 Locate OSCDIMG'
        #===================================================================================================
        if (Test-Path "$OSBuilderContent\Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe") {
            $oscdimg = "$OSBuilderContent\Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe"
        } elseif (Test-Path "C:\Program Files\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe") {
            $oscdimg = "C:\Program Files\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe"
        } elseif (Test-Path "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe") {
            $oscdimg = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe"
        } else {
            Write-Host '========================================================================================' -ForegroundColor DarkGray
            Write-Warning "Could not locate OSCDIMG in Windows ADK at:"
            Write-Warning "${env:ProgramFiles(x86)}\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe"
            Write-Warning "You can optionally copy OSCDIMG to:"
            Write-Warning "$OSBuilderContent\Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\oscdimg.exe"
            Break
        }
        Write-Verbose "OSCDIMG: $oscdimg"
    }

    PROCESS {
        Write-Host '========================================================================================' -ForegroundColor DarkGray
        Write-Host "$($MyInvocation.MyCommand.Name) PROCESS" -ForegroundColor Green

        #===================================================================================================
        Write-Verbose '19.1.14 Select Source OSMedia'
        #===================================================================================================
        $SelectedOSMedia = @()

        if ($FullName) {
            foreach ($Item in $FullName) {
                Write-Verbose "Checking $Item"
                $SelectedOSMedia += $AllMyOSBMedia | Where-Object {$_.FullName -eq $Item}
            }
        } else {
            $SelectedOSMedia = $AllMyOSBMedia | Out-GridView -Title "OSBuilder: Select one or more OSMedia to create an ISO and press OK (Cancel to Exit)" -PassThru
        }

        #===================================================================================================
        Write-Verbose '19.1.1 Process OSMedia'
        #===================================================================================================
        foreach ($Media in $SelectedOSMedia) {
            $ISOSourceFolder = "$($Media.FullName)\OS"
            $ISODestinationFolder = "$($Media.FullName)\ISO"
            if (!(Test-Path $ISODestinationFolder)) {New-Item $ISODestinationFolder -ItemType Directory -Force | Out-Null}



            #$ISOFile = "$ISODestinationFolder\$($Media.Name).iso"

            $PEImage = Import-CliXml -Path "$($Media.FullName)\info\xml\Get-WindowsImage.xml"
            #$PEImage = $PEImage

            $OSArchitecture = $($PEImage.Architecture)
            if ($OSArchitecture -eq '0') {$OSArchitecture = 'x86'}
            if ($OSArchitecture -eq '6') {$OSArchitecture = 'ia64'}
            if ($OSArchitecture -eq '9') {$OSArchitecture = 'x64'}
            if ($OSArchitecture -eq '12') {$OSArchitecture = 'x64 ARM'}

            $UBR = $($PEImage.UBR)

            $OSImageName = $($PEImage.ImageName)
            $OSImageName = $OSImageName -replace '\(', ''
            $OSImageName = $OSImageName -replace '\)', ''

            if ($Media.MediaType -eq "PEBuild") {
                $OSImageName = "WinPE $($Media.Arch) $($Media.ReleaseId) $($Media.UBR)"
                $ISOFile = "$ISODestinationFolder\WinPE $($Media.Arch) $($Media.ReleaseId) $($Media.UBR).iso"
                if ($Media.ReleaseId -eq 7601) {
                    $OSImageName = "WinPE3 $($Media.Arch) SP1 $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\WinPE3 $($Media.Arch) SP1 $($Media.UBR).iso"
                }
                if ($Media.ReleaseId -eq 9600) {
                    $OSImageName = "WinPE5 $($Media.Arch) $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\WinPE5 $($Media.Arch) $($Media.UBR).iso"
                }
                if ($Media.MajorVersion -eq 10) {
                    $OSImageName = "WinPE10 $($Media.Arch) $($Media.ReleaseId) $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\WinPE10 $($Media.Arch) $($Media.ReleaseId) $($Media.UBR).iso"
                }
            } else {
                if ($Media.ReleaseId -eq 7601) {
                    $OSImageName = "Win7 $($Media.Arch) SP1 $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\Win7 $($Media.Arch) SP1 $($Media.UBR).iso"
                }
                if ($Media.ReleaseId -eq 9600) {
                    $OSImageName = "Server $($Media.Arch) $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\Server 2012 R2 $($Media.Arch) $($Media.UBR).iso"
                }
                if ($Media.MajorVersion -eq 10 -and $Media.InstallationType -eq 'Client') {
                    $OSImageName = "Win10 $($Media.Arch) $($Media.ReleaseId) $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\Win10 $($Media.Arch) $($Media.ReleaseId) $($Media.UBR).iso"
                }
                if ($Media.MajorVersion -eq 10 -and $Media.InstallationType -like "*Server*") {
                    $OSImageName = "Server $($Media.Arch) $($Media.ReleaseId) $($Media.UBR)"
                    $ISOFile = "$ISODestinationFolder\Server $($Media.Arch) $($Media.ReleaseId) $($Media.UBR).iso"
                }
            }

            # 32 character limit for a Label
            # 23 = Win10 Edu x64 17134.112
            # 25 = Win10 Edu N x64 17134.112
            # 23 = Win10 Ent x64 17134.112
            # 25 = Win10 Ent N x64 17134.112
            # 23 = Win10 Pro x64 17134.112
            # 27 = Win10 Pro Edu x64 17134.112
            # 29 = Win10 Pro EduN x64 17134.112
            # 27 = Win10 Pro Wks x64 17134.112
            # 26 = Win10 Pro N x64 17134.112
            # 29 = Win10 Pro N Wks x64 17134.112
            $ISOLabel = '-l"{0}"' -f $OSImageName
            $ISOFolder = "$($Media.FullName)\ISO"
            if (!(Test-Path $ISOFolder)) {New-Item -Path $ISOFolder -ItemType Directory -Force | Out-Null}

            if (!(Test-Path $ISOSourceFolder)) {
                Write-Warning "Could not locate $ISOSourceFolder"
                Write-Warning "Make sure you have proper OS before using New-OSBMediaISO"
                Return
            }
            $etfsboot = "$ISOSourceFolder\boot\etfsboot.com"
            if (!(Test-Path $etfsboot)) {
                Write-Warning "Could not locate $etfsboot"
                Write-Warning "Make sure you have proper OS before using New-OSBMediaISO"
                Return
            }

            $efisys = "$ISOSourceFolder\efi\microsoft\boot\efisys.bin"
            if (Test-Path "$ISOSourceFolder\efi\microsoft\boot\efisys.bin") {
                $efisys = "$ISOSourceFolder\efi\microsoft\boot\efisys.bin"
            } elseif (Test-Path "$OSBuilderContent\Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin") {
                $efisys = "$OSBuilderContent\Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin"
            } elseif (Test-Path "C:\Program Files\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin") {
                $efisys = "C:\Program Files\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin"
            } elseif (Test-Path "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin") {
                $efisys = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$env:PROCESSOR_ARCHITECTURE\Oscdimg\efisys.bin"
            } else {
                Write-Warning "Could not locate $efisys"
                Write-Warning "Make sure you have proper OS before using New-OSBMediaISO"
                Return
            }

            Write-Host "Label: $OSImageName" -ForegroundColor Cyan
            Write-Host "Creating: $ISOFile" -ForegroundColor Cyan
            $data = '2#p0,e,b"{0}"#pEF,e,b"{1}"' -f $etfsboot, $efisys
            start-process $oscdimg -args @("-m","-o","-u2","-bootdata:$data",'-u2','-udfver102',$ISOLabel,"`"$ISOSourceFolder`"", "`"$ISOFile`"") -Wait
        }
    }

    END {
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
        #Write-Host "$($MyInvocation.MyCommand.Name) END" -ForegroundColor Green
    }
}