Public/WebFile.ps1
<#
.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://osd.osdeploy.com/module/functions/save-webfile .NOTES 21.3.16.2 Updated to Return Get-Item #> function Save-WebFile { [CmdletBinding()] param ( #URL of the file to download [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline)] [string]$SourceUrl, #Destination File Name [string]$DestinationName, #Destination Folder [Alias('Path')] [string]$DestinationDirectory = (Join-Path $env:TEMP 'OSD'), #Overwrite the file if it exists already #The default action is to skip the download [switch]$Overwrite, [switch]$WebClient ) #================================================= # Values #================================================= Write-Verbose "SourceUrl: $SourceUrl" Write-Verbose "DestinationName: $DestinationName" Write-Verbose "DestinationDirectory: $DestinationDirectory" Write-Verbose "Overwrite: $Overwrite" Write-Verbose "WebClient: $WebClient" #================================================= # Set DestinationDirectory #================================================= if (Test-Path "$DestinationDirectory") { Write-Verbose "Directory already exists at $DestinationDirectory" } else { New-Item -Path "$DestinationDirectory" -ItemType Directory -Force -ErrorAction Stop | Out-Null } #================================================= # Test File #================================================= $DestinationNewItem = New-Item -Path (Join-Path $DestinationDirectory "$(Get-Random).txt") -ItemType File if (Test-Path $DestinationNewItem.FullName) { $DestinationDirectory = $DestinationNewItem | Select-Object -ExpandProperty Directory Write-Verbose "Destination Directory is writable at $DestinationDirectory" Remove-Item -Path $DestinationNewItem.FullName -Force | Out-Null } else { Write-Warning "Unable to write to Destination Directory" Break } #================================================= # DestinationName #================================================= if ($PSBoundParameters['DestinationName']) { } else { $DestinationName = Split-Path -Path $SourceUrl -Leaf } Write-Verbose "DestinationName: $DestinationName" #================================================= # WebFileFullName #================================================= $DestinationDirectoryItem = (Get-Item $DestinationDirectory).FullName $DestinationFullName = Join-Path $DestinationDirectoryItem $DestinationName #================================================= # OverWrite #================================================= if ((-NOT ($PSBoundParameters['Overwrite'])) -and (Test-Path $DestinationFullName)) { Write-Verbose "DestinationFullName already exists" Get-Item $DestinationFullName } else { #================================================= # Download #================================================= $SourceUrl = [Uri]::EscapeUriString($SourceUrl) Write-Verbose "Testing file at $SourceUrl" #================================================= # Test for WebClient Proxy #================================================= $UseWebClient = $false if ($WebClient -eq $true) { $UseWebClient = $true } elseif (([System.Net.WebRequest]::DefaultWebProxy).Address) { $UseWebClient = $true } elseif (!(Test-CommandCurlExe)) { $UseWebClient = $true } 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() } else { Write-Verbose "cURL Source: $SourceUrl" Write-Verbose "Destination: $DestinationFullName" if ($host.name -match 'ConsoleHost') { Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`"" } else { #PowerShell ISE will display a NativeCommandError, so progress will not be displayed $Quiet = Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`" 2>&1" } } #================================================= # Return #================================================= if (Test-Path $DestinationFullName) { Get-Item $DestinationFullName } else { Write-Warning "Could not download $DestinationFullName" $null } #================================================= } } |