Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-WinSCPSession: You cannot call a method on a null-valued expression #131

Closed
Gaz2600 opened this issue Aug 18, 2020 · 14 comments
Closed

Comments

@Gaz2600
Copy link

Gaz2600 commented Aug 18, 2020

Please fill in the following fields, replacing the values inside the brackets {{ }}.

Issue Description

My SFTP code has stopped working and I'm getting conflicting results. If I reboot my server and run the script it works over and over but if I open a new .ps1 with the same script it starts erroring with "New-WinSCPSession: You cannot call a method on a null-valued expression"

Example

Import-Module -Name WinSCP -RequiredVersion 5.17.5.3
$Credential = Import-CliXml “C:\Creds\Account.xml”
$Hostname = "my.sftp.net"
$Protocol = "sftp"
$Port = 22
$HostKeyFingerprint = "ssh-rsa 2048 1c:2f:3f:13:........."
$SessionOptions = New-WinSCPSessionOption -HostName $Hostname -Protocol $Protocol -PortNumber $Port -Credential $Credential -SshHostKeyFingerprint $HostKeyFingerprint
$Session = New-WinSCPSession -SessionOption $SessionOptions
if ($session){
Receive-WinSCPItem -WinSCPSession $Session -RemotePath "/export/file.csv" -LocalPath "C:\Data\file.csv"
Remove-WinSCPSession -WinSCPSession $session
}

Expected Output

I expect a successful connection

Actual Output

After a reboot the script runs fine, I can run it over and over but if I open the script in another .ps1 instance I get an error "New-WinSCPSession: You cannot call a method on a null-valued expression" if I go back to the working .ps1 I start getting the error on that script also.

WinSCP-PowerShell Version

5.17.5.3

Environment

Windows 2019 10.0.17763
PSVersion: 5.1.17763.1007
FTP Server: CerberusFTPServer_11.0

@tomohulk
Copy link
Owner

if I had to take a guess, I bet its something with this: $Credential = Import-CliXml “C:\Creds\Account.xml”. All of your other values are hard coded except that. Have you tried just outputting all your variables and making sure they all have values. This module hasn't updated in a while (im trying to find time to rectify that) so nothing has changed in the code in quite some time.

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 19, 2020

After a reboot it works is the thing so I know the variables are good, it's only when I open a 2nd script containing the same formatted code with different variable for another SFTP site that it breaks and going back to the original script that was working after the reboot it is then broken. If you don't plan on making any updates anytime soon do you have any alternative recommendations for SFTP in powershell?

@tomohulk
Copy link
Owner

im trying to get this updated, I have to re write all my pester test because pester was completely refractored, I just haven't had time, and I have been using Mac for the past year or more, so this thing doesn't do me much good any more.

can you dump all you're variables from the second script. Based on the error, some value is $null, and that is where the issue is. I don't think its with any logic in the code.

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 20, 2020

None of the variables in my script are empty, even the $credential data is in the $sessionsoptions variable. I also just tried removing as many variables as I could and still get the error:

$sessionoptions = New-WinSCPSessionOption -HostName "my.sftp.com" -Protocol "sftp" -PortNumber 22 -Credential (Get-Credential) -SshHostKeyFingerprint "ssh-rsa 2048 00:00:00:00:00:00:00:00:00:00..."
$session = New-WinSCPSession -SessionOption $sessionOptions
$session

Error: New-WinSCPSession : You cannot call a method on a null-valued expression

@tomohulk
Copy link
Owner

Can you clone this repo, and load up the latest version. Im still trying to get the build pipeline working so I can publish it. But one of the biggest thing is that it will now load up the proper dll, based on Core or Desktop pwsh versions. Also has the latest version of the dlls.

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 20, 2020

whats the best way to load the module? do I copy this into C:\program files\windowspowershell\modules\winscp\5.17.5.4"

@tomohulk
Copy link
Owner

tomohulk commented Aug 20, 2020

you can just put it on your desktop and use the -Path parameter, make sure to start a new session or you will need to add the -Force Parameter as well.
Import-Module -Path .\Desktop\WinSCP-Master\WinSCP

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 20, 2020

first few tests are working, let me add this to a few other scripts

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 20, 2020

2nd test I get an error
"New-Alias " The alias is not allowed, because an alias with the name 'Enter-WinSCPSession' already exists." and
"New-Alias : The alias is not allowed, because an alias with the name 'Open-WinSCPSession' already exists" and
"New-Alias : The alias is not allowed, because an alias with the name 'Close-WinSCPSession' already exists" and
"New-Alias : The alias is not allowed, because an alias with the name 'Exit-WinSCPSession' already exists"

I am doing Remove-WinSCPSession at the end of each session also. And even with these errors it is still connecting and downloading the files.

@tomohulk
Copy link
Owner

you need to re start your powershell session, its trying duplicate the aliases when importing the module, or you can use -Force when you import the module.

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 20, 2020

ok that takes care of that error

@tomohulk
Copy link
Owner

is it good to say the updated code fixes this error? I ma about half way through my pester rewrites, so I hopping to have the build pipeline back up and running sometime next week to get this to the PSGallery.

@Gaz2600
Copy link
Author

Gaz2600 commented Aug 22, 2020 via email

@tomohulk
Copy link
Owner

well, took all weekend, but the build pipeline seems to be restored lol. had to re write 120+ Pester tests to work from v3 to v5. Anyway, its published to the gallery and the releases here. im going to close this. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants