Author Posts

October 14, 2015 at 7:43 am

Hi,

I've written the below code to mount a WIM and inject some driver files.

< #
.Synopsis
   Add drivers to image
.DESCRIPTION
   Add required drivers to a newly mounted image and dismount once completed
.EXAMPLE
   update-wim -Drivers .inf -Wim .wim
.EXAMPLE
    Update-Wim -Drivers Netwbw02.INF,Netwew00.INF -Wim C:\sccm\dell\boot.wim -TargetFolder C:\temp\Mount
.INPUTS
   -Drivers: add INF file. for addtional add a ',' between files. i.e. -Drivers Netwbw02.INF,Netwew00.INI
   -Wim: Add .Wim .i.e. boot.wim
   -TargetFolder: If left blank will default to C:\TempMount" else i.e. -TargetFolder C:\Temp\Image\Wim
.OUTPUTS
   Output from this cmdlet (if any)
.NOTES
   By Graham Beer
   Created on 14/10/2015
#>


function Update-Wim {

      [CmdletBinding(SupportsShouldProcess=$true,
                         ConfirmImpact='medium')]
    Param (
                 
        [Parameter(Mandatory=$true)]                         
        [ValidateScript({
            if ($_ -match "[IiNnFf]{3}$"){$true}
            else {Write-warning "$_ Is not a valid Driver file: Must be a .INF file" }})]
        [System.object[]]$Drivers,
                        
        [ValidateScript({
            if ($_ -match "[WwIiMm]{3}$"){$true}
            else {Write-warning "$_ Is not a Wim file: Must be a .wim file" }})]
        [system.String]$Wim,
        
        [system.String]$TargetFolder = 'C:\TempMount'
                                
         )


##
    Begin #display dirvers to add to wim
  {
Try  {
     if ([boolean](Get-Module -Name DISM)) 
     {
    Import-Module -Name DISM -PassThru -OutVariable Import |Out-Null
     Write-Host -ForegroundColor Yellow "$($Import.name) module has been succesfully imported"
     }
    else
     {
    write-host -ForegroundColor Red "DISM module is not available!!!"
    break
     }
  }
catch 
     {    
     write-warning $_.Exception.Message
     }

Try {    
    if ($TargetFolder -eq 'C:\TempMount')
    {
    write-host -ForegroundColor Cyan "Default $($TargetFolder) being used`n"
    }
    else
    {
    write-host -ForegroundColor Cyan "Custom Folder of $($TargetFolder) has been selected`n"
    } 
    if ($Drivers.Count -gt 1)
    { 
    Write-host -ForegroundColor Cyan "Going to install $Drivers files"
    }
    else 
    {
    Write-host -ForegroundColor Cyan "Going to install $Drivers file"
    }
  }
catch 
     {    
     write-warning $_.Exception.Message
     } 
   }  

##    
    Process #Start process
  {   



    #Create directory to mount WIM
    write-warning "STAGE 1: Set Mount Folder"
Try  {
        if (Test-Path $TargetFolder) 
            {
    write-warning "Directory Exists" 
            }
        else
            {
        New-Item -ItemType 'Directory' -Path $TargetFolder -Force | Out-Null
    Write-host -ForegroundColor Cyan "$TargetFolder in place`n"           
     }
     }
catch 
     {    
     write-warning $_.Exception.Message
     }  



     #Mount the Wim to directory
     write-warning "STAGE 2: Mounting selected $($wim)......."
Try  {
        Mount-WindowsImage –ImagePath $wim –Index 1 -Path $TargetFolder | Out-Null
     Write-host -ForegroundColor Cyan "$($wim) now mounted to $TargetFolder`n"
     }  
catch 
     {    
     write-warning $_.Exception.Message
     }



     #Add requested drivers to image
     Write-Warning "STAGE 3: Add Drivers to $($Wim)`n"
Try  {
#Trap { "Error: $_"; Continue; }
       Foreach ($driver in $Drivers) {
       $currentDriver = $Driver
       Add-WindowsDriver –Path $TargetFolder –Driver $Driver -ForceUnsigned -ErrorAction Continue
     Write-host -ForegroundColor Cyan " : $($Driver) has been injected to $($Wim)" }
    }
catch 
    { 
    Write-host -ForegroundColor Red ('Failed to access {0} : {1} ' -f $currentDriver, ` 
     $_.Exception.Message )  
     $failed = ($_).count
    }


Try  {
     #display newly added drivers to image
     if ($Failed -le 1)
         {
     write-host -ForegroundColor Red "STAGE 4: Skipping as no drivers have been injected`n"  
         } 
         else
         {
     Write-Warning "STAGE 4: Showing newly installed driver(s)..."
      Get-WindowsDriver –Path $TargetFolder | where { $_.OriginalFileName -match $Driver } | select @{Name="FileName";exp={split-path $($_.OriginalFileName) -leaf}}, Driver, ClassDescription | ft -AutoSize }  
          
     }
catch 
     {    
     write-warning $_.Exception.Message
     }



     #Dismount the image ready for use
     Write-Warning "STAGE 5: Dismounting $($Wim)...."
Try  {
        Dismount-WindowsImage –Path $TargetFolder -Save | Out-Null
     Write-host -ForegroundColor Cyan "Completed Dismount of $($Wim)`n"
     }
catch 
     {    
     write-warning $_.Exception.Message
     }
   
##  
  }  
    End #Closing summary
  {

Try  {
    #delete $TargetFolder folder
    Remove-Item $TargetFolder -Force | out-null
    Write-Warning "Folder $TargetFolder has now been removed`n"
     }
catch 
     {    
     write-warning $_.Exception.Message
     }

    #completion message      
    if ([boolean]$Failed) 
    {
    $FailedCount = [int]$drivers.count - [int]$failed
    write-host -ForegroundColor Yellow "SUMMARY :"
    write-host -ForegroundColor Red "$($FailedCount) failed to install out of $($Drivers.count). Check DISM log for details"
    }
    else
    {
    write-host -ForegroundColor Yellow "SUMMARY :"
    Write-Host -ForegroundColor Green "$Drivers now added to $wim, image ready for use"  
    }
  }
}

This part of the code,

     #Add requested drivers to image
     Write-Warning "STAGE 3: Add Drivers to $($Wim)`n"
Try  {
#Trap { "Error: $_"; Continue; }
       Foreach ($driver in $Drivers) {
       $currentDriver = $Driver
       Add-WindowsDriver –Path $TargetFolder –Driver $Driver -ForceUnsigned -ErrorAction Continue
     Write-host -ForegroundColor Cyan " : $($Driver) has been injected to $($Wim)" }
    }
catch 
    { 
    Write-host -ForegroundColor Red ('Failed to access {0} : {1} ' -f $currentDriver, ` 
     $_.Exception.Message )  
     $failed = ($_).count
    }

Does not continue on a failed driver. I can work out why. Can anyone help ?

October 14, 2015 at 7:53 am

Try moving the try/catch block into the foreach block. In otherwords only have the try block encompass the command(s) that will actually throw the error.

Chris

October 14, 2015 at 11:47 am

Chris,

Many thanks, that works. I changed as shown below.

  #Add requested drivers to image
     Write-Warning "STAGE 3: Add Drivers to $($Wim)`n"
     Foreach ($driver in $Drivers) {
Try  {
     $currentDriver = $Driver
     Add-WindowsDriver –Path $TargetFolder –Driver $Driver -ForceUnsigned -ErrorAction Continue | Out-Null
     Write-host -ForegroundColor Cyan ": $($Driver) has been injected to $($Wim)`n" 
     }
catch 
    { 
    Write-host -ForegroundColor Red ('Failed to access {0} : {1} ' -f $currentDriver, ` 
    $_.Exception.Message )  
    $failed = ($_).count
    }
  }