Public/RETIREPENDING/Save-OSDDownload.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
.SYNOPSIS
Downloads a file from the internet
 
.DESCRIPTION
Downloads a file from the internet. Success returns $true
 
.LINK
https://osd.osdeploy.com/module/functions/save-osddownload
 
.NOTES
19.10.25 David Segura @SeguraOSD
#>

function Save-OSDDownload {
    [CmdletBinding()]
    param (
        #URL of the file to download
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline)]
        [Alias('DriverUrl')]
        [string[]]$SourceUrl,

        #Destination File
        [string]$DestinationName,

        #Destination Folder
        [string]$DownloadFolder = "$env:TEMP\OSD",

        #Overwrite the file if it exists already
        #The default action is to skip the download
        [switch]$Overwrite,

        #Download the file using BITS-Transfer
        #Interactive Login required
        [switch]$BitsTransfer
    )

    begin {

    }
    process {
        foreach ($Input in $SourceUrl) {
            #=======================================================================
            # Create Object
            #=======================================================================
            $Global:OSDDownload = [ordered]@{
                Name = $null
                Parent = $DownloadFolder
                FullName = $null
                SourceUrl = $SourceUrl[0]
                Method = 'None'
                Download = $false
                IsDownloaded = $false
            }
            #=======================================================================
            # Set DestinationName
            #=======================================================================
            if ($PSBoundParameters['DestinationName']) {
                $Global:OSDDownload.Name = $DestinationName
            } else {
                $Global:OSDDownload.Name = Split-Path -Path $Global:OSDDownload.SourceUrl -Leaf
            }
            #=======================================================================
            # DownloadFolder
            #=======================================================================
            if (Test-Path "$DownloadFolder") {

            }
            else {
                New-Item -Path "$DownloadFolder" -ItemType Directory -Force -ErrorAction Stop
            }
            $Global:OSDDownload.DownloadFolder = (Get-Item $DownloadFolder).FullName
            $Global:OSDDownload.FullName = Join-Path $Global:OSDDownload.DownloadFolder $Global:OSDDownload.Name
            #=======================================================================
            # OverWrite
            #=======================================================================
            if ($PSBoundParameters['Overwrite']) {
            }
            else {
                if (Test-Path $Global:OSDDownload.FullName) {
                    Write-Verbose "Download already exists $($Global:OSDDownload.FullName)"
                    $Global:OSDDownload.IsDownloaded = $true
                    Return $Global:OSDDownload
                }
            }
            #=======================================================================
            # Download
            #=======================================================================
            if (Get-Command 'curl.exe') {
                Write-Verbose "cURL: $($Global:OSDDownload.SourceUrl)"
                $Global:OSDDownload.Method = 'cURL'

                if ($host.name -match 'ConsoleHost') {
                    Invoke-Expression "& curl.exe --location --output `"$($Global:OSDDownload.FullName)`" --url $($Global:OSDDownload.SourceUrl)"
                }
                else {
                    #PowerShell ISE will display a NativeCommandError, so progress will not be displayed
                    $Quiet = Invoke-Expression "& curl.exe --location --output `"$($Global:OSDDownload.FullName)`" --url $($Global:OSDDownload.SourceUrl) 2>&1"
                }
            }
            elseif ($PSBoundParameters['BitsTransfer']) {
                $Global:OSDDownload.Method = 'cURL'
                Start-BitsTransfer -Source $Global:OSDDownload.SourceUrl -Destination $Global:OSDDownload.FullName
            }
            else {
                $Global:OSDDownload.Method = 'WebClient'
                [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls1
                $WebClient = New-Object System.Net.WebClient
                $WebClient.DownloadFile($Global:OSDDownload.SourceUrl, $Global:OSDDownload.FullName)
            }
            #=======================================================================
            # Return
            #=======================================================================
            if (Test-Path $Global:OSDDownload.FullName) {
                $Global:OSDDownload.IsDownloaded = $true
                Return $Global:OSDDownload
            }
            else {
                Write-Warning "Could not download $($Global:OSDDownload.FullName)"
                Return $null
            }
            #=======================================================================
        }
    }
    end {}
}