Collect your drivers and export them to a share

Last week I saw a really interesting Tweet from Adam Eaddy mentioning his blog where he had published a automated way to build Configuration Manager Driver Packages automatically by collecting drivers from a remote PC. The script worked great, but I missed the nice folder structure to keep the drivers nice and ordered. I tweaked his script a bit, so it would fit my needs.

This PowerShell script collects the drivers (in a nice manufacturer\model\version\arch structure) and copies them to a network share. This makes it possible to use your own Configuration Manager driver import script (or import them manually) or even use them in your MDT configuration.

image

#==========================================================================
# NAME: CollectDrivers.ps1
#
# AUTHOR: Helmer Zandbergen
# DATE  : 04/24/2016
#
# COMMENT: This script detects driver of the current machine it is run on and
#          exports them to location mentioned in the parameter $SHARENAME
#
#
# SCRIPT INSPIRED BY : Adam Eaddy (https://adameaddy.wordpress.com)
#
# VERSION : 1.0
# 1.0 (26-apr-2016)- Initial Script.
#
# USE : .\CollectDrivers.ps1 "\\Servername\Share\Driver\location"
#==========================================================================
 
[CmdletBinding()]
Param (

[Parameter(Mandatory=$True, Position=0)]
[string]$SHARENAME

)
 
#Do not change these!
$COMPMANUFACTURER=(Get-WmiObject -Class win32_computersystem).Manufacturer

If ($COMPMANUFACTURER.ToUpper() -eq "LENOVO" ) {
    $COMPMODEL=(Get-WmiObject -Class win32_computersystemproduct).version
	}
Else{
	$COMPMODEL=(Get-WmiObject -Class win32_computersystem).Model
}

$OSVER=(Get-WmiObject -Class win32_operatingsystem).Caption
$OSARCH=(Get-WmiObject -Class win32_operatingsystem).OSArchitecture
$UTEMP=$env:TEMP
$DRIVERPATH ="$UTEMP\Drivers"
$LOGFILE ="$UTEMP\$COMPMODEL.log"
$NEWLOGFILE ="$UTEMP\Drivers\$COMPMANUFACTURER-$COMPMODEL-$OSVER-$OSARCH.log"
$DriverSource="$SHARENAME\$COMPMANUFACTURER\$COMPMODEL\$OSVER\$OSARCH"

Write-Host ""
Write-Host "Manufacturer: $COMPMANUFACTURER"
write-host "Model: $COMPMODEL"
Write-Host "OS Version: $OSVER ($OSARCH)"
Write-Host ""
Write-Host "I'm currently..."
 
#$ScriptBlock = { param($SHARENAME)
 
#Get Drivers
Set-Location C:
$SDrivers = Get-ChildItem -Path $SDriverSource -Include *.inf -Recurse

#End of declarations
 
#Export the drivers to a temp directory
If (Test-Path $NEWLOGFILE){
Write-Output "This script has been cancelled because it has already completed on this device." | out-file $NEWLOGFILE -Append
Write-Host "This script has been cancelled because it has already completed on this device."
}Else{
Write-Host "* Exporting drivers to temporary location."
Export-WindowsDriver -Destination "$DRIVERPATH\$COMPMANUFACTURER\$COMPMODEL\$OSVER\$OSARCH" -Online | out-file $LOGFILE
move-item $LOGFILE $NEWLOGFILE
Remove-Item $DRIVERPATH'\'$COMPMANUFACTURER'\'$COMPMODEL'\'$OSVER'\'$OSARCH'\'prn* -Recurse
}
 
#Copy Drivers to Driver Share
If (Test-Path $SHARENAME'\'$COMPMODEL){
Write-Output "This Copy has been cancelled because it has already completed for this device." | out-file $NEWLOGFILE -Append
Write-Host "This Copy has been cancelled because it has already completed on this device."
}Else{
Write-Host "* Copying drivers to destination share."
copy-item $DRIVERPATH'\*' -Destination $SHARENAME -Recurse
}
 
#Cleanup Temp directory
Write-Host "* Cleaning up temporary location."
Remove-Item "$UTEMP\Drivers"-Recurse
Write-Host ""
Write-Host "* Done!"
Write-Host ""

Note : you have to run the script as an Administrator.

Please let me know what you think!