Berikut adalah fungsi PowerShell yang menyelesaikan URL pendek sebelum mengunduh file
function Get-FileFromUri {
param(
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
[Alias('Uri')]
$Url,
[parameter(Mandatory=$false, Position=1)]
[string]
[Alias('Folder')]
$FolderPath
)
process {
try {
# resolve short URLs
$req = [System.Net.HttpWebRequest]::Create($Url)
$req.Method = "HEAD"
$response = $req.GetResponse()
$fUri = $response.ResponseUri
$filename = [System.IO.Path]::GetFileName($fUri.LocalPath);
$response.Close()
# download file
$destination = (Get-Item -Path ".\" -Verbose).FullName
if ($FolderPath) { $destination = $FolderPath }
if ($destination.EndsWith('\')) {
$destination += $filename
} else {
$destination += '\' + $filename
}
$webclient = New-Object System.Net.webclient
$webclient.downloadfile($fUri.AbsoluteUri, $destination)
write-host -ForegroundColor DarkGreen "downloaded '$($fUri.AbsoluteUri)' to '$($destination)'"
} catch {
write-host -ForegroundColor DarkRed $_.Exception.Message
}
}
}
Gunakan seperti ini untuk mengunduh file ke folder saat ini:
Get-FileFromUri http://example.com/url/of/example/file
Atau untuk mengunduh file ke folder yang ditentukan:
Get-FileFromUri http://example.com/url/of/example/file C:\example-folder