Search code examples
.netpowershellwebdavwinscpwinscp-net

PowerShell script not creating log file of WinSCP session


I wrote a PowerShell script where I'm trying to SFTP a file from one of our shares to a vendor SFTP server. The script completes without error but my file never arrives on the SFTP server. I would like to log the session to find out where the problem is but I cannot see the log file out on my share either.

Any help/direction on why my log file is not getting created would be appreciated. Thanks.

Here is my PS code:

# 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() 
} 

Solution

  • You are assigning the SessionLogPath property before creating the $session object.

    This is the correct code:

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