Melinda
Melinda

Reputation: 1531

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

Upvotes: 1

Views: 2836

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202272

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" 

Upvotes: 2

Leon Evans
Leon Evans

Reputation: 146

This is the working code I use in production across a few customers:

The $session variable writes the log file.

$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

Upvotes: 0

Related Questions