PowerShell脚本未创建WinSCP会话的日志文件

时间:2019-01-21 18:55:22

标签: .net powershell webdav winscp winscp-net

我写了一个PowerShell脚本,试图在其中将文件从我们的一个共享SFTP传送到供应商的SFTP服务器。该脚本已正确完成,但我的文件从未到达SFTP服务器。我想登录该会话以找出问题所在,但也看不到共享上的日志文件。

任何关于未创建我的日志文件的帮助/说明都将不胜感激。谢谢。

这是我的PS代码:

# Load WinSCP .NET assembly 
Add-Type -Path "WinSCPnet.dll" 

# Declare variables 
$date = Get-Date 
$dateStr = $date.ToString("yyyyMMdd") 
$filePath = "\\share\apps\CoverageVerifier\cvgver." + $dateStr + ".0101" 

# Set up session options 
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
    Protocol = [WinSCP.Protocol]::WebDAV 
    HostName = "secureftpa.verisk.com" 
    PortNumber = 443 
    UserName = "account" 
    Password = "password" 
    WebdavSecure = $True 
    TlsHostCertificateFingerprint = "00:00:00:00:00:00:00:00:00:..." 
} 

$session.SessionLogPath = "\\share\apps\CoverageVerifier\UploadLog.log" 
$session = New-Object WinSCP.Session 
try 
{ 
    # Connect 
    $session.Open($sessionOptions)
    # Upload files 
    $transferOptions = New-Object WinSCP.TransferOptions 
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary 

    $transferResult = $session.PutFiles($filePath, "/", $False, $transferOptions) 

    # Throw on any error 
    $transferResult.Check() 

    # Print results 
    foreach ($transfer in $transferResult.Transfers) 
    { 
        Write-Host "Upload of $($transfer.FileName) succeeded" 
    } 
} 
catch 
{ 
    Write-Host "Error: $($_.Exception.Message)" 
    exit 1 
} 
finally 
{ 
    $session.Dispose() 
} 

2 个答案:

答案 0 :(得分:2)

在创建SessionLogPath对象之前,您要分配$session属性。

这是正确的代码:

$session = New-Object WinSCP.Session 
$session.SessionLogPath = "\\share\apps\CoverageVerifier\UploadLog.log" 

答案 1 :(得分:0)

这是我在一些客户中用于生产的工作代码:

$ session变量写入日志文件。

$csvsavepath = "C:\SCRIPTS\$teamname\$scriptname\"
$sessionlog = 'sessionlog.txt'
# Setup the connection Options #
$sessionOptions = Set-SFTPSessionOption -HostName ftp.public.com -UserName 'username' -Password "$Password" -SshHostKey 'ssh-rsa 2048 [key here]'

# Create and open session to remote host #
$Session = Open-SFTPSession -sessionOptions $sessionOptions -SessionLogPath "$csvsavepath$sessionlog"

# Upload files to host #
Write-ToSFTPServer -Session $Session -SourcePath "$csvsavepath$csv" -RemotePath "\$csv"  -TransferMode binary -DeleteSource

# clean up connection object #
Close-SFTPSession -Session $session