Private/Cab/New-CabFileOSDDriver.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
<#
.LINK
    https://www.osdeploy.com/psmodule/osdrivers/
.SYNOPSIS
    Creates a CAB file from a Directory or Child Directories
.DESCRIPTION
    Creates a CAB file from a Directory or Child Directories
.PARAMETER Path
    Directory to create the CAB from
.PARAMETER HighCompression
    Forces LZX High Compression (Slower). Unchecked is MSZIP Fast Compression
.PARAMETER MakeCABsFromSubDirs
    Creates CAB files from Path Subdirectories
.EXAMPLE
    New-AutoDriverCabFile -Path C:\Temp\Dell\LatitudeE10_A01
    Creates MSZIP Fast Compression CAB from of C:\Temp\Dell\LatitudeE10_A01
.EXAMPLE
    New-AutoDriverCabFile -Path C:\Temp\Dell -HighCompression -MakeCABsFromSubDirs
    Creates LZX High Compression CABS from all subdirectories of C:\Temp\Dell
.NOTES
    NAME: New-AutoDriverCabFile.ps1
    AUTHOR: David Segura, david@segura.org
    BLOG: http://www.osdeploy.com
    CREATED: 02/18/2018
    VERSION: 1.1.0.2
#>

function New-CabFileOSDDriver
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$ExpandedDriverPath,
        
        [Parameter(Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]$PublishPath,

        [switch]$HighCompression = $false,
        [switch]$RemoveDirective = $false,
        [switch]$RemoveSource = $false,
        [switch]$ShowOutput = $false
    )

    Begin {}

    Process {
        #===================================================================================================
        # Defaults
        #===================================================================================================
        $HighCompression = $true
        $RemoveDirective = $false
        #===================================================================================================
        # SourceName
        #===================================================================================================
        $SourceName = (Get-Item $ExpandedDriverPath).Name
        #===================================================================================================
        # PublishPath
        #===================================================================================================
        if ($PublishPath) {
            if ( ! ( Test-Path $PublishPath ) ) { New-Item -Type Directory -Path $PublishPath | Out-Null }
        } else {
            $PublishPath = (Get-Item $ExpandedDriverPath).Parent.FullName
        }
        #===================================================================================================
        # CabName
        #===================================================================================================
        $CabName = "$SourceName.cab"
        #===================================================================================================
        # OSDDriver.drvpnp
        #===================================================================================================
        if (Test-Path "$ExpandedDriverPath\OSDDriver.drvpnp") {Copy-Item -Path "$ExpandedDriverPath\OSDDriver.drvpnp" -Destination "$PublishPath\$SourceName.drvpnp"}
        #===================================================================================================
        # Directive
        #===================================================================================================
        $DirectivePath = Join-Path -Path $PublishPath -ChildPath "$SourceName.ddf"

        $DirectiveString = [System.Text.StringBuilder]::new()
        [void]$DirectiveString.AppendLine(';*** MakeCAB Directive file;')
        [void]$DirectiveString.AppendLine('.OPTION EXPLICIT')
        [void]$DirectiveString.AppendLine(".Set CabinetNameTemplate=$CabName")
        [void]$DirectiveString.AppendLine(".Set DiskDirectory1=$PublishPath")
        [void]$DirectiveString.AppendLine('.Set Cabinet=ON')
        [void]$DirectiveString.AppendLine('.Set Compress=ON')
        if ($HighCompression.IsPresent) {[void]$DirectiveString.AppendLine('.Set CompressionType=LZX')}
        else {[void]$DirectiveString.AppendLine('.Set CompressionType=MSZIP')}
        [void]$DirectiveString.AppendLine('.Set CabinetFileCountThreshold=0')
        [void]$DirectiveString.AppendLine('.Set FolderFileCountThreshold=0')
        [void]$DirectiveString.AppendLine('.Set FolderSizeThreshold=0')
        [void]$DirectiveString.AppendLine('.Set MaxCabinetSize=0')
        [void]$DirectiveString.AppendLine('.Set MaxDiskFileCount=0')
        [void]$DirectiveString.AppendLine('.Set MaxDiskSize=0')
        #===================================================================================================
        # Unblock
        #===================================================================================================
        Get-ChildItem $ExpandedDriverPath -Recurse | Unblock-File
        #===================================================================================================
        # SourceContent
        #===================================================================================================
        $SourceContent = @()
        $SourceContent = Get-ChildItem -Recurse $ExpandedDriverPath | Where-Object { -Not($_.PsIsContainer)}
        #===================================================================================================
        # OSDDriver-DDF0.clixml
        #===================================================================================================
        #Write-Host "Generating Content Directive: $ExpandedDriverPath\OSDDriver-DDF0.clixml" -ForegroundColor Gray
        $SourceContent | Select-Object -ExpandProperty Fullname | Export-Clixml "$ExpandedDriverPath\OSDDriver-DDF0.clixml" -Force
        #===================================================================================================
        # Complete Directive
        #===================================================================================================
        $SourceContent | Select-Object -ExpandProperty Fullname | Foreach-Object {
            [void]$DirectiveString.AppendLine("""$_"" ""$($_.SubString($ExpandedDriverPath.Length + 1))""")
        }
        #===================================================================================================
        # MakeCab
        #===================================================================================================
        Write-Verbose "Compressing $ExpandedDriverPath" -Verbose
        $DirectiveString.ToString() | Out-File -FilePath $DirectivePath -Encoding UTF8
        if ($ShowOutput.IsPresent) {
            makecab /F $DirectivePath
        } else {
            #makecab /F $DirectivePath | Out-Null
            cmd /c "makecab /F ""$DirectivePath""" '>nul' # | Out-Null
        }
        #===================================================================================================
        # Cleanup
        #===================================================================================================
        #if (Test-Path 'setup.inf') {Remove-Item 'setup.inf' -Force}
        #if (Test-Path 'setup.rpt') {Remove-Item 'setup.rpt' -Force}
        if (Test-Path "$ExpandedDriverPath\OSDDriver-Devices.csv") {
            Copy-Item "$ExpandedDriverPath\OSDDriver-Devices.csv" -Destination "$PublishPath\$SourceName.csv" -Force | Out-Null
            #Remove-Item "$ExpandedDriverPath\OSDDriver-Devices.csv" -Force | Out-Null
        }
        #if (Test-Path "$ExpandedDriverPath\OSDDriver.drvpnp") {Remove-Item "$ExpandedDriverPath\OSDDriver.drvpnp" -Force | Out-Null}
        if (Test-Path "$ExpandedDriverPath\OSDDriver-DDF0.clixml") {Remove-Item "$ExpandedDriverPath\OSDDriver-DDF0.clixml" -Force | Out-Null}
        if (Test-Path "$ExpandedDriverPath\OSDDriver-DDF1.clixml") {Remove-Item "$ExpandedDriverPath\OSDDriver-DDF1.clixml" -Force | Out-Null}
        if ($RemoveDirective.IsPresent) {Remove-Item $DirectivePath -Force | Out-Null}
        if ($RemoveSource.IsPresent) {Remove-Item -Path $ExpandedDriverPath -Recurse -Force | Out-Null}
    }

    End {}
}