Advertisement
marcd2k20

OSD Dell Driver Injector

Mar 27th, 2025 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 8.08 KB | Source Code | 0 0
  1. #Initialize Task Sequence Environment
  2. $TSEnv=New-Object -COMObject Microsoft.SMS.TSEnvironment
  3. $SMSTSLogPath=$TSEnv.Value("_SMSTSLogPath")
  4. $OSDTargetSystemDrive=$TSEnv.Value("OSDTargetSystemDrive")
  5.  
  6. #Disable Progress bars to speed up Invoke-WebRequest
  7. $ProgressPreference = 'SilentlyContinue'
  8.  
  9. #Define Log file path
  10. $Script:Logfile="$($SMSTSLogPath)\Apply-Drivers.log"
  11.  
  12.  
  13. #Function to write a log entry, with handling for file locks
  14. Function Write-LogEntry
  15. {
  16.     Param($Value)
  17.     $Tries=0
  18.     $Success=$False
  19.     Do {
  20.         Try{
  21.             if ($Value-ne " "){
  22.                 $Timestamp="[$((Get-Date).ToString())] "
  23.             } else {
  24.                 $Timestamp=""
  25.             }
  26.             Add-Content -Value "$($Timestamp)$($Value)" -Path $Script:LogFile -Force -ErrorAction Stop
  27.             $Success=$True
  28.         }
  29.         Catch{
  30.             $Success=$False
  31.             $Tries++
  32.             Start-Sleep -Seconds 1
  33.         }  
  34.     }
  35.     Until ($Success -eq $True -or $Tries -ge 5)
  36. }
  37.  
  38. #Function to delete files created by the script
  39. Function Cleanup-Temp
  40. {
  41.     Write-LogEntry -Value " "
  42.     Write-LogEntry -Value "Cleaning up Temp files"
  43.     Try{
  44.         Remove-Item "$($TempFolder)" -Recurse -Force -ErrorAction Stop
  45.         Write-LogEntry -Value "Successfully cleaned up Temp files"
  46.     }
  47.     Catch {
  48.         Write-LogEntry -Value "Failed to clean up Temp files, you will need to manually remove $($TempFolder)"
  49.     }
  50. }
  51.  
  52. #Function to clean up and provide exit code before exiting script
  53. Function Exit-Script
  54. {
  55.     Param($ExitCode)
  56.     Cleanup-Temp
  57.     Write-LogEntry -Value " "
  58.     Write-LogEntry -Value "Script Completed with exit code $($ExitCode)"
  59.     Exit $ExitCode
  60. }
  61.  
  62. #Create Log File
  63. If (Test-Path $LogFile){
  64.     Remove-Item $LogFile -Force
  65. }
  66. Write-LogEntry -Value "Started Driver Injection Script"
  67.  
  68. #Create Temp Folder
  69. Write-LogEntry -Value " "
  70. Write-LogEntry -Value "Creating Drivers folder in Temp"
  71. $TempFolder="$($OSDTargetSystemDrive)\Drivers"
  72. If (!(Test-Path $TempFolder)){
  73.     Try{
  74.         New-Item -Path "$($OSDTargetSystemDrive)" -Name "Drivers" -ItemType Directory -Force -ErrorAction Stop
  75.         Write-LogEntry -Value "Successfully created $($TempFolder) folder"
  76.     }
  77.     Catch{
  78.         Write-LogEntry -Value "Failed to create $($TempFolder) folder"
  79.         Exit-Script -ExitCode 100
  80.     }
  81. } else {
  82.     Write-LogEntry -Value "$($TempFolder) folder already exists"
  83. }
  84.  
  85. #Get System SKU
  86. Write-LogEntry -Value " "
  87. Write-LogEntry -Value "Getting System SKU value from WMI"
  88. Try {
  89.     $Script:SystemSku="$((Get-WMIObject -Namespace root\WMI -Class MS_SystemInformation -ErrorAction Stop).SystemSku)"
  90.     Write-LogEntry -Value "Retrieved System SKU value: $($Script:SystemSku)"
  91. }
  92. Catch{
  93.     Write-LogEntry -Value "Failed to retrieve System SKU value"
  94.     Exit-Script -ExitCode 101
  95. }
  96.  
  97. #Download Model Catalog XML
  98. Write-LogEntry -Value " "
  99. Write-LogEntry -Value "Downloading Driver Pack Catalog XML"
  100. Try{
  101.     $Script:CatalogCABName="DriverPackCatalog.cab"
  102.     Invoke-WebRequest -Uri "https://downloads.dell.com/catalog/$Script:CatalogCABName" -OutFile "$($TempFolder)\$Script:CatalogCABName" -ErrorAction Stop
  103.     Write-LogEntry -Value "Successfully downloaded https://downloads.dell.com/catalog/$Script:CatalogCABName"
  104. }
  105. Catch{
  106.     Write-LogEntry -Value "Failed to download https://downloads.dell.com/catalog/$Script:CatalogCABName"
  107.     Exit-Script -ExitCode 102
  108. }
  109.  
  110. #Extract Catalog XML
  111. Write-LogEntry -Value " "
  112. Write-LogEntry -Value "Extracting Driver Pack Catalog XML"
  113. Try{
  114.     $Script:CatalogXMLName = $Script:CatalogCABName -Replace "cab","xml"
  115.     Start-Process "$($env:windir)\system32\expand.exe" -WindowStyle Hidden -ArgumentList "`"$($TempFolder)\$($Script:CatalogCABName)`" `"$($TempFolder)\$($Script:CatalogXMLName)`"" -Wait -ErrorAction Stop
  116.     Write-LogEntry -Value "Successfully extracted $($Script:CatalogXMLName) from $($Script:CatalogCABName)"
  117. }
  118. Catch{
  119.     Write-LogEntry -Value "Failed to extract $($Script:CatalogXMLName) from $($Script:CatalogCABName)"
  120.     Exit-Script -ExitCode 103    
  121. }
  122.  
  123. #Get Highest Driver Pack Version from XML
  124. Write-LogEntry -Value " "
  125. Write-LogEntry -Value "Finding Latest Driver Pack Version"
  126. $Script:DriversFound -eq $False
  127. Try{
  128.     [xml]$Script:DriversXML= Get-Content "$($TempFolder)\$($Script:CatalogXMLName)" -ErrorAction Stop
  129.     $Script:DriverPacks=@($Script:DriversXML.DriverPackManifest.DriverPackage | Where-Object {$_.SupportedSystems.Brand.Model.systemID -eq $Script:SystemSku -and $_.supportedoperatingsystems.operatingsystem.oscode -like "*11"})
  130.     $Script:DriverPacks = $Script:DriverPacks | ForEach-Object {
  131.         [PSCustomObject]@{
  132.             Version = $_.dellVersion
  133.             PaddedVersion=[version]$(
  134.                 $Padded=$_.dellVersion -replace "[^0-9.]", "" -replace "^0", ""
  135.                 $Padded += ".0" * (4 - $Padded.Split('.').Count)
  136.                 $Padded
  137.             )
  138.             DownloadPath = "https://$($Script:DriversXML.DriverPackManifest.baselocation)/$($_.path)"
  139.             MD5Hash=$_.hashMD5
  140.         }
  141.     }
  142.     $Script:DriverPack = $Script:DriverPacks | Sort-Object -Property PaddedVersion -Descending | Select-Object -First 1
  143.     Write-LogEntry -Value "Successfully retrieved Driver Pack version: $($Script:DriverPack.Version)"
  144. } Catch{
  145.     Write-LogEntry -Value "Failed to retrieve latest Driver Pack version"
  146.     Exit-Script -ExitCode 104
  147. }
  148.  
  149. #Download Driver Pack
  150. Write-LogEntry -Value " "
  151. Write-LogEntry -Value "Downloading Driver Pack"
  152. Try{
  153.     $Script:DriversEXEName = Split-Path -Path $Script:DriverPack.DownloadPath -Leaf
  154.     Invoke-WebRequest -Uri $Script:DriverPack.DownloadPath -OutFile "$($TempFolder)\$($Script:DriversEXEName)" -ErrorAction Stop
  155.     Write-LogEntry -Value "Successfully downloaded $($Script:DriverPack.DownloadPath)"
  156. }
  157. Catch{
  158.     Write-LogEntry -Value "Failed to download $($Script:DriverPack.DownloadPath)"
  159.     Exit-Script -ExitCode 105
  160. }
  161.  
  162. #Verify Driver Pack MD5 Hash
  163. Write-LogEntry -Value " "
  164. Write-LogEntry -Value "Verifying integrity of download"
  165. Try{
  166.     $MD5=(Get-FileHash "$($TempFolder)\$($Script:DriversEXEName)" -Algorithm MD5).Hash
  167.     If ($MD5 -eq $Script:DriverPack.MD5Hash){
  168.         Write-LogEntry -Value "Successfully verified integrity of download"
  169.     } else {
  170.         Write-LogEntry -Value "Failed to verify integrity of download"
  171.         Exit-Script -ExitCode 106
  172.     }
  173. }
  174. Catch{
  175.     Write-LogEntry -Value "Failed to verify integrity of download"
  176.     Exit-Script -ExitCode 106
  177.  
  178. }
  179.  
  180. #Extract Driver Pack
  181. Write-LogEntry -Value " "
  182. Write-LogEntry -Value "Extracting Driver Pack"
  183. Try {
  184.     If (!(Test-Path $($TempFolder))){
  185.         New-Item -Path "$($TempFolder)" -Name "DriverPack" -ItemType Directory -Force
  186.     }
  187.     $Drivers=Start-Process "$($TempFolder)\$($Script:DriversEXEName)" -ArgumentList "/s /e=`"$($TempFolder)\DriverPack`" /l=`"$($TempFolder)\dell.log`"" -Wait -PassThru -ErrorAction Stop
  188.     $Script:DriversExitCode=$Drivers.ExitCode
  189.     Switch ($DriversExitCode){
  190.         0{Write-LogEntry -Value "Successfully extracted $($TempFolder)\$($Script:DriversEXEName) to $($TempFolder)\DriverPack"}
  191.         default {throw $DriversExitCode}
  192.     }
  193. }
  194. Catch {
  195.     Switch ($_){
  196.         default {Write-LogEntry -Value "Failed to extract $($TempFolder)\$($Script:DriversEXEName) with exit code code $($_)"}
  197.     }
  198.     Exit-Script -ExitCode 107
  199. }
  200.  
  201. #Inject Drivers into image
  202. Write-LogEntry -Value " "
  203. Write-LogEntry -Value "Injecting Driver Pack"
  204. Try{
  205.     $DRIVERS=Start-Process "Dism.exe" -WindowStyle Hidden -ArgumentList "/Image:$($OSDTargetSystemDrive)\ /Add-Driver /Driver:$($TempFolder)\DriverPack /Recurse" -Wait -PassThru
  206.     $DRIVERSExitcode=$DRIVERS.Exitcode
  207.     Switch ($DRIVERSExitcode){
  208.        0 {Write-LogEntry -Value "Driver injection completed successfully"}
  209.        default {Write-LogEntry -Value "Driver injection completed with exit code $($DRIVERSExitcode)"}
  210.     }  
  211. }
  212. Catch{
  213.     Write-LogEntry "Failed to inject drivers from $($TempFolder)\DriverPack into image"
  214.     Exit-Script -ExitCode 108
  215. }
  216.  
  217. #Exit  the script with success code
  218. Exit-Script -ExitCode 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement