Check here for clues and debugging
https://needleinathreadstack.wordpress.com/2016/05/12/why-does-it-take-notepad-20-seconds-to-open/
https://needleinathreadstack.wordpress.com/2016/05/12/why-does-it-take-notepad-20-seconds-to-open/
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Create object WinSCP.SessionOptions`r"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "***.**.***.**"
UserName = "AldoProva"
Password = "***"
SshHostKeyFingerprint = "ssh-rsa 2048 ***"
}
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Object created`r"
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Create object WinSCP.Session`r"
$session = New-Object WinSCP.Session
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Object created`r"
try
{
# Connect
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Open Session`r"
$session.Open($sessionOptions)
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Session opened`r"
# Upload files
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Create object WinSCP.TransferOptions`r"
$transferOptions = New-Object WinSCP.TransferOptions
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Object created`r"
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles("E:\test.txt", "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host $(Get-Date -format "dd/MM/yyyy HH:mm:ss") " Upload of $($transfer.FileName) succeeded`r"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
Write-Host "`r"
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
21/02/2018 17:01:05 Create object WinSCP.SessionOptions
21/02/2018 17:01:05 Object created
21/02/2018 17:01:05 Create object WinSCP.Session
21/02/2018 17:01:05 Object created
21/02/2018 17:01:05 Open Session
21/02/2018 17:01:07 Session opened
21/02/2018 17:01:07 Create object WinSCP.TransferOptions
21/02/2018 17:01:07 Object created
21/02/2018 17:01:07 Upload of E:\test.txt succeeded
21/02/2018 17:01:32 Create object WinSCP.SessionOptions
21/02/2018 17:01:32 Object created
21/02/2018 17:01:32 Create object WinSCP.Session
21/02/2018 17:01:33 Object created
21/02/2018 17:01:33 Open Session
21/02/2018 17:01:35 Session opened
21/02/2018 17:01:35 Create object WinSCP.TransferOptions
21/02/2018 17:01:35 Object created
21/02/2018 17:01:35 Upload of E:\test.txt succeeded
Option Explicit
' Setup session options
Dim sessionOptions
WScript.Echo Date & " " & Time & " Create object WinSCP.SessionOptions"
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
WScript.Echo Date & " " & Time & " Object created"
With sessionOptions
.Protocol = 0
.HostName = "***.**.***.**"
.UserName = "AldoProva"
.Password = "***"
.SshHostKeyFingerprint = "ssh-rsa 2048 ***"
End With
Dim session
WScript.Echo Date & " " & Time & " Create object WinSCP.Session"
Set session = WScript.CreateObject("WinSCP.Session")
WScript.Echo Date & " " & Time & " Object created"
' Connect
WScript.Echo Date & " " & Time & " Open session"
session.Open sessionOptions
WScript.Echo Date & " " & Time & " Session opened"
' Upload files
Dim transferOptions
WScript.Echo Date & " " & Time & " Create object WinSCP.TransferOptions"
Set transferOptions = WScript.CreateObject("WinSCP.TransferOptions")
WScript.Echo Date & " " & Time & " Object created"
transferOptions.TransferMode = 0
Dim transferResult
Set transferResult = session.PutFiles("E:\test.txt", "/", False, transferOptions)
' Throw on any error
transferResult.Check
' Print results
Dim transfer
For Each transfer In transferResult.Transfers
WScript.Echo Date & " " & Time & " Upload of " & transfer.FileName & " succeeded"
Next
' Disconnect, clean up
session.Dispose
WScript.Echo ""
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
21/02/2018 17:01:07 Create object WinSCP.SessionOptions
21/02/2018 17:01:08 Object created
21/02/2018 17:01:09 Create object WinSCP.Session
21/02/2018 17:01:09 Object created
21/02/2018 17:01:09 Open session
21/02/2018 17:01:11 Session opened
21/02/2018 17:01:11 Create object WinSCP.TransferOptions
21/02/2018 17:01:11 Object created
21/02/2018 17:01:11 Upload of E:\test.txt succeeded
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
21/02/2018 17:01:35 Create object WinSCP.SessionOptions
21/02/2018 17:01:55 Object created
21/02/2018 17:01:55 Create object WinSCP.Session
21/02/2018 17:01:55 Object created
21/02/2018 17:01:55 Open session
21/02/2018 17:01:57 Session opened
21/02/2018 17:01:57 Create object WinSCP.TransferOptions
21/02/2018 17:01:57 Object created
21/02/2018 17:01:57 Upload of E:\test.txt succeeded