-2

I have a PowerShell script that uses the WinSCP .Net library to get some files from a FTP server.

The script runs automatically over night on a server from a administrator account using Task Scheduler.

The problem is that sometimes (it does not have a pattern or at least I can't figure it out) it gives me an error : "Object reference not set to an instance of an object".

Error:

Exception calling "Open" with "1" argument(s): "Object reference not set to an
instance of an object."
At C:\user\blabla\powershellscript.ps1:47 
char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

The actual line from the code (line 47) is : $session.Open($sessionOptions).

If I run the script or the task from Task Scheduler it runs smoothly and does not give me any error.

PowerShell script:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

$FileSourcePath = '/path_to_the_file'

Add-Type -Path "C:\WinSCP\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "ip_to_the_server"
    UserName = "correct_username"
    Password = "correct_password"
    SshHostKeyFingerprint = "correct_sshrsa_fingerprint"
}

$session = New-Object WinSCP.Session

try {
    # Connect
    $session.Open($sessionOptions)

    # Transfer files
    $session.GetFiles($FileSourcePath, $FileDestinationPath).Check()
} catch {
    $_ | Out-File c:\blabla\errors.txt -Append
} finally {
    $session.Dispose()
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
MrGuwee
  • 1
  • 3
  • 1
    You are trying to use a null object as if it was a properly referenced object. Most of the time, when you try to assign value into object, and if the value is null, then this kind of exception occurs. – Ranadip Dutta Apr 24 '19 at 09:14
  • @RanadipDutta if i understand correctly you are saying that the object $sessionOptions is null ? why is that happening ? (that can't be true because if i run manually the script it does not give me any error) – MrGuwee Apr 24 '19 at 09:54
  • So print it and see in case of automated run which variable inturns is having a null value. Debugging is upto you however you wish to debug. You can log the values or you can print it and check..But the error is mainly because of the null values.It might not be specific to that particular variable but can be any other variable being used prior to that. – Ranadip Dutta Apr 24 '19 at 10:24
  • In that code `$sessionOptions` can hardly be `null` and if it were, the `Session.Open would throw `ArgumentNullException`. – Martin Prikryl Apr 24 '19 at 14:44
  • Set `$session.DebugLogPath` and post a log file showing the problem. – Martin Prikryl Apr 24 '19 at 14:44
  • @MartinPrikryl i dont know if i did it corectly; i did it after creating the object and before opening the session: `$session = New-Object WinSCP.Session $session.SessionlogPath = $logPath # Connect $session.Open($sessionOptions)` and it gives me the same error without a log: `Exception calling "Open" with "1" argument(s): "Object reference not set to an instance of an object."` If i don't get an error it generates me the log – MrGuwee May 02 '19 at 07:50
  • I've asked for `DebugLogPath`, not `SessionlogPath`. – Martin Prikryl May 02 '19 at 07:52
  • @MartinPrikryl My bad. I will change it and come with an update tomorrow if i get any error. – MrGuwee May 02 '19 at 08:42
  • @MartinPrikryl [here](https://pastebin.com/AnNgM9kV) is the log – MrGuwee May 03 '19 at 07:03
  • It's a [bug](https://winscp.net/tracker/1744) that it reports *"Object reference not set to an instance of an object"*. WinSCP should report a more meaningful error. -- Though the root cause is different, but I unfortunately cannot tell what goes wrong -- The only thing I can imagine, is that the machine is overloaded. Did you try setting `Session.Timeout` to a higher value? – Martin Prikryl May 03 '19 at 07:12
  • @MartinPrikryl no. i added now `Timeout = new-timespan -minutes 5` in `$sessionOptions = New-Object WinSCP.SessionOptions -Property @{...` . i will come on monday with an update. Thanks a lot ! – MrGuwee May 03 '19 at 08:29
  • Your comment does not really show a code that sets `Session.Timeout`. – Martin Prikryl May 03 '19 at 08:33
  • That is true. I just described what i did. Here is the code : `# Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = $hostname UserName = $username Password = $password SshHostKeyFingerprint = $sshfp Timeout = new-timespan -minutes 5 }` – MrGuwee May 03 '19 at 08:49
  • I'm back. As far as i can see i now get 2 different errors. Here is [log1](https://pastebin.com/1ANJLD92) and [log2](https://pastebin.com/0btWkycm) – MrGuwee May 06 '19 at 06:52

1 Answers1

-1

I fixed it by moving the scripts from Task Schedule to SQL Server Agent job. Now it works flawless.

Thank you for all the feedback and advices.

MrGuwee
  • 1
  • 3