我之前使用.bat脚本进行此过程,并且编写PowerShell脚本相对较新。我正在从FTP位置进行传输,需要在传输后从FTP服务器中删除每个文件。这个的正确语法是什么?代码如下: -
$a=Get-Date -format MMMM
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "xxx.xxx.xxx.xxx"
UserName = "joe"
Password = "joebloggs"
SshHostKeyFingerprint = "ssh-rsa 1024 01:07:da:11:22:33:44:55:66"
FtpMode = [WinSCP.FtpMode]::Active
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.GetFiles("/*", "C:\Users\User\Dropbox\Rockar\Data\System Data\Raw Feeds\Stock\"+$a+"\", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
答案 0 :(得分:2)
在WinSCP网站上进一步阅读有关getfiles参数winscp.net/eng/docs/library_session_getfiles#parameters的详细信息,发现答案只需将$true
中的$false
设置为Middleware
答案 1 :(得分:0)
你应该致电$session.RemoveFiles
,这是方法说明:
https://winscp.net/eng/docs/library_session_removefiles