PowerShell中的.NET程序集中的WinSCP -nopreservetime开关

时间:2015-07-20 14:47:55

标签: powershell winscp winscp-net

我正在尝试将-nopreservetime添加到此代码中。我不知道在哪里或如何添加它,我尝试了不同的事情,没有结果。

# Setup session options 
$sessionOptions = New-Object WinSCP.SessionOptions 
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp 
$sessionOptions.HostName = "#######.com" 
$sessionOptions.UserName = "#####" 
$sessionOptions.Password = "#####" 
$sessionOptions.SshHostKeyFingerprint = "#####" 
$sessionOptions.Timeout = "##" 

$TransferOptions.PreserveTimestamp = $false 

$session = New-Object WinSCP.Session 
$Session.SessionLogPath = "C:\Users\user1\Desktop\Winscp\TEST\sessionlog.txt" 
$Session.debugLogPath = "C:\Users\user1\Desktop\Winscp\TEST\debugsessionlog"+$date+".txt"

try 
{ 
    Write-Host $date,("Connecting to FTP site.") 
    # Connect 
    $session.Open($sessionOptions) 
    Write-Host $date,("Connected to FTP site.") 

    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary 

    # Upload files, collect results 
    $transferResult = $session.PutFiles("$localPath", "$remotePath", $False, $transferOptions)
}

这通常是:

$transferResult = $session.PutFiles($localPath, $remotePath) 

我只是想禁用保留时间戳。我在GUI中关闭它,但它不在powershell_ise.exe

中工作

上面的代码在日志中返回:

  

2015-07-20 08:52:44.635脚本:put -nopermissions -preservetime -transfer =" binary" - " C:\ Users \ user \ Desktop \ Winscp ############ *。pdf" " / OUTGOING / TEST ##### / ## /"

1 个答案:

答案 0 :(得分:0)

使用TransferOptions.PreserveTimestamp

$transferOptions.PreserveTimestamp = $False

请参阅Converting transfer settings scripting switches to code based on .NET assembly

你实际上已经有了那段代码,但是在错误的地方。您设置了未初始化变量的属性。

您必须在$transferOptions

初始化后移动线
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary 
$transferOptions.PreserveTimestamp = $False