【发布时间】:2014-04-08 13:16:35
【问题描述】:
我正在上传文件时尝试获取文件大小。我的最终目标是尝试获得上传速度,但在我的while循环中,我得到一个异常调用“OpenRead”,参数为“1”:“远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问)。
请看下面的代码
$uploadRemoteFile = "Remote URI"
$ftpuname = "UserName"
$ftppassword = 'Password'
function upload-ftp{
$File = "$env:TEMP\something.exe"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpuname,$ftppassword);
$uri = New-Object System.Uri($uploadRemoteFile)
$webclient.UploadFileAsync($Uri, $File)
$arrayftp = @()
Function Get-newfilesize{
$webclientRead = New-Object System.Net.WebClient;
$webclientRead.Credentials = New-Object System.Net.NetworkCredential($ftpuname,$ftppassword);
[void]$webclientRead.OpenRead($uri);
[Int64]$bytes_total= ($webclientRead.ResponseHeaders["Content-Length"])
$webclientsize = ($bytes_total.ToString());
$webclientsize
}
while ($webclient.IsBusy){
$oldftpfile = Get-newfilesize;
$oldftpdate = Get-Date;
Start-Sleep -Milliseconds 1
$newftpfile = Get-newfilesize;
$newftpdate = Get-Date;
$ftpsizediff = $newftpfile - $oldftpfile;
$ftptimediff = $newftpdate - $oldftpdate;
$totalftpdiff = $ftpsizediff / $ftptimediff.totalseconds;
$totalftpdiff | foreach {
if ($_ -gt 0){$arrayftp += $_ }
}
}
$testftpcap= New-Object psobject -Property @{"Upload Speed" =((($arrayftp | measure -Average).Average/1MB)* 10)}
$testftpcap | Export-csv -NoTypeInformation -Path $env:TEMP\ftpspeed.csv
}
谢谢
【问题讨论】:
标签: powershell ftp webclient