微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。