locked
Powershell Script Automation RRS feed

  • Question

  • I have a script that connects to an SFTP location and synchronises a remote and Local location.  This works like a dream when i run it manually 100%.

    I need to run this script every 15 mins and as such I have created a Task Scheduler Job. 

    When i run the task scheduler job once created it runs but nothing happens.  

    I can seem to get this job to run,  the credentials i am using are the same i created the script in and when i run manually it works it is only when i try to run the script using task scheduler it does nothing.  I am running out of ideas. 

    Any assistance would be greatly appreciated.   

    Tuesday, July 9, 2019 8:51 AM

Answers

  • I suspect that it is the firewall.  PowerShell is allowed through the firewall by default.  The Task Scheduler is not.

    To test this turn off your firewall and retest.

    The authentication that is erring is the SFTP auth and not the account auth.  If the account was failing the log would never get created.

    The first error controls the issue.

    "Error: Exception calling "Open" with "1" argument(s): "Disconnected: No supported authentication methods available "

    You will need to contact the vendor of your DLL to learn why this may be happening if it is not firewall related.

    Note that FTP requires two ports. One data port and one command port. 


    \_(ツ)_/


    Tuesday, July 9, 2019 11:12 AM

All replies

  • Hi,

    I think you need to double check the task scheduler configuration. Can you post some screenshots of your config? Do you need to enter parameters when you run the script manually?

    Regards,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    Tuesday, July 9, 2019 9:00 AM
  • Add error detection to your script that writes to a file.  

    Check the scheduler log to get the completion status.

    Any simple script will run in the scheduler.  Start be running a simple script that write to a log file to be sure the task is set up correctly.


    \_(ツ)_/

    Tuesday, July 9, 2019 9:12 AM
  • Hi There,  I am having issues posting pictures at the moment however the Task Schedule config is basically 

    On the action calling the powershell.exe 

    Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    The only Argument I have is 

    Add argument (Optional): -ExecutionPolicy Bypass C:\Admin\Scripts\SFTPSyncScript.ps1

    As stated I have ensured that the script works when ran manually however when ran manually it does prompt me to Change the Execution Policy.  I am thinking that the task scheduler is running into this issue and the bypass i am using is not working. 

    Tuesday, July 9, 2019 10:51 AM
  • Before continuing please read the following: https://westmesatech.com/?p=56

    You are not posting correct and complete information.  We cannot guess at what you are trying to do.


    \_(ツ)_/


    • Edited by jrv Tuesday, July 9, 2019 10:57 AM
    Tuesday, July 9, 2019 10:56 AM
  • The Script I am using i utilising WinSCP.dlls to connect to a Vendor SFTP server using a key,  the script is pretty straight forward and is working when ran manually (however the Execution policy needs to be manually modified.    When I run the script manually the log produces the following. 

    [07/09/2019 11:01:38] Sync Started
    [07/09/2019 11:01:39] Upload of /Vendor/Test-File.txt succeeded
    [07/09/2019 11:01:39] Permissions of D:\Destination\Test-File.txt kept with their defaults
    [07/09/2019 11:01:39] Timestamp of D:\Destination\Test-File.txt kept with its default (current time)

    When I use the Task Scheduler to run the script the log on the script is pulling back the following error.

    [07/09/2019 10:58:59] Sync Started
    [07/09/2019 10:59:00] Error: Exception calling "Open" with "1" argument(s): "Disconnected: No supported authentication methods available (server sent: publickey)
    Authentication log (see session log for details):
    Using username "test_account".

    Authentication failed."

    The Task Scheduler states the task completed with return code with return code 2147942401.

    I have looked this up and i can confirm i am using an account with admin permissions and it has logon as a batch job permissions.  

    Tuesday, July 9, 2019 11:07 AM
  • I guess i am looking for advice on why a script works perfectly well when you run in manually but fails to run when you put it in the task scheduler.

    The post previous states the 2 line in the task scheduler actions i am guessing maybe my arguments are in correct to get around the execution policy which i am being prompted for when i run it manually.

    I am not looking for assistance with the script itself.  

    Tuesday, July 9, 2019 11:12 AM
  • I suspect that it is the firewall.  PowerShell is allowed through the firewall by default.  The Task Scheduler is not.

    To test this turn off your firewall and retest.

    The authentication that is erring is the SFTP auth and not the account auth.  If the account was failing the log would never get created.

    The first error controls the issue.

    "Error: Exception calling "Open" with "1" argument(s): "Disconnected: No supported authentication methods available "

    You will need to contact the vendor of your DLL to learn why this may be happening if it is not firewall related.

    Note that FTP requires two ports. One data port and one command port. 


    \_(ツ)_/


    Tuesday, July 9, 2019 11:12 AM
  • Thanks i will give it a try. 
    Tuesday, July 9, 2019 11:12 AM
  • Actually SFTP uses one port with SSH on port 22.  I still think it is likely the firewall.


    \_(ツ)_/

    Tuesday, July 9, 2019 11:16 AM

  • [07/09/2019 10:59:00] Error: Exception calling "Open" with "1" argument(s): "Disconnected: No supported authentication methods available (server sent: publickey)
    Authentication log (see session log for details):

    If the server sent the public key, then to me that would indicate that you've gotten thru the firewall. 

    Have you configured to run the task with the same account that you have run it manually?

    What's in the authentication and session logs?

    https://winscp.net/eng/docs/logging_xml

    Tuesday, July 9, 2019 1:44 PM

  • [07/09/2019 10:59:00] Error: Exception calling "Open" with "1" argument(s): "Disconnected: No supported authentication methods available (server sent: publickey)
    Authentication log (see session log for details):

    If the server sent the public key, then to me that would indicate that you've gotten thru the firewall. 

    Have you configured to run the task with the same account that you have run it manually?

    What's in the authentication and session logs?

    https://winscp.net/eng/docs/logging_xml

    Not necessarily.  The error is not always correct.  It depends on how the vendor coded this step.  The server response is often bogus when authentication fails.  The issue can also be due to using SSL when TLS is required although it works manually so that isn't the issue. 

    "Open" is a complex set of steps.  The first is authentication but that requires SSL/TLS to set up first.  Often the failure to connect is seen as a protocol failure when it is a block.  Only the vendor knows how this is set up.


    \_(ツ)_/

    Tuesday, July 9, 2019 1:52 PM
  • thanks for the reply,  Will check the WinSCP logs however as you have stated if i run it manually and it works i guess it points to the firewall not being an issue.  I did however disable the firewall briefly just to test and am still receiving the same issue.  
    Tuesday, July 9, 2019 2:52 PM
  • The error is not always correct.  


    True. But without seeing the code or a verbose session log, we're just guessing at what the problem might be.  
    Tuesday, July 9, 2019 3:03 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, July 16, 2019 9:09 AM
  • It seems as if this script is temperamental.  I opened up the key using Putty gen and copied the fingerprint from in here and it seemed to work up until recently i am receiving the same error message again. 

    i will test further and when i have a solution i will Mark it as the answer

     
    Wednesday, August 14, 2019 10:39 AM
  • What is in the WinSCP logs? 
    Wednesday, August 14, 2019 12:43 PM