powershell 使用System.Net命名空间中的WebClient类的DownloadFileAsync方法下载文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用System.Net命名空间中的WebClient类的DownloadFileAsync方法下载文件相关的知识,希望对你有一定的参考价值。

function Get-FileWCAsynchronous{
    param(
        [Parameter(Mandatory=$true)]
        $url, 
        $destinationFolder="$env:USERPROFILE\Downloads", 
        [switch]$includeStats
    )
    $wc = New-Object Net.WebClient
    $wc.UseDefaultCredentials = $true
    $file = $url | Split-Path -Leaf
    $destination = Join-Path $destinationFolder $file
    $start = Get-Date 
    $null = Register-ObjectEvent -InputObject $wc -EventName DownloadProgressChanged `
        -MessageData @{start=$start;includeStats=$includeStats} `
        -SourceIdentifier WebClient.DownloadProgressChanged -Action { 
            filter Get-FileSize {
	            "{0:N2} {1}" -f $(
	            if ($_ -lt 1kb) { $_, 'Bytes' }
	            elseif ($_ -lt 1mb) { ($_/1kb), 'KB' }
	            elseif ($_ -lt 1gb) { ($_/1mb), 'MB' }
	            elseif ($_ -lt 1tb) { ($_/1gb), 'GB' }
	            elseif ($_ -lt 1pb) { ($_/1tb), 'TB' }
	            else { ($_/1pb), 'PB' }
	            )
            }
            $elapsed = ((Get-Date) - $event.MessageData.start)
            #calculate average speed in Mbps
            $averageSpeed = ($EventArgs.BytesReceived * 8 / 1MB) / $elapsed.TotalSeconds
            $elapsed = $elapsed.ToString('hh\:mm\:ss')
            #calculate remaining time considering average speed
            $remainingSeconds = ($EventArgs.TotalBytesToReceive - $EventArgs.BytesReceived) * 8 / 1MB / $averageSpeed
            $receivedSize = $EventArgs.BytesReceived | Get-FileSize
            $totalSize = $EventArgs.TotalBytesToReceive | Get-FileSize        
            Write-Progress -Activity (" $url {0:N2} Mbps" -f $averageSpeed) `
                -Status ("{0} of {1} ({2}% in {3})" -f $receivedSize,$totalSize,$EventArgs.ProgressPercentage,$elapsed) `
                -SecondsRemaining $remainingSeconds `
                -PercentComplete $EventArgs.ProgressPercentage
            if ($EventArgs.ProgressPercentage -eq 100){
                 Write-Progress -Activity (" $url {0:N2} Mbps" -f $averageSpeed) `
                -Status 'Done' -Completed
                if ($event.MessageData.includeStats.IsPresent){
                    ([PSCustomObject]@{Name='Get-FileWCAsynchronous';TotalSize=$totalSize;Time=$elapsed}) | Out-Host
                }
            }
        }    
    $null = Register-ObjectEvent -InputObject $wc -EventName DownloadFileCompleted `
         -SourceIdentifier WebClient.DownloadFileCompleted -Action { 
            Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
            Unregister-Event -SourceIdentifier WebClient.DownloadFileCompleted
            Get-Item $destination | Unblock-File
        }  
    try  {  
        $wc.DownloadFileAsync($url, $destination)  
    }  
    catch [System.Net.WebException]  {  
        Write-Warning "Download of $url failed"  
    }   
    finally  {    
        $wc.Dispose()  
    }  
 }

以上是关于powershell 使用System.Net命名空间中的WebClient类的DownloadFileAsync方法下载文件的主要内容,如果未能解决你的问题,请参考以下文章

System.Net.WebException 的 Powershell 处理

24.2 网络编程基础

C#使用 System.Net.Mail发送邮件功能

个人对 Socket 和协议的理解 以及 使用 System.Net.Sockets 命名空间编写简单的 TCP 通讯程序

命名空间在类中找到,但在 aspx 中没有

Powershell使用边界MIME将pdf文件上传到RightFax Web服务