PowerShell Script not functioning correctly when scheduled Task
Hello, I am having a problem with a PowerShell script that works perfectly when executing it manually from PowerShell, however once I put it into a scheduled task, it appears as if the sessionOptions object is not being created. I have tried running the scheduled task under a domain account with Log on As Batch job access, and with my own account that is a Server adminitrator. Both accounts have the same behaviour, the SFTP session does not connect as a Scheduled task. As per my code below, I am attempting to log the SFTP session, however the log file is never created.
WinSCP = 5.17.6
OS = Server 2008R2 or Server 2012R2
PowerShell = v4.0 or v5.1
CODE:
Any help or ideas would be appricated.
Thanks
WinSCP = 5.17.6
OS = Server 2008R2 or Server 2012R2
PowerShell = v4.0 or v5.1
CODE:
$sessionOptions = New-Object WINSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "Hardcoded SFTP site name" UserName = "Hardcoded SFTP username" SshHostKeyFingerPrint = "Hardcoded FingerPrint" SshPrivateKeyPath = "Hardcoded relative path to .ppk file" PrivateKeyPassphrase = "Hardcoded Passphrase" } $sftpSession = New-Object WinSCP.Session $sftpSession.SessionLogPath = "C:\Automation\SFTPSessionLog.txt" TRY { $sftpSession.Open($sessionOptions) #-- Open SFTP session # Force binary mode transfer $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
Any help or ideas would be appricated.
Thanks