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.
The Script as below:> 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)
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 }