Public/Functions/split/Convert-FolderToIso.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


function Convert-FolderToIso {
    [CmdletBinding(PositionalBinding = $false)]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('FullName')]
        [string]$folderFullName,

        [string]$isoFullName = $null,

        [ValidateLength(1,16)]
        [string]$isoLabel = 'FolderToIso',

        [System.Management.Automation.SwitchParameter]$noPrompt
    )
    #=================================================
    # Blocks
    #=================================================
    Block-WinPE
    Block-StandardUser
    Block-PowerShellVersionLt5
    Block-WindowsVersionNe10
    Block-WindowsReleaseIdLt1703
    #=================================================
    # Make sure the folder we are iso'ing exists
    #=================================================
    if (! (Test-Path $folderFullName)) {
        Write-Warning "Convert-FolderToIso: folderFullName does not exist at $folderFullName"
        Break
    }
    #=================================================
    # Make sure folder is a folder
    #=================================================
    if ((Get-Item $folderFullName) -isnot [System.IO.DirectoryInfo]) {
        Write-Warning "Convert-FolderToIso: folderFullName is not a folder"
        Break
    }
    #=================================================
    # isoFullName
    #=================================================
    $GetItem = Get-Item -Path $folderFullName
    if (! ($isoFullName)) {
        $isoFullName = Join-Path (get-item -Path "T:\DevBox\Win11_22000.318").Parent.FullName ($GetItem.BaseName + '.iso')
    }
    #=================================================
    # Variables
    #=================================================
    Write-Verbose -Verbose "folderFullName: $folderFullName"
    Write-Verbose -Verbose "isoFullName: $isoFullName"
    Write-Verbose -Verbose "isoLabel: $isoLabel"
    #=================================================
    # Test-FolderToIso
    #=================================================
    $Params = @{
        folderFullName = $folderFullName
        isoFullName = $isoFullName
        isoLabel = $isoLabel
    }

    if (Test-FolderToIso @Params) {
        #=================================================
        # Get Adk Paths
        #=================================================
        $AdkPaths = Get-AdkPaths
        #=================================================
        # oscdimg.exe
        #=================================================
        $oscdimgexe = $AdkPaths.oscdimgexe
        Write-Verbose -Verbose "oscdimgexe: $oscdimgexe"
        #=================================================
        # Bootable
        #=================================================
        $isBootable = $true
        $folderBoot = Join-Path $folderFullName 'boot'
        if (! (Test-Path $folderBoot)) {
            Write-Warning "Convert-FolderToIso: folderFullName does not contain a Boot directory at $folderBoot"
            $isBootable = $false
        }
        $etfsbootcom = Join-Path $folderBoot 'etfsboot.com'
        if (! (Test-Path $etfsbootcom)) {
            Write-Warning "Convert-FolderToIso: folderFullName is missing $etfsbootcom"
            $isBootable = $false
        }

        $folderEfiBoot = Join-Path $folderFullName 'efi\microsoft\boot'
        if (! (Test-Path $folderEfiBoot)) {
            Write-Warning "Convert-FolderToIso: folderFullName does not contain a Boot directory at $folderEfiBoot"
            $isBootable = $false
        }
        $efisysbin = Join-Path $folderEfiBoot 'efisys.bin'
        if (! (Test-Path $efisysbin)) {
            Write-Warning "Convert-FolderToIso: folderFullName is missing $efisysbin"
        }
        $efisysnopromptbin = Join-Path $folderEfiBoot 'efisys_noprompt.bin'
        if (! (Test-Path $efisysnopromptbin)) {
            Write-Warning "Convert-FolderToIso: folderFullName is missing $efisysnopromptbin"
        }
        if ((Test-Path $efisysbin) -or (Test-Path $efisysnopromptbin)) {
            #Bootable
        }
        else {
            $isBootable = $false
        }
        #=================================================
        # Strings
        #=================================================
        $isoLabelString = '-l"{0}"' -f "$isoLabel"
        Write-Verbose -Verbose "isoLabelString: $isoLabelString"
        #=================================================
        # Create ISO
        #=================================================
        if ($isBootable) {
            if (($noPrompt) -and (Test-Path $efisysnopromptbin)) {
                $BootDataString = '2#p0,e,b"{0}"#pEF,e,b"{1}"' -f "$etfsbootcom", "$efisysnopromptbin"
                Write-Verbose -Verbose "BootDataString: $BootDataString"
                $Process = Start-Process $oscdimgexe -args @("-m","-o","-u2","-bootdata:$BootDataString",'-u2','-udfver102',$isoLabelString,"`"$folderFullName`"", "`"$isoFullName`"") -PassThru -Wait -NoNewWindow
            }
            else {
                $BootDataString = '2#p0,e,b"{0}"#pEF,e,b"{1}"' -f "$etfsbootcom", "$efisysbin"
                Write-Verbose -Verbose "BootDataString: $BootDataString"
                $Process = Start-Process $oscdimgexe -args @("-m","-o","-u2","-bootdata:$BootDataString",'-u2','-udfver102',$isoLabelString,"`"$folderFullName`"", "`"$isoFullName`"") -PassThru -Wait -NoNewWindow
            }
        }
        else {
            $Process = Start-Process $oscdimgexe -args @("-m","-o","-u2",'-udfver102',$isoLabelString,"`"$folderFullName`"", "`"$isoFullName`"") -PassThru -Wait -NoNewWindow
        }
    
        if (Test-Path $isoFullName) {
            Get-Item -Path $isoFullName
        }
        else {
            Write-Error "Something didn't work"
        }
        #=================================================
    }
    else {
        Write-Warning 'Test-FolderToIso failed with one or more errors'
    }
}