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