few fixes and new func IsURL
This commit is contained in:
@@ -169,15 +169,15 @@ Function Get-Version {
|
||||
# Determines whether a URL is valid
|
||||
Function IsURLValid {
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL)
|
||||
Try {
|
||||
$HTTPRequest = [System.Net.WebRequest]::Create($URL)
|
||||
$HTTPResponse = $HTTPRequest.GetResponse()
|
||||
$HTTPStatus = [int]$HTTPResponse.StatusCode
|
||||
$HTTPResponse.Close()
|
||||
}
|
||||
Catch { }
|
||||
If ($HTTPStatus -ne 404) { Return $true }
|
||||
Else { Return $false }
|
||||
Try { $status = [System.Net.WebRequest]::Create($URL).GetResponse().StatusCode }
|
||||
Catch { Write-Error $_.Exception.Message ; Return $false }
|
||||
If ( $status -eq 404 ) { Return $false }
|
||||
Return $true
|
||||
}
|
||||
|
||||
Function IsURL {
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Value)
|
||||
Return $value -match "https?:\/\/"
|
||||
}
|
||||
|
||||
# Downloads a file from a URL
|
||||
@@ -186,21 +186,19 @@ Function Download-File {
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL,
|
||||
[Parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$File
|
||||
)
|
||||
If ( !($File) ) { $File = '{0}{1}' -f (Get-TempPath), (Split-Path $URL -Leaf) }
|
||||
$URL = Get-AbsoluteURI -URL $URL
|
||||
If ( IsURLValid $URL ) {
|
||||
If ( Test-Path $File ) {
|
||||
Write-Output "Deleting existing file: ""${File}"""
|
||||
Try { Remove-Item $File -Force }
|
||||
Catch { Write-Error $_.Exception.Message }
|
||||
}
|
||||
Write-Output "Downloading ""${URL}"" to ""${File}"""
|
||||
Try { (New-Object System.Net.WebClient).DownloadFile($URL,$File) }
|
||||
Catch { Write-Error $_.Exception.Message }
|
||||
If ( Test-Path "${File}" ) { Return "${File}" }
|
||||
Else { Write-Output "Downloaded file does not exist at ""${File}""" ; Return $false }
|
||||
$URI = Get-AbsoluteURI -URL $URL
|
||||
If ( $null -eq $URI ) { Return }
|
||||
If ( [string]::IsNullOrEmpty($File) ) { $File = '{0}{1}' -f (Get-TempPath), (Split-Path $URL -Leaf) }
|
||||
If ( Test-Path $File ) {
|
||||
Write-Output "Deleting existing file: ""${File}"""
|
||||
Try { Remove-Item $File -Force }
|
||||
Catch { Write-Error $_.Exception.Message ; Return }
|
||||
}
|
||||
Else { Write-Output "URL is not valid or file cannot be reached: ${URL}" }
|
||||
Write-Output "Downloading ""${URI}"" to ""${File}"""
|
||||
Try { (New-Object System.Net.WebClient).DownloadFile($URI,$File) }
|
||||
Catch { Write-Error $_.Exception.Message ; Return }
|
||||
If ( Test-Path "${File}" ) { Return "${File}" }
|
||||
Else { Write-Output "Downloaded file does not exist at ""${File}""" ; Return }
|
||||
}
|
||||
|
||||
# Install a program from a provided path
|
||||
@@ -822,6 +820,7 @@ Function ZeroPad-Decimal {
|
||||
# Function to follow a URL and return the absolute URI of the result (whether redirected or not)
|
||||
Function Get-AbsoluteURI {
|
||||
param([string]$URL)
|
||||
If ( !(IsURLValid $URL) ) { Return }
|
||||
Return [System.Net.HttpWebRequest]::Create($URL).GetResponse().ResponseUri.AbsoluteUri
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user