Unable to log output from powershell script.

Advertisement

gothbox
Joined:
Posts:
7

Unable to log output from powershell script.

Hi all!

I'm really new to powershell, and need some help figuring out why this script fails.

Here's the script:

try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null

# Setup session options
$SessionOptions.DisableVersionCheck
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "***"
$sessionOptions.UserName = "***"
$sessionOptions.Password = "***"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 ***"
$session.SessionLogPath = "\\***\Log\Uploaded.log"
$session = New-Object WinSCP.Session

try
{
$session.ExecutablePath = "D:\Program Files (x86)\WinSCP\winscp.exe"
# Connect
$session.Open($sessionOptions)

$localPath = "\\***\testwebexp*.txt"
$remotePath = "/**/out/"
$backupPath = "\\***exp\processed\"

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

# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}


The script works well when i remove the line :
$session.SessionLogPath = "\\linux1\v\prod\dataexch\no2\vismaservicessere\exp\Log\Uploaded.log"


But when i try to run it with the løine it fails with this error:
The property 'SessionLogPath' cannot be found on this object. Verify that the property exists and can be set.


These's somewhere where i need to include this, but im do dumt to figure it out myself.
Could anyone please give me a helping hand ?

Reply with quote

Advertisement

gothbox
Joined:
Posts:
7

Figured it out :)

Had to put $session = New-Object WinSCP.Session before $session.SessionLogPath = "\\***\Log\Uploaded.log" and it worked :)

But does anyone know if there is possible to log the size of the files transferred ?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

gothbox wrote:

But does anyone know if there is possible to log the size of the files transferred ?
Well, it's your local files, so you can query their size. Transfer size is the same (as you use binary mode transfer).

Reply with quote

Advertisement

You can post new topics in this forum