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 "WinSCPnet.dll"
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 + "*.pmt"), ($remotePath + "*.pmt"), $false, $transferOptions)
$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:
31,808
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.

Reply with quote

Guest

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

martin wrote:

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

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:
31,808
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

martin wrote:

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.

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

You can post new topics in this forum