Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

I know. Read the Session.DebugLogLevel documentation. It sets a level of both logs (those logs that are enabled).
Adrian_hansraj

Hi Martin,

Thank you for the response, however I don't want the debug log, I just want to enable a session log like there is in the GUI, where you enable session log and then you select the verbose level, but I want to be able to do that from a PowerShell script, I already tried the debug log and it throws too much information, when I tested the session log with a reduced verbose level (enable session on logging level: Reduced) with the GUI it gave me what I wanted, I just need to do that from the script now, any help would be appreciated. Thank you. Please see pic for reference of GUI Option
Adrian_hansraj

Log level

Hi, could anyone tell me what is the code to set the log level for logging sessions?
martin

    ...
    $session.SessionLogPath = "C:\path\to\winscp.log"
    # Connect
    $session.Open($sessionOptions)
    ...
prbrown

Hello,

I'm using a generated script and I'm not really sure how to turn on the log;

Can you show me which part need to be change to enable the log.

Thanks.
# Load WinSCP .NET assembly
Add-Type -Path "C:\ci\WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "####"
    UserName = "####"
    SshHostKeyFingerprint = "ssh-rsa 2048 #####"
    SshPrivateKeyPath = "######k"
}
 
$session = New-Object WinSCP.Session
$opt = New-object Session.SessionLogPath -
 
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Set up transfer options
    $transferOptions = New-Object WinSCP.TransferOptions -Property @{
        PreserveTimestamp = $False
    }
   
    # Transfer files
    $session.PutFiles("C:\ci\upload\", "/upload/*", $False, $transferOptions).Check()
}
finally
{
    $session.Dispose()
}
 
istandard

How to enable session log in PowerShell example

This is the PowerShell/.NET assembly example I found on this site. What code and where do I add it to enable the session log? I'm having another issue with SSH Host fingerprint but I know you will need the log to help.

Here is the example script
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $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("d:\toupload\*", "/home/user/", $False, $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}