Able to transfer file to MFT server if execute manually. Not able to transfer file to MFT server if set as job scheduler. Is there any wrong with my script or do I need add extra credential? Tried to find log for file transfer, no luck. I cant find any log for it.
####Load WinSCP .NET assembly
Add-Type -Path "D:\Users\WinSCPnet.dll" -verbose
$session = New-Object WinSCP.Session
$session.SessionLogPath = "D:\Users\SessionLog_$todayLogFile.log"
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "[linux IP address]"
$sessionOptions.UserName = "[user name share between linux]"
$sessionOptions.PortNumber = "[port number of linux]"
$sessionOptions.Password = ""
$sessionOptions.SshPrivateKeyPath = "D:\Users\Private.ppk"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 ......="
$sessionOptions.GiveUpSecurityAndAccepptAnySshHostKey = "true"
try
{
# Open the WinSCP.Session object using the WinSCP.SessionOptions object.
#$session.SessionLogPath = "D:\Users\WinSCPtxt.log"
$session.Open($sessionOptions)
# Open WinSCP.TransferOptions
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False
$transferFile = Get-ChildItem "D:\Users\*.zip" | Where-Object {($_.LastWriteTime -ge (Get-Date).AddDays(-1))}
$destinationMFT = "/Outbox/"
# Upload
$session.PutFiles( $transferFile, $destinationMFT , $False, $transferOptions).Check()
}
#catch { Set-Content -Path 'D:\Users\result.log' $_.Exception.Message }
finally
{
# Disconnect, clean up
$session.Dispose()
}
Software:
- Powershell v2.0
- .Net 4.0
- Standalone WinSCP (no privilege for installing)
- Window Server 2008
- WinSCP 5.13.9. If cannot load WinSCP.dll Loading WinSCP in PowerShell - Could not load file or assembly - This assembly is built by a runtime newer than the currently loaded runtime
- Running using task scheduler for daily basis