-1

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:

  1. Powershell v2.0
  2. .Net 4.0
  3. Standalone WinSCP (no privilege for installing)
  4. Window Server 2008
  5. 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
  6. Running using task scheduler for daily basis
user3542587
  • 199
  • 1
  • 13
  • tried. seems not capture any log > – user3542587 Jul 25 '19 at 15:04
  • sorry. typo in here. in real script is correct. – user3542587 Jul 25 '19 at 15:08
  • @MartinPrikryl I have marked up the folder. so, i have full access on the folder at the moment. Now, i scratching my head why manually execute able to transfer file, but when set task scheduler not able to transfer file. I read your another post where u suspect "D:\Users\*.zip" have to put "\\computername\Users\*.zip" i tried and seem not working also. – user3542587 Jul 25 '19 at 15:24
  • @MartinPrikryl execute manually will create file. but no log in written inside the log file. – user3542587 Jul 26 '19 at 12:21
  • If WinSCP had problems creating a log file, the `$session.Open` would throw an exception and no upload would happen. I cannot imagine that you can get the behavior you are describing (not log, but uploads succeeds). – Martin Prikryl Jul 26 '19 at 12:38
  • Anyway, add this after `PutFiles`: `Set-Content -Path 'C:\Users\Output.log' ($session.Output -join "\`n")` - What do you get in `Output.log` when run manually and when run in scheduler? – Martin Prikryl Jul 26 '19 at 12:39
  • @MartinPrikryl my apologies. if execute manually, i will have logs inside the log file. now have to find reason why set task scheduler not able to transfer file. https://winscp.net/eng/docs/faq_scheduler i studied this web, seems like every criteria on point, please correct me if im wrong. – user3542587 Jul 26 '19 at 13:00
  • I do not understand. So *did you* or *did you not* found a reason why the script does not work in scheduler? – Martin Prikryl Jul 26 '19 at 13:08
  • @MartinPrikryl sorry. I didnt found a reason why the script doesnt work in scheduler but it works when execute manually. anything lacking in my script? – user3542587 Jul 26 '19 at 14:48
  • Add this this to the beginning of your script: `try {` - And this to the end of the script: `Set-Content -Path 'D:\Users\result.log' 'Success' } catch { Set-Content -Path 'D:\Users\result.log' $_.Exception.Message }` - What will you get in `D:\Users\result.log` then, when executed from scheduler? – Martin Prikryl Jul 26 '19 at 15:02
  • @MartinPrikryl no luck. still no result when executed from scheduler. – user3542587 Jul 27 '19 at 10:16
  • @MartinPrikryl found answer why able execute manually but cannot execute using job scheduler. please add this in WinSCP web. 1. argument to call job scheduler then you need add $sessionOptions.GiveUpSecurityAndAccepptAnySshHostKey = "true" << this i follow 1 of your answer where "SFTP without using HostKey" – user3542587 Jul 27 '19 at 14:40
  • What does it mean *"still no result"*? Do you mean that no `result.log` is created? – Martin Prikryl Jul 30 '19 at 06:35

1 Answers1

-1

Argument to call job scheduler might not be correct and always shows hit (0x1)

  1. Start a program > %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe

  2. Argument > -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "\path\to\file.ps1"

Refer to here: PowerShell script won't execute as a Windows scheduled task

user3542587
  • 199
  • 1
  • 13
  • Do not use `GiveUpSecurityAndAcceptAnySshHostKey`, unless you do not care about security. Not to mention that it's `GiveUpSecurityAndAcce**p**tAnySshHostKey` not `GiveUpSecurityAndAcce**pp**tAnySshHostKey` - so the code in your answer can never work. + *"if your MFT server doesnt accept any ssh host key"* is a total non sense. Server does not accept any host keys. It's the client that is accepting server's host keys. And WinSCP can of course accept any host key, if you use the correct syntax. – Martin Prikryl Jul 30 '19 at 06:34
  • OK, so you have removed the `GiveUpSecurityAndAcceptAnySshHostKey` -- But now I do not see how does your answer actually answer your question. – Martin Prikryl Aug 02 '19 at 16:25
  • @MartinPrikryl i not much changing my script. I just change the argument to call for scheduler. -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "\path\to\file.ps1" – user3542587 Aug 06 '19 at 11:31
  • So would you explain in your answer what exactly did you change and why it does help? – Martin Prikryl Aug 06 '19 at 11:36
  • So it was the `-ExecutionPolicy Bypass`? So your script did not run at all? Nothing to do with *"transfer"*? – Martin Prikryl Aug 06 '19 at 12:42