我试图在上传文件时获取文件大小。我的最终目标是尝试获取上传速度,但在我的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
}
由于
答案 0 :(得分:0)
不完全是您问题的直接答案,但您可以使用Powershell FTP模块 - 效果很好,也有进度条... (http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb)