Export-OSDrivers.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
<#
.LINK
    https://www.osdeploy.com/psmodule/osdrivers/
.SYNOPSIS
    Exports an existing OSDrivers Repository to a new one. This can be used for Package Creation
.DESCRIPTION
    Exports an existing OSDrivers Repository to a new one. This can be used for Package Creation
.PARAMETER PrimaryOSDrivers
    The OSDrivers Primary Repository (Source)
.PARAMETER SecondaryOSDrivers
    The new OSDrivers Repository (Destination)
.PARAMETER WindowsProfile
    Select an Operating System for Compatible Drivers to Export
.PARAMETER Like
    Wildcard include for Driver Packages in the PrimaryOSDrivers. eg. *Dell*
.PARAMETER NotLike
    Wildcard exclude for Driver Packages in the PrimaryOSDrivers. eg. *Lenovo*
.PARAMETER Subset
    Filter for Core, Video, and Make Drivers
.PARAMETER Visibility
    Filter for Public and Private Drivers
.EXAMPLE
    Export-OSDrivers -PrimaryOSDrivers D:\OSDrivers -SecondaryOSDrivers "D:\Packages\Win10 x64 Video Nvidia" -WindowsProfile "Win10 x64" -Like *nvidia* -Subset "Only Video"
    Exports D:\OSDrivers to "D:\Packages\Win10 x64 Video Nvidia" with only Win10 compatible Nvidia Video Drivers.
.NOTES
    NAME: Export-OSDrivers.ps1
    AUTHOR: David Segura, david@segura.org
    BLOG: http://www.osdeploy.com
    CREATED: 02/18/2018
    VERSION: 1.1.0.2
#>


function Export-OSDrivers
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$PrimaryOSDrivers,
        [Parameter(Mandatory=$True)]
        [string]$SecondaryOSDrivers,
        [Parameter(Mandatory=$True)]
        [ValidateSet('Win10 x64','Win10 x86','Win7 x64','Win7 x86','All Operating Systems','All x64','All x86')]
        [string]$WindowsProfile,
        [string]$Like,
        [string]$NotLike,
        [ValidateSet('Only Core','Only Video','Only Dell','Only HP','Only Lenovo')]
        [string]$Subset,
        [ValidateSet('Public Files Hidden','Public Files Only','Private Files Hidden','Private Files Only','Public and Private Hidden','Public and Private Only')]
        [string]$Visibility
    )
    
    #Get a list of all files in the PrimaryOSDrivers
    $SourceFiles = Get-ChildItem $PrimaryOSDrivers -File -Recurse

    #Filter out OSDrivers.vbs
    $OSDriversVBS = $SourceFiles | Where-Object {$_.name -like 'OSDrivers.vbs'}
    $SourceFiles = $SourceFiles | Where-Object {$_.name -notlike 'OSDrivers.vbs'}
    
    #Filter out the OS and Architecture
    if ($WindowsProfile -like 'Win10*') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike '*Win7*'}}
    if ($WindowsProfile -like 'Win7*') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike '*Win10*'}}
    if ($WindowsProfile -like '*x64') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike '*x86*'}}
    if ($WindowsProfile -like '*x86') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike '*x64*'}}
    
    #Wildcards
    if ("$Like" -ne "") {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -like "$Like"}}
    if ("$NotLike" -ne "") {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike "$NotLike"}}
    
    #Subset
    if ($Subset -eq 'Only Core') {$SourceFiles = $SourceFiles | Where-Object {$_.directoryname -like '*\Core*'}}
    if ($Subset -eq 'Only Video') {$SourceFiles = $SourceFiles | Where-Object {$_.directoryname -like '*\Video*'}}
    if ($Subset -eq 'Only Dell') {$SourceFiles = $SourceFiles | Where-Object {$_.directoryname -like '*\Dell*' -or $_.directoryname -like '*\X-Dell*'}}
    if ($Subset -eq 'Only HP') {$SourceFiles = $SourceFiles | Where-Object {$_.directoryname -like '*\HP*'}}
    if ($Subset -eq 'Only Lenovo') {$SourceFiles = $SourceFiles | Where-Object {$_.directoryname -like '*\Lenovo*'}}
    
    #Visibility
    if ($Visibility -eq 'Public Files Hidden') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike "*\Public\*"}}
    if ($Visibility -eq 'Public Files Only') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -like "*\Public\*"}}
    if ($Visibility -eq 'Private Files Hidden') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike "*\Private\*"}}
    if ($Visibility -eq 'Private Files Only') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -like "*\Private\*"}}
    if ($Visibility -eq 'Public and Private Hidden') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -notlike "*\Public\*" -and $_.fullname -notlike "*\Private\*"}}
    if ($Visibility -eq 'Public and Private Only') {$SourceFiles = $SourceFiles | Where-Object {$_.fullname -like "*\Public\*" -or $_.fullname -like "*\Private\*"}}
    
    $SourceFiles = $SourceFiles | Select-Object -Property DirectoryName, Name, Length, Extension, CreationTime, LastWriteTime, FullName | Out-GridView -Title "Select files to export and press OK" -PassThru
    
    foreach ($file in $SourceFiles) {
        $NewDir = $file.DirectoryName.Replace($PrimaryOSDrivers, $SecondaryOSDrivers)
        If (-Not (Test-Path $NewDir))
            {
            md $NewDir
            }
        Copy-Item -Path $file.FullName -Destination $NewDir
        
        If ($OSDriversVBS -ne $null) {
            Copy-Item -Path $OSDriversVBS.FullName -Destination $SecondaryOSDrivers
        }
    }
}