使用PowerShell和WinSCP .NET程序集统计上传文件的数量

问题描述

我有用于将文件从计算机上传到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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...