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

Count number of uploaded file using PowerShell and WinSCP .NET assembly

我有用于将文件从我的计算机上传到 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 returns TransferOperationResult。它有 Transfers 属性 和汇款集合。

$results = $session.PutFiles("C:\Myfileslocation\*.*", $path+"/*")
$results.Check() # Throws if any transfer failed

$count = $results.Transfers.Count