private/MDT/pwsh/Step-WinPEAppCurl.ps1
|
function Step-WinPEAppCurl { <# .SYNOPSIS Downloads and adds curl.exe to a mounted WinPE image. .DESCRIPTION Downloads the latest curl for Windows (win64-mingw) from curl.se and caches it under WinPEAppsPath\curl-for-windows. The curl.exe binary is then copied to MountPath\Windows\System32 so it is available during WinPE boot. If curl.exe already exists in System32, this function exits immediately without re-downloading. .PARAMETER AppName Display name recorded in $global:BuildMedia.InstalledApps. Default: 'curl'. .PARAMETER Architecture Target architecture for the downloaded binary. Default: $global:BuildMedia.Architecture. .PARAMETER MountPath Path to the directory where the WIM image is mounted. Defaults to $global:BuildMedia.MountPath. .PARAMETER WinPEAppsPath Root directory used to cache downloaded WinPE app binaries. Defaults to $global:BuildMedia.WinPEAppsPath. .EXAMPLE Step-WinPEAppCurl Downloads (if needed) and installs curl.exe using values from the global BuildMedia hashtable. .INPUTS None. This function does not accept pipeline input. .OUTPUTS None. .NOTES Author: David Segura Company: Recast Software Called by Invoke-OSDeployMDT during the WIM stage. Reference: https://curl.se/windows/ #> [CmdletBinding()] param ( [System.String] $AppName = 'curl', [System.String] $Architecture = $global:BuildMedia.Architecture, [System.String] $MountPath = $global:BuildMedia.MountPath, [System.String] $WinPEAppsPath = $global:BuildMedia.WinPEAppsPath ) #================================================= $Error.Clear() Write-Verbose "[$(Get-Date -format s)] Start" #================================================= Write-Verbose "[$(Get-Date -format s)] Architecture: $Architecture" Write-Verbose "[$(Get-Date -format s)] MountPath: $MountPath" Write-Verbose "[$(Get-Date -format s)] WinPEAppsPath: $WinPEAppsPath" #================================================= if (Test-Path -LiteralPath "$MountPath\Windows\System32\curl.exe") { Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] $AppName already installed at $MountPath\Windows\System32\curl.exe" return } #================================================= # curl for Windows # https://curl.se/windows/ $CacheCurl = Join-Path $WinPEAppsPath 'curl-for-windows' if (-not (Test-Path -Path $CacheCurl)) { Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Adding cache content $CacheCurl" New-Item -Path $CacheCurl -ItemType Directory -Force | Out-Null } else { Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Source: $CacheCurl" } # amd64 if (-not (Test-Path "$CacheCurl\amd64")) { $Uri = 'https://curl.se/windows/latest.cgi?p=win64-mingw.zip' $DownloadUri = (Invoke-WebRequest -Uri $Uri -Method Head -ErrorAction SilentlyContinue).BaseResponse.RequestMessage.RequestUri.AbsoluteUri if ($DownloadUri) { $FileName = if ($DownloadUri -match 'filename%3D([^&]+)') { [System.Uri]::UnescapeDataString($Matches[1]) } else { Split-Path $DownloadUri -Leaf } if (-not (Test-Path "$CacheCurl\$FileName")) { curl.exe --location --output "$CacheCurl\$FileName" $DownloadUri $DownloadResult = Get-Item -LiteralPath "$CacheCurl\$FileName" -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Expand-Archive -Path $($DownloadResult.FullName) -DestinationPath "$CacheCurl\amd64" -Force } } } Get-ChildItem -Path "$CacheCurl\$Architecture" -Recurse -Include 'curl.exe' -ErrorAction SilentlyContinue | Select-Object -First 1 | ForEach-Object { Copy-Item $_.FullName -Destination "$MountPath\Windows\System32" -Force # Record the installed app $global:BuildMedia.InstalledApps += $AppName } #================================================= Write-Verbose "[$(Get-Date -format s)] End" #================================================= } |