Advertisement
Guest User

Add Driver to Offline Image Functions

a guest
Oct 29th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.03 KB | Source Code | 0 0
  1. function Test-IsElevated {
  2.  
  3.     [CmdletBinding()]
  4.     [OutputType([bool])]
  5.     param ()
  6.  
  7.     $currentIdentity = [Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()
  8.     $currentIdentity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
  9. }
  10.  
  11.  
  12. function Get-OfflineImageDriverFolder {
  13.  
  14.     [CmdletBinding()]
  15.     [OutputType([IO.DirectoryInfo])]
  16.     param (
  17.         [Parameter(Mandatory)]
  18.         [ValidateScript({ $_ -notin [IO.Path]::GetInvalidFileNameChars() })]
  19.         [string] $FolderName,
  20.  
  21.         [string] $Computer
  22.     )
  23.  
  24.     $cimParams = @{
  25.         Class       = 'Win32_Volume'
  26.         Computer    = $Computer
  27.         Filter      = 'DriveType = "2"'
  28.         ErrorAction = 'Stop'
  29.     }
  30.  
  31.     $usbDrives = Get-CimInstance @cimParams
  32.  
  33.     foreach ($usb in $usbDrives) {
  34.         [IO.DirectoryInfo] $folder = [IO.Path]::Combine($usb.Name, $FolderName)
  35.  
  36.         if ($folder.Exists) {
  37.             Write-Debug ('Found driver folder: {0}' -f $folder.FullName)
  38.             $folder
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. function Add-OfflineImageDriver {
  45.  
  46.     [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
  47.     [OutputType([void])]
  48.     param (
  49.         [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
  50.         [Alias('FullName')]
  51.         [ValidateScript({ [IO.Directory]::Exists($_) })]
  52.         [string[]] $Path
  53.     )
  54.  
  55.     begin {
  56.         if (!(Test-IsElevated)) {
  57.             $errorMsg = 'The requested operation requires elevation.'
  58.             $errorRecord = [Management.Automation.ErrorRecord]::new($errorMsg, $null, [Management.Automation.ErrorCategory]::SecurityError, $null)
  59.             $PSCmdlet.ThrowTerminatingError($errorRecord)
  60.         }
  61.  
  62.         try {
  63.             $tsEnv = New-Object -ComObject Microsoft.SMS.TSEnvironment
  64.         } catch  {
  65.             $PSCmdlet.ThrowTerminatingError($_)
  66.         }
  67.  
  68.         $dismPath = [IO.Path]::Combine([Environment]::GetFolderPath([Environment+SpecialFolder]::System), 'dism.exe')
  69.     }
  70.  
  71.     process {
  72.         foreach ($inputPath in $Path) {
  73.             $dismArgs = @(
  74.                 '/Image:"{0}\"' -f $tsEnv.Value('OSDTargetSystemDrive')
  75.                 '/LogPath:"{0}"' -f $tsEnv.Value('_SMSTSLogPath')
  76.                 '/Add-Driver'
  77.                 '/Driver:"{0}"' -f $inputPath
  78.                 '/Recurse'
  79.             ) -join ' '
  80.  
  81.             if ($PSCmdlet.ShouldProcess($dismPath, $dismArgs)) {
  82.                 $output = & $dismPath $dismArgs
  83.  
  84.                 if ($LASTEXITCODE -ne 0) {
  85.                     $errorMsg = $output -join "`n"
  86.                     $errorRecord = [Management.Automation.ErrorRecord]::new($errorMsg, $null, [Management.Automation.ErrorCategory]::InvalidResult, $null)
  87.                     $PSCmdlet.WriteError($errorRecord)
  88.                     continue
  89.                 }
  90.  
  91.                 Write-Debug ('Added drivers from {0} to {1}.' -f $inputPath, $tsEnv.Value('OSDTargetSystemDrive'))
  92.                 $tsEnv.Value('OSDCustomDriversApplied') = 'True'
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement