From 3b6eb49c56a05c5251a2a1912541d32b87427b98 Mon Sep 17 00:00:00 2001 From: David Segura Date: Wed, 30 Sep 2026 14:22:46 -0500 Subject: [PATCH] refactor(download): unify download functions and improve usage across scripts --- CHANGELOG.md | 3 + .../Update-RecastOSDCloudUSBCache.ps1 | 4 +- .../Get-DeploymentDiskObject.ps1 | 0 ...e.ps1 => Invoke-RecastOSDDownloadFile.ps1} | 57 +++++++++++++++---- .../step-install-downloadwindowsimage.ps1 | 4 +- .../step-Save-WindowsDriver-DriverPack.ps1 | 4 +- 6 files changed, 54 insertions(+), 18 deletions(-) rename OSDCloud/private/{core => core-disk}/Get-DeploymentDiskObject.ps1 (100%) rename OSDCloud/private/core/{Invoke-OSDCloudDownloadFile.ps1 => Invoke-RecastOSDDownloadFile.ps1} (85%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0346c..22d276a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,14 +15,17 @@ All notable changes to this project will be documented in this file. - Made bundled operating-system catalogs win when an external catalog has the same build revision, while preserving higher-revision external overlays. - Merged the OSD and OSDCloud catalog projections into `Get-OSDCoreOperatingSystems`, retaining each module's native property schema. - Updated deployment initialization, operating-system selection, default OS resolution, catalog refresh, maintenance guidance, and function help to use the unified provider and synchronized catalog model. +- Unified the shared private core utilities with OSD and renamed the centralized download helper to `Invoke-RecastOSDDownloadFile`. ### Fixed - Made HP driver pack catalog release-date parsing culture-independent. +- Corrected the shared download helper's forced WebClient path so it no longer overwrites the typed switch parameter. ### Removed - Removed the redundant private `Get-OSDCloudCoreOperatingSystems` provider after migrating its callers to the unified function. +- Removed the legacy `Invoke-OSDCloudDownloadFile` command name. ## 26.9.29.1 - September 29, 2026 diff --git a/OSDCloud/private/core-cache/Update-RecastOSDCloudUSBCache.ps1 b/OSDCloud/private/core-cache/Update-RecastOSDCloudUSBCache.ps1 index 7769e2b..0100c34 100644 --- a/OSDCloud/private/core-cache/Update-RecastOSDCloudUSBCache.ps1 +++ b/OSDCloud/private/core-cache/Update-RecastOSDCloudUSBCache.ps1 @@ -325,7 +325,7 @@ function Update-RecastOSDCloudUSBCache { (New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'Skip the OperatingSystem download.') ) if ($downloadOperatingSystem -and ($host.UI.PromptForChoice($caption, $message, $choices, 1) -eq 0)) { - $savedOperatingSystem = Invoke-OSDCoreDownloadFile -SourceUrl $selectedOperatingSystemUrl -DestinationDirectory $osdCoreOperatingSystemDestination -DestinationName $global:OSDCoreOperatingSystemCloudObject.FileName -ErrorAction Stop + $savedOperatingSystem = Invoke-RecastOSDDownloadFile -SourceUrl $selectedOperatingSystemUrl -DestinationDirectory $osdCoreOperatingSystemDestination -DestinationName $global:OSDCoreOperatingSystemCloudObject.FileName -ErrorAction Stop # Verify the downloaded payload before refreshing the cache inventory. if (-not [string]::IsNullOrWhiteSpace($expectedOperatingSystemHash)) { @@ -434,7 +434,7 @@ function Update-RecastOSDCloudUSBCache { (New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'Skip the driver pack download.') ) if ($downloadDriverPack -and ($host.UI.PromptForChoice($caption, $message, $choices, 1) -eq 0)) { - $savedDriverPack = Invoke-OSDCoreDownloadFile -SourceUrl $global:OSDCoreDriverPackCloudObject.Url -DestinationDirectory $osdCoreDriverPackDestination -DestinationName $global:OSDCoreDriverPackCloudObject.FileName -ErrorAction Stop + $savedDriverPack = Invoke-RecastOSDDownloadFile -SourceUrl $global:OSDCoreDriverPackCloudObject.Url -DestinationDirectory $osdCoreDriverPackDestination -DestinationName $global:OSDCoreDriverPackCloudObject.FileName -ErrorAction Stop # Verify the downloaded driver pack before refreshing the cache inventory. if (-not [string]::IsNullOrWhiteSpace($expectedDriverPackHashMD5)) { diff --git a/OSDCloud/private/core/Get-DeploymentDiskObject.ps1 b/OSDCloud/private/core-disk/Get-DeploymentDiskObject.ps1 similarity index 100% rename from OSDCloud/private/core/Get-DeploymentDiskObject.ps1 rename to OSDCloud/private/core-disk/Get-DeploymentDiskObject.ps1 diff --git a/OSDCloud/private/core/Invoke-OSDCloudDownloadFile.ps1 b/OSDCloud/private/core/Invoke-RecastOSDDownloadFile.ps1 similarity index 85% rename from OSDCloud/private/core/Invoke-OSDCloudDownloadFile.ps1 rename to OSDCloud/private/core/Invoke-RecastOSDDownloadFile.ps1 index 1072632..0f5ce9d 100644 --- a/OSDCloud/private/core/Invoke-OSDCloudDownloadFile.ps1 +++ b/OSDCloud/private/core/Invoke-RecastOSDDownloadFile.ps1 @@ -1,12 +1,40 @@ -<# -.SYNOPSIS -Downloads a file from the internet and returns a Get-Item Object -.DESCRIPTION -Downloads a file from the internet and returns a Get-Item Object -.LINK -https://github.com/OSDeploy/OSD/tree/master/Docs -#> -function Invoke-OSDCloudDownloadFile { +function Invoke-RecastOSDDownloadFile { + <# + .SYNOPSIS + Downloads a file to a local path and returns file information. + + .DESCRIPTION + Downloads content from a source URL into a destination directory using + either curl or WebClient fallback logic, then returns a FileInfo object for + the downloaded file. + + .PARAMETER SourceUrl + Source URL to download. + + .PARAMETER DestinationName + Optional destination file name. If omitted, the file name is derived from + the source URL. + + .PARAMETER DestinationDirectory + Destination directory for the downloaded file. + + .PARAMETER Overwrite + Overwrites the destination file if it already exists. + + .PARAMETER WebClient + Forces use of WebClient instead of curl. + + .EXAMPLE + Invoke-RecastOSDDownloadFile -SourceUrl 'https://example.org/file.cab' -DestinationDirectory "$env:TEMP\OSD" + Downloads the file and returns a FileInfo object. + + .LINK + https://github.com/OSDeploy/OSD/tree/master/docs + + .NOTES + Author: David Segura - Recast Software + 2026-09-30 - Unified the OSD and OSDCloud download helpers + #> [CmdletBinding()] [OutputType([System.IO.FileInfo])] param @@ -33,6 +61,7 @@ function Invoke-OSDCloudDownloadFile { [System.Management.Automation.SwitchParameter] $WebClient ) + $Error.Clear() #================================================= # Values #================================================= @@ -108,9 +137,13 @@ function Invoke-OSDCloudDownloadFile { if ($UseWebClient -eq $true) { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls1 - $WebClient = New-Object System.Net.WebClient - $WebClient.DownloadFile($SourceUrl, $DestinationFullName) - $WebClient.Dispose() + $webClientInstance = New-Object System.Net.WebClient + try { + $webClientInstance.DownloadFile($SourceUrl, $DestinationFullName) + } + finally { + $webClientInstance.Dispose() + } } else { Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] cURL Source: $SourceUrl" diff --git a/OSDCloud/private/steps/4-install/step-install-downloadwindowsimage.ps1 b/OSDCloud/private/steps/4-install/step-install-downloadwindowsimage.ps1 index fb3c084..de6434f 100644 --- a/OSDCloud/private/steps/4-install/step-install-downloadwindowsimage.ps1 +++ b/OSDCloud/private/steps/4-install/step-install-downloadwindowsimage.ps1 @@ -122,7 +122,7 @@ function step-install-downloadwindowsimage { $null = New-Item -Path $USBDownloadPath -ItemType Directory -Force } Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Downloading Windows image to USB cache." - $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $OperatingSystemFilePath -DestinationDirectory "$USBDownloadPath" -DestinationName $FileName + $SaveWebFile = Invoke-RecastOSDDownloadFile -SourceUrl $OperatingSystemFilePath -DestinationDirectory "$USBDownloadPath" -DestinationName $FileName if ($SaveWebFile) { Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Copying cached Windows image from $($SaveWebFile.FullName) to $DownloadPath." @@ -135,7 +135,7 @@ function step-install-downloadwindowsimage { # $SaveWebFile is a FileInfo Object, not a path Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] DownloadPath: $DownloadPath" Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Downloading Windows image directly to $DownloadPath." - $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $OperatingSystemFilePath -DestinationDirectory $DownloadPath -ErrorAction Stop + $SaveWebFile = Invoke-RecastOSDDownloadFile -SourceUrl $OperatingSystemFilePath -DestinationDirectory $DownloadPath -ErrorAction Stop $FileInfo = $SaveWebFile } #================================================= diff --git a/OSDCloud/private/steps/5-drivers/step-Save-WindowsDriver-DriverPack.ps1 b/OSDCloud/private/steps/5-drivers/step-Save-WindowsDriver-DriverPack.ps1 index 528bb42..2367619 100644 --- a/OSDCloud/private/steps/5-drivers/step-Save-WindowsDriver-DriverPack.ps1 +++ b/OSDCloud/private/steps/5-drivers/step-Save-WindowsDriver-DriverPack.ps1 @@ -121,7 +121,7 @@ function step-Save-WindowsDriver-DriverPack { $null = New-Item -Path $USBDownloadPath -ItemType Directory -Force } Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Downloading DriverPack to USB cache." - $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $DriverPackCloudObject.Url -DestinationDirectory "$USBDownloadPath" -DestinationName $FileName + $SaveWebFile = Invoke-RecastOSDDownloadFile -SourceUrl $DriverPackCloudObject.Url -DestinationDirectory "$USBDownloadPath" -DestinationName $FileName if ($SaveWebFile) { Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Copying cached DriverPack from $($SaveWebFile.FullName) to $DownloadPath." @@ -134,7 +134,7 @@ function step-Save-WindowsDriver-DriverPack { # $SaveWebFile is a FileInfo Object, not a path Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] DownloadPath: $DownloadPath" Write-Verbose -Message "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Downloading DriverPack directly to $DownloadPath." - $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $DriverPackCloudObject.Url -DestinationDirectory $DownloadPath -ErrorAction Stop + $SaveWebFile = Invoke-RecastOSDDownloadFile -SourceUrl $DriverPackCloudObject.Url -DestinationDirectory $DownloadPath -ErrorAction Stop $FileInfo = $SaveWebFile } #=================================================