问题描述
我有用于将文件从计算机上传到FTP的脚本。但是我想计算一下我的脚本上传了多少文件。我正在使用WinSCP脚本。
Add-Type -Path "WinSCPnet.dll"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.DebugLogPath = "C:\forKeppLog\transfer_data_script.log"
$session.Open($sessionoptions)
#Message
Write-Host -ForegroundColor Cyan "`r`nUploading files to $servername..."
Write-Host -ForegroundColor White "Do not close this window!"
$path = "/MyuploadfilesTOFtp/"
# Transfer files
$session.PutFiles("C:\Myfileslocation\*.*",$path+"/*").Check()
}
finally
{
#Inform user
Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername."
#dispose of session
$session.dispose()
}
我可以在日志文件中看到上传了哪些文件,但是使用起来不方便。我想为上传的计数文件数获取一些变量。
我想做这样的事情
Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername. Uploaded file's number is $numberofiles."
解决方法
Session.PutFiles
返回TransferOperationResult
。它具有Transfers
属性,并包含转移的集合。
$results = $session.PutFiles("C:\Myfileslocation\*.*",$path+"/*")
$results.Check() # Throws if any transfer failed
$count = $results.Transfers.Count