Driver Installation

Driver Installation using PSADT

Single Driver – Install

$driverFile = "caboem.inf"
$driverVersion = "5.1.7.5277"
$driverPath = "$dirFiles\caboem.inf"
$installed = Get-WindowsDriver -Online -All | Where-Object {
    ($_.OriginalFileName -split '\\')[-1] -ieq $driverFile -and $_.Version -eq $driverVersion
}
if (-not $installed) {
    Write-Log -Message "Installing missing driver: $driverFile version $driverVersion" -Severity 1
    try {
        Execute-Process -Path "pnputil.exe" -Parameters "/add-driver `"$driverPath`" /install" -Wait -IgnoreExitCodes 259 -ErrorAction Stop
        Write-Log -Message "Successfully installed: $driverFile" -Severity 1
    } catch {
        Write-Log -Message "Failed to install $driverFile. Error: $_" -Severity 3
    }
} else {
    Write-Log -Message "Driver already installed: $driverFile version $driverVersion" -Severity 1
}

Single Driver – Uninstall

$driverFile = "caboem.inf"
$driverVersion = "5.1.7.5277"
$driverPath = "$dirFiles\caboem.inf"
$installed = Get-WindowsDriver -Online -All | Where-Object {
    ($_.OriginalFileName -split '\\')[-1] -ieq $driverFile -and $_.Version -eq $driverVersion
}
if ($installed) {
    Write-Log -Message "Uninstalling driver: $driverFile version $driverVersion" -Severity 1
    try {
        Execute-Process -Path "pnputil.exe" -Parameters "/delete-driver `"$driverPath`" /uninstall /force" -Wait -IgnoreExitCodes '-536870339,1168' -ErrorAction Stop
        Write-Log -Message "Successfully uninstalled: $driverFile" -Severity 1
    } catch {
        Write-Log -Message "Failed to uninstall $driverFile. Error: $_" -Severity 3
    }
} else {
    Write-Log -Message "Driver not found or already uninstalled: $driverFile version $driverVersion" -Severity 1
}

Single Driver – Detection

$driverFile = "caboem.inf"
$driverVersion = "5.1.7.5277"
$installed = Get-WindowsDriver -Online -All | Where-Object {
    ($_.OriginalFileName -split '\\')[-1] -eq $driverFile -and $_.Version -eq $driverVersion
}
if ($installed) {
    Write-Host "Expected driver is installed."
    exit 0
} else {
    Write-Host "Missing: $driverFile version $driverVersion"
    exit 1
}

Multiple Drivers – Install

$drivers = @(
    @{ File = "ser2at.inf"; Version = "3.8.15.5"; Path = "$dirFiles\0\ser2at.inf" },
    @{ File = "FtdiBus.inf"; Version = "2.12.36.4"; Path = "$dirFiles\1\FtdiBus.inf" },
    @{ File = "FtdiPort.inf"; Version = "2.12.36.4"; Path = "$dirFiles\2\FtdiPort.inf" },
    @{ File = "FtdiPort.inf"; Version = "2.12.0.0"; Path = "$dirFiles\3\FtdiPort.inf" }
)
foreach ($driver in $drivers) {
    $driverFile = $driver.File
    $driverVersion = $driver.Version
    $driverPath = $driver.Path
    $installed = Get-WindowsDriver -Online -All | Where-Object {
        ($_.OriginalFileName -split '\\')[-1] -ieq $driverFile -and $_.Version -eq $driverVersion
    }
    if (-not $installed) {
        Write-Log -Message "Installing missing driver: $driverFile version $driverVersion" -Severity 1
        try {
            Execute-Process -Path "pnputil.exe" -Parameters "/add-driver `"$driverPath`" /install" -Wait -IgnoreExitCodes 259 -ErrorAction Stop
            Write-Log -Message "Successfully installed: $driverFile" -Severity 1
        } catch {
            Write-Log -Message "Failed to install $driverFile. Error: $_" -Severity 3
        }
    } else {
        Write-Log -Message "Driver already installed: $driverFile version $driverVersion" -Severity 1
    }
}       

Multiple Drivers – Uninstall

$drivers = @(
    @{ File = "ser2at.inf"; Version = "3.8.15.5"; Path = "$dirFiles\0\ser2at.inf" },
    @{ File = "FtdiBus.inf"; Version = "2.12.36.4"; Path = "$dirFiles\1\FtdiBus.inf" },
    @{ File = "FtdiPort.inf"; Version = "2.12.36.4"; Path = "$dirFiles\2\FtdiPort.inf" },
    @{ File = "FtdiPort.inf"; Version = "2.12.0.0"; Path = "$dirFiles\3\FtdiPort.inf" }
)
foreach ($driver in $drivers) {
    $driverFile = $driver.File
    $driverVersion = $driver.Version
    $driverPath = $driver.Path
    $installed = Get-WindowsDriver -Online -All | Where-Object {
        ($_.OriginalFileName -split '\\')[-1] -ieq $driverFile -and $_.Version -eq $driverVersion
    }
    if ($installed) {
        Write-Log -Message "Uninstalling driver: $driverFile version $driverVersion" -Severity 1
        try {
            Execute-Process -Path "pnputil.exe" -Parameters "/delete-driver `"$driverPath`" /uninstall /force" -Wait -IgnoreExitCodes '-536870339,1168' -ErrorAction Stop
            Write-Log -Message "Successfully uninstalled: $driverFile" -Severity 1
        } catch {
            Write-Log -Message "Failed to uninstall $driverFile. Error: $_" -Severity 3
        }
    } else {
        Write-Log -Message "Driver not found or already uninstalled: $driverFile version $driverVersion" -Severity 1
    }
}

Multiple Drivers – Detection

$driversToCheck = @(
    @{ File = "ser2at.inf"; Version = "3.8.15.5" },
    @{ File = "FtdiBus.inf"; Version = "2.12.36.4" },
    @{ File = "FtdiPort.inf"; Version = "2.12.0.0" },
    @{ File = "FtdiPort.inf"; Version = "2.12.36.4" }
)
# Flag to track missing drivers
$allInstalled = $true
foreach ($driver in $driversToCheck) {
    $installed = Get-WindowsDriver -Online -All | Where-Object {
        ($_.OriginalFileName -split '\\')[-1] -eq $driver.File -and $_.Version -eq $driver.Version
    }
    if ($installed) {
        Write-Host "Installed: $($driver.File) version $($driver.Version)"
    } else {
        Write-Host "Missing: $($driver.File) version $($driver.Version)"
        $allInstalled = $false
    }
}
if ($allInstalled) {
    Write-Host "All expected drivers are installed."
    exit 0
} else {
    Write-Host "One or more drivers are missing or have incorrect versions."
    exit 1
}