Post a reply

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

Re: Problem Running Poweshell script as Shceduled task

johnwade01 wrote:

. 2019-04-15 10:08:58.151 Reading key file "C:\ConnectScripts\SCPKEY.ppk"
. 2019-04-15 10:08:58.151 Key file contains public key only

That means your .ppk file is actually not a PPK file.
And it "Runs fine if ran from desktop" probably only because you have the matching private key loaded into Pageant (covered in the FAQ you have found).
johnwade01

Problem Running Poweshell script as Shceduled task

Runs fine if ran from desktop. I have sched task set to run as same user.
I have edited IP and Username for security purposes.

I have found this article but not sure how it applies to using the dll vs the exe "https://winscp.net/eng/docs/faq_scheduler"

****PS Script
$start = Get-Date -format "dd-MMM-yyyy HH mm"
$fileout = "Output.csv"
$logfile = "c:\ConnectScripts\myScript.log"
CD \ConnectScripts


Add-Type -Path "C:\ConnectScripts\WinSCPnet.dll"

try{
# Set up session options
Add-content $logfile -value "Begin FTP"
Add-content $logfile -value "Create Session"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "0.0.0.0"
UserName = "UNAME"
SshHostKeyFingerprint = "ssh-ed25519 256 sBvf50pmJKOpC6pvDBDYbTQOP1PQ2DZhwKDQlFXAUPI="
SshPrivateKeyPath = "C:\ConnectScripts\SCPKEY.ppk"
}

$sessionOptions.AddRawSettings("AgentFwd", "1")
$sessionOptions.AddRawSettings("FSProtocol", "2")

$session = New-Object WinSCP.Session


try
{
$session.SessionLogPath = "C:\ConnectScripts\Session.log"

# Connect
$session.Open($sessionOptions)

# Your code
$session.PutFiles("C:\ConnectScripts\" + $fileout, "/dropoff/*").Check()
}
finally
{
$session.Dispose()
}
}
catch
{
Add-content $logfile -value "Error: $($_.Exception.Message)"
}

**** Exception from Powershell log
Error: Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "UNAME".

Authentication failed."

*****Session.log
. 2019-04-15 10:08:57.242 --------------------------------------------------------------------------
. 2019-04-15 10:08:57.242 WinSCP Version 5.15 (Build 9365) (OS 6.2.9200 - Windows Server 2012 Standard)
. 2019-04-15 10:08:57.242 Configuration: nul
. 2019-04-15 10:08:57.242 Log level: Normal
. 2019-04-15 10:08:57.242 Local account: GIA\administrator
. 2019-04-15 10:08:57.243 Working directory: C:\Program Files (x86)\WinSCP
. 2019-04-15 10:08:57.243 Process ID: 2140
. 2019-04-15 10:08:57.243 Command-line: "C:\Program Files (x86)\WinSCP\winscp.exe" /xmllog="C:\Users\administrator.GIA\AppData\Local\Temp\wscp6EB0.01FE8351.tmp" /xmlgroups /xmllogrequired /nointeractiveinput /dotnet=5.15.0 /ini=nul /log="C:\ConnectScripts\Session.log" /console /consoleinstance=_28336_31118057_311
. 2019-04-15 10:08:57.243 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/10/2019, DST End: 11/3/2019
. 2019-04-15 10:08:57.243 Login time: Monday, April 15, 2019 10:08:57 AM
. 2019-04-15 10:08:57.243 --------------------------------------------------------------------------
. 2019-04-15 10:08:57.243 Script: Retrospectively logging previous script records:
> 2019-04-15 10:08:57.243 Script: option batch on
< 2019-04-15 10:08:57.243 Script: batch on
< 2019-04-15 10:08:57.244 Script: reconnecttime 120
> 2019-04-15 10:08:57.244 Script: option confirm off
< 2019-04-15 10:08:57.244 Script: confirm off
> 2019-04-15 10:08:57.244 Script: option reconnecttime 120
< 2019-04-15 10:08:57.244 Script: reconnecttime 120
> 2019-04-15 10:08:57.244 Script: open "sftp://UNAME@0.0.0.0" -hostkey="ssh-ed25519 256 sBvf50pmJKOpC6pvDBDYbTQOP1PQ2DZhwKDQlFXAUPI=" -privatekey="C:\ConnectScripts\SCPKEY.ppk" -timeout=15 -rawsettings AgentFwd="1" FSProtocol="2"
. 2019-04-15 10:08:57.244 --------------------------------------------------------------------------
. 2019-04-15 10:08:57.244 Session name: UNAME@0.0.0.0 (Ad-Hoc site)
. 2019-04-15 10:08:57.244 Host name: 0.0.0.0 (Port: 22)
. 2019-04-15 10:08:57.244 User name: UNAME(Password: No, Key file: Yes, Passphrase: No)
. 2019-04-15 10:08:57.244 Tunnel: No
. 2019-04-15 10:08:57.244 Transfer Protocol: SFTP
. 2019-04-15 10:08:57.244 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2019-04-15 10:08:57.244 Disable Nagle: No
. 2019-04-15 10:08:57.244 Proxy: None
. 2019-04-15 10:08:57.244 Send buffer: 262144
. 2019-04-15 10:08:57.244 SSH protocol version: 2; Compression: No
. 2019-04-15 10:08:57.244 Bypass authentication: No
. 2019-04-15 10:08:57.244 Try agent: Yes; Agent forwarding: Yes; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2019-04-15 10:08:57.244 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom:
. 2019-04-15 10:08:57.244 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2019-04-15 10:08:57.244 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2019-04-15 10:08:57.244 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2019-04-15 10:08:57.244 Simple channel: Yes
. 2019-04-15 10:08:57.244 Return code variable: Autodetect; Lookup user groups: Auto
. 2019-04-15 10:08:57.244 Shell: default
. 2019-04-15 10:08:57.244 EOL: LF, UTF: Auto
. 2019-04-15 10:08:57.244 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2019-04-15 10:08:57.244 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
. 2019-04-15 10:08:57.244 SFTP Bugs: Auto,Auto
. 2019-04-15 10:08:57.244 SFTP Server: default
. 2019-04-15 10:08:57.244 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2019-04-15 10:08:57.244 Cache directory changes: Yes, Permanent: Yes
. 2019-04-15 10:08:57.244 Recycle bin: Delete to: No, Overwritten to: No, Bin path:
. 2019-04-15 10:08:57.244 DST mode: Unix
. 2019-04-15 10:08:57.244 --------------------------------------------------------------------------
. 2019-04-15 10:08:57.245 Looking up host "0.0.0.0" for SSH connection
. 2019-04-15 10:08:57.245 Connecting to 0.0.0.0 port 22
. 2019-04-15 10:08:57.310 We claim version: SSH-2.0-WinSCP_release_5.15
. 2019-04-15 10:08:57.362 Server version: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
. 2019-04-15 10:08:57.362 Using SSH protocol version 2
. 2019-04-15 10:08:57.362 Have a known host key of type ssh-ed25519
. 2019-04-15 10:08:57.407 Doing ECDH key exchange with curve Curve25519 and hash SHA-256
. 2019-04-15 10:08:58.023 Server also has ecdsa-sha2-nistp256/ssh-rsa host keys, but we don't know any of them
. 2019-04-15 10:08:58.024 Host key fingerprint is:
. 2019-04-15 10:08:58.024 ssh-ed25519 256 39:a4:21:74:37:42:7a:81:08:46:78:47:db:d2:23:32 sBvf50pmJKOpC6pvDBDYbTQOP1PQ2DZhwKDQlFXAUPI=
. 2019-04-15 10:08:58.025 Host key matches configured key
. 2019-04-15 10:08:58.025 Initialised AES-256 SDCTR client->server encryption
. 2019-04-15 10:08:58.025 Initialised HMAC-SHA-256 client->server MAC algorithm
. 2019-04-15 10:08:58.025 Initialised AES-256 SDCTR server->client encryption
. 2019-04-15 10:08:58.025 Initialised HMAC-SHA-256 server->client MAC algorithm
. 2019-04-15 10:08:58.151 Reading key file "C:\ConnectScripts\SCPKEY.ppk"
. 2019-04-15 10:08:58.151 Key file contains public key only
! 2019-04-15 10:08:58.151 Using username "UNAME".
. 2019-04-15 10:08:58.196 Server offered these authentication methods: publickey,keyboard-interactive
. 2019-04-15 10:08:58.196 Attempting keyboard-interactive authentication
. 2019-04-15 10:08:58.241 Prompt (keyboard interactive, "SSH server authentication", "Using keyboard-interactive authentication.", "Password: ")
. 2019-04-15 10:08:58.242 Disconnected: Unable to authenticate