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

Advertisement

sachinG
Joined:
Posts:
2
Location:
India

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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,605
Location:
Prague, Czechia

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

Most probably the local account, used to run the Scheduler's task, does not have permissions to the shared folder \\servername\directoryname.

See also My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?

Reply with quote

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,605
Location:
Prague, Czechia

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.

Reply with quote

sachinG
Joined:
Posts:
2
Location:
India

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.

Reply with quote

Advertisement

hajareyogesh1979@gmail.com
Joined:
Posts:
2
Location:
india

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.

Reply with quote

hajareyogesh1979@gmail.com
Joined:
Posts:
2
Location:
india

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"

Reply with quote

Advertisement

You can post new topics in this forum