New-OSDriverCAB.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
<#
.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-OSDriverCAB -Path C:\Temp\Dell\LatitudeE10_A01
 Creates MSZIP Fast Compression CAB from of C:\Temp\Dell\LatitudeE10_A01
.EXAMPLE
 New-OSDriverCAB -Path C:\Temp\Dell -HighCompression -MakeCABsFromSubDirs
 Creates LZX High Compression CABS from all subdirectories of C:\Temp\Dell
.NOTES
 NAME:    New-OSDriverCAB.ps1
 AUTHOR:    David Segura, david@segura.org
 BLOG:    http://www.osdeploy.com
 CREATED:    02/18/2018
 VERSION:    1.1.0.2
#>


function New-OSDriverCAB
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$Path,
        [switch]$HighCompression,
        [switch]$MakeCABsFromSubDirs
    )
    
    If ($HighCompression) {
        $Compress = ".Set CompressionType=LZX"
    } Else { 
        $Compress = ".Set CompressionType=MSZIP"
    }
    
    If ($MakeCABsFromSubDirs) {
        Get-ChildItem -Directory -Path $Path | ForEach ($_) {
        PSMakeCAB $_.FullName }
    } Else {
        PSMakeCAB $Path
    }

}

function PSMakeCAB($Path)
{
    #Set the CAB File Name
    $CabFileName = (Get-Item $Path).Name
    $CabFileNameExt = (Get-Item $Path).Name + ".cab"
    
    #Set the Destination Directory
    $DestinationDir = (Get-Item $Path).Parent.FullName
    
    Write-Host "Creating " $DestinationDir\$CabFileNameExt
    
    $ddf = ";*** MakeCAB Directive file;
.OPTION EXPLICIT
.Set CabinetNameTemplate=$CabFileNameExt
.Set DiskDirectory1=$DestinationDir
.Set Cabinet=on
.Set Compress=on
$Compress
.Set CabinetFileCountThreshold=0
.Set FolderFileCountThreshold=0
.Set FolderSizeThreshold=0
.Set MaxCabinetSize=0
.Set MaxDiskFileCount=0
.Set MaxDiskSize=0
"

    $PathFullName = (Get-Item $Path).fullname
    #Remove Streams
    Get-ChildItem $PathFullName -Recurse | Unblock-File
    $ddfpath = ($DestinationDir+"\$CabFileName.ddf")
    $ddf += (ls -recurse $Path | ? {!$_.psiscontainer}|select -expand fullname|%{'"'+$_+'" "'+$_.SubString($PathFullName.length+1)+'"'}) -join "`r`n"
    $ddf
    $ddf | Out-File -encoding UTF8 $ddfpath
    makecab /F $ddfpath
    #rm $ddfpath
    #rm setup.inf
    #rm setup.rpt
}