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

hajareyogesh1979@gmail.com

While running the Script in Windows Powershell ISE it is working fine. Task Scheduler does not work

While running the Script in Windows Powershell ISE it is working fine.
getting a success below result in Powershell ISE
PS D:\YH SFTP\TEST_Staging> D:\YH SFTP\TEST_Staging\1_To_Staging_Final_22_OCT_2020_Ver_2.ps1

Upload of D:\TEST_STAGING\Staging_Send_TO_\3_22_OCT_2020.txt succeeded, moving to backup

but when I schedule the same script in at all.
-Command  "start-process -verb runAs "powershell " -argumentlist "D:\YH SFTP\TEST_Staging\1_To_Staging_Final_22_OCT_2020_Ver_2.ps1"
martin

Re: same issue Script working fine while running from powershell ISE but not working in Task Shedular

@hajareyogesh1979: Sorry, your post does not include enough information.

Please read how to troubleshoot problems with WinSCP. If it turns out that you are not able to help yourself, read how to ask for support or report bugs efficiently, so others can help you.
hajareyogesh1979@gmail.com

same issue Script working fine while running from powershell ISE but not working in Task Shedular

Same issue for me, script working fine while running from PowerShell ISE but not working in Task Scheduler.
sachinG

Re: The Script works well when run in Powershell ISE but does not work through task scheduler

Thank you Martin, Doing below two things in a Windows scheduler for this particular batch, fixed this issue for me.
1. Unchecked "Run with Highest Privileges"
2. Select option "Run only when user is logged on"

Best Regards,
Sachin.
martin

Re: The Script works well when run in Powershell ISE but does not work through task scheduler

So can do dir \\servername\directoryname in your PowerShell script, before you do $session.PutFiles?

Also, please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
Guest

Re: The Script works well when run in Powershell ISE but does not work through task scheduler

Thanks for the reply @Martin. I had specified the same account to run this task from the Task Scheduler's which I am using to run it from PowerShell ISE. Also tried to create the files manually in the UNC path using the same account which is successful.
sachinG

The script works well when run in PowerShell ISE but does not work through Task Scheduler

Hi, The below script does upload a file to remote server successfully when ran in PowerShell ISE manually but shows below log in the session log and does not upload any files when executed through Scheduler. Any suggestion are greatly appreciated.

> 2019-07-14 21:49:30.655 Script: put -nopermissions -nopreservetime -transfer="binary" -filemask="|*/" -- "\\servername\directoryname\*" "/remotepath/"
< 2019-07-14 21:49:45.658 Script: No file matching '*' found. (System Error. Code: 1317.
< 2019-07-14 21:49:45.658 The specified account does not exist)

The Script as below:
param (
    $localPath = "\\LocalUNCPath\*",
    $remotePath = "/remotepath/",
    $backupPath = "\\LocalUNCbackupPath"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "hostname"
        UserName = "userid"
        Password = "password"
        SshHostKeyFingerprint = "ssh-rsa key"
    }
 
    $session = New-Object WinSCP.Session
    $DateTime = Get-Date -format dd-MM-yyyy
    $session.SessionLogPath = "C:\LogDir\Sessionlog$DateTime.log"
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.FileMask = "|*/"
        $transferOptions.FilePermissions = $Null
        $transferOptions.PreserveTimestamp = $false;
 
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath, $false, $transferOptions)
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
                Send-MailMessage -To “<EmailID>" -From “<EmailID>" -SMTPServer smtpServerName -Subject “Upload of $($transfer.FileName) Successful - $env:COMPUTERNAME” -Body “Upload of $($transfer.FileName) Successful: $env:COMPUTERNAME”
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
                Send-MailMessage -To “<EmailID>" -From “<EmailID>" -SMTPServer smtpServerName -Subject “Upload of $($transfer.FileName) failed - $env:COMPUTERNAME” -Body “Upload of $($transfer.FileName) failed: $env:COMPUTERNAME $($transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}