Private/Pnp/Get-OSDDriverPnp.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
function Get-OSDDriverPnp {
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory = $true,ValueFromPipelineByPropertyName = $true)]
        [string]$ExpandedDriverPath,
        [switch]$GeForce,
        [switch]$NoHardwareIdRev,
        [switch]$NoHardwareIdSubsys
    )
    #======================================================================================
    # Validate Admin Rights
    #======================================================================================
    $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
    If (!( $isAdmin )) {
        Write-Host "Checking User Account Control settings ..." -ForegroundColor Green
        if ((Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA -eq 0) {
            #UAC Disabled
            Write-Host '========================================================================================' -ForegroundColor DarkGray
            Write-Host "User Account Control is Disabled ... " -ForegroundColor Green
            Write-Host "You will need to correct your UAC Settings ..." -ForegroundColor Green
            Write-Host "Try running this script in an Elevated PowerShell session ... Exiting" -ForegroundColor Green
            Write-Host '========================================================================================' -ForegroundColor DarkGray
            Start-Sleep -s 10
            Exit 0
        } else {
            #UAC Enabled
            Write-Host "UAC is Enabled" -ForegroundColor Green
            Start-Sleep -s 3
            if ($Silent) {
                Write-Host "-- Restarting as Administrator (Silent)" -ForegroundColor Cyan ; Start-Sleep -Seconds 1
                Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Silent" -Verb RunAs -Wait
            } elseif($Restart) {
                Write-Host "-- Restarting as Administrator (Restart)" -ForegroundColor Cyan ; Start-Sleep -Seconds 1
                Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Restart" -Verb RunAs -Wait
            } else {
                Write-Host "-- Restarting as Administrator" -ForegroundColor Cyan ; Start-Sleep -Seconds 1
                Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs -Wait
            }
            Exit 0
        }
    } else {
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
        #Write-Host "-- Running with Elevated Permissions ..." -ForegroundColor Cyan ; Start-Sleep -Seconds 1
        #Write-Host '========================================================================================' -ForegroundColor DarkGray
    }

    $OSDDriverPnp = @()

    if (Test-Path "$ExpandedDriverPath") {
        Get-ChildItem "$ExpandedDriverPath" autorun.inf -Recurse | ForEach-Object {
            $RenameMessage = "$(Get-Date) Renaming $($_.FullName) to $($_.Name).txt"
            Add-Content -Path "$ExpandedDriverPath\OSDDriver-Renames.txt" -Value $RenameMessage
            Write-Warning "Get-OSDDriverPnp: $RenameMessage"
            $_ | Rename-Item -NewName $_.Name.Replace('.inf', '.txt') -Force
        }

        Get-ChildItem "$ExpandedDriverPath" setup.inf -Recurse | ForEach-Object {
            $RenameMessage = "$(Get-Date) Renaming $($_.FullName) to $($_.Name).txt"
            Add-Content -Path "$ExpandedDriverPath\OSDDriver-Renames.txt" -Value $RenameMessage
            Write-Warning "Get-OSDDriverPnp: $RenameMessage"
            $_ | Rename-Item -NewName $_.Name.Replace('.inf', '.txt') -Force
        }

        $ExpandInfs = Get-ChildItem -Path "$ExpandedDriverPath" -Recurse -Include *.inf -File | Where-Object {$_.Name -notlike "*autorun.inf*"} | Select-Object -Property FullName
        foreach ($ExpandInf in $ExpandInfs) {
            Write-Host "Process: $($ExpandInf.FullName)" -ForegroundColor DarkGray

            $OSDDriverPnp += Get-WindowsDriver -Online -Driver "$($ExpandInf.FullName)" | `
            Select-Object -Property HardwareId,HardwareDescription,Version,ManufacturerName,`
            Architecture,ServiceName,CompatibleIds,ExcludeIds,Driver,Inbox,CatalogFile,ClassName,`
            ClassGuid,ClassDescription,BootCritical,DriverSignature,ProviderName,Date,MajorVersion,`
            MinorVersion,Build,Revision | Sort-Object HardwareId
        }
    }
    #===================================================================================================
    # Filter
    #===================================================================================================
    #$OSDDriverPnp = $OSDDriverPnp | Where-Object {$_.HardwareId -notlike "SWC*"}
    $OSDDriverPnp = $OSDDriverPnp | Where-Object {$_.HardwareId -ne 'PCI\VEN_8086'}
    $OSDDriverPnp = $OSDDriverPnp | Where-Object {$_.HardwareId -notlike "{*"}
    if ($GeForce.IsPresent) {$OSDDriverPnp = $OSDDriverPnp | Where-Object {$_.HardwareDescription -match "GeForce"}}
    foreach ($Pnp in $OSDDriverPnp) {
        $Pnp.HardwareId = ($Pnp.HardwareId -split '\&CC')[0]
        if ($NoHardwareIdRev.IsPresent) {$Pnp.HardwareId = ($Pnp.HardwareId -split '\&REV')[0]}
        if ($NoHardwareIdSubsys.IsPresent) {$Pnp.HardwareId = ($Pnp.HardwareId -split '\&SUBSYS')[0]}
        #if ($Pnp.HardwareId -match 'PCI\\') {
            #$HardwareId = $Pnp.HardwareId -split '&'
            #$Pnp.HardwareId = "$($HardwareId[0])&$($HardwareId[1])"
        #}
    }
    $OSDDriverPnp = $OSDDriverPnp | Sort-Object HardwareId -Unique
    #===================================================================================================
    # Return
    #===================================================================================================
    Return $OSDDriverPnp
}