-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl-file.ps1
63 lines (59 loc) · 2.84 KB
/
dl-file.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
<# function dl-file($url,$filename)
{
$web = New-Object system.net.webclient
Register-ObjectEvent `
-InputObject $web `
-EventName DownloadProgressChanged `
-SourceIdentifier WebClient.DownloadProgressChanged `
-Action { Write-Progress -Activity "Downloading: $($EventArgs.ProgressPercentage)% Completed" -Status $url -PercentComplete $EventArgs.ProgressPercentage} | Out-Null
Register-ObjectEvent `
-InputObject $web `
-EventName DownloadFileCompleted `
-SourceIdentifier WebClient.DownloadFileComplete `
-Action {
Write-Color -Text "Download Complete",$filename -Color Green,Magenta
Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete
} | Out-Null
try
{
$web.DownloadFile($url,$filename)
}
catch [system.net.webexception]
{
Write-Warning "Unable to download File!!"
if($_.exception){ Write-Color "Error Details:`t",$_.exception.message -Color Yellow,Red}
elseif($_.message){Write-Color "Error Details:`t",$_.message -Color Yellow,Red}
else{$_}
}
Finally
{
$web.Dispose()
}
}
#>
function dl-panopta($url,$filename)
{
$Global:dlfinished = $false
$web = New-Object system.net.webclient
Register-ObjectEvent `
-InputObject $web `
-EventName DownloadFileCompleted `
-SourceIdentifier WebClient.DownloadFileCompleted `
-Action {$Global:dlfinished = $true;Unregister-Event -SourceIdentifier 'webclient.downloadfilecompleted'} | Out-Null
try
{
$web.DownloadFileAsync($url,$filename)
}
catch [system.net.webexception]
{
Write-Warning "Unable to download File!!"
if($_.exception){ Write-Color "Error Details:`t",$_.exception.message -Color Yellow,Red}
elseif($_.message){Write-Color "Error Details:`t",$_.message -Color Yellow,Red}
else{$_}
}
Finally
{
$web.Dispose()
}
}