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

Set-SFTPFile : Object reference not set to an instance of an object. #294

Closed
TLA50 opened this issue Oct 27, 2019 · 9 comments
Closed

Set-SFTPFile : Object reference not set to an instance of an object. #294

TLA50 opened this issue Oct 27, 2019 · 9 comments

Comments

@TLA50
Copy link

TLA50 commented Oct 27, 2019

I am trying to upload a csv file to a sftp site, but keep getting this error. "Set-SFTPFile : Object reference not set to an instance of an object."

My code is below.

Set the credentials

$Password = ConvertTo-SecureString 'BlahBlah' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ('UserNAME', $Password)

Set local file path, SFTP path, and the backup location path which I assume is an SMB path

$FilePath = "C:\Temp\MY_CSV_FILE.csv"
$SftpPath = '/'
$SmbPath = 'C:\temp\Backup'

Set the IP of the SFTP server

$SftpIp = 'sftp.place.co.uk'

Load the Posh-SSH module

Import-Module Posh-SSH

Establish the SFTP connection

$ThisSession = New-SSHSession -ComputerName $SftpIp -AcceptKey $Credential

Upload the file to the SFTP path

Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath

#Disconnect all SFTP Sessions
Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }

Copy the file to the SMB location

Copy-Item -Path $FilePath -Destination $SmbPath

@darkoperator
Copy link
Owner

darkoperator commented Oct 30, 2019 via email

@dbrennand
Copy link

dbrennand commented Feb 27, 2020

I am experiencing the same issue. For both Set-SFTPFile and Set-SFTPItem.

Set-SFTPItem : Object reference not set to an instance of an object.

This could be linked to issue #37.

@darkoperator
Copy link
Owner

darkoperator commented Feb 27, 2020 via email

@dbrennand
Copy link

dbrennand commented Feb 27, 2020

It looks like the function isn't working correctly when passing a -SessionId with an active SSH session using New-SSHSession.

I fixed my issue by creating a SFTPSession using New-SFTPSession. Then passing that object directly into New-SFTPItem and Set-SFTPItem.

Here is my working code:

 
$Test = New-SFTPSession -ComputerName "Someip" -AcceptKey -KeyFile "somekeyfilepath" -Credential $Credential -Verbose:($PSBoundParameters["Verbose"] -eq $true) -ErrorAction "Stop"

New-SFTPItem -SFTPSession $Test -Path "Path to file" -ItemType "File"
Set-SFTPItem -SFTPSession $Test -Destination "somedestination" -Path "somefile" -Force -Verbose:($PSBoundParameters["Verbose"] -eq $true) -ErrorAction "Stop"

@darkoperator
Copy link
Owner

PS C:\Users\carlos\Documents\Posh-SSH\Release> New-SFTPSession -ComputerName 192.168.1.213 -Credential carlos

SessionId  Host                                                                                Connected
---------  ----                                                                                ---------
    0      192.168.1.213                                                                          True


PS C:\Users\carlos\Documents\Posh-SSH\Release> Get-SFTPSession -SessionId 0

SessionId  Host                                                                                Connected
---------  ----                                                                                ---------
    0      192.168.1.213                                                                          True


PS C:\Users\carlos\Documents\Posh-SSH\Release> New-SFTPItem -SessionId 0 -Path /tmp/test.tx -ItemType file


FullName       : /tmp/test.tx
LastAccessTime : 2/27/2020 9:21:13 PM
LastWriteTime  : 2/27/2020 9:21:13 PM
Length         : 0
UserId         : 1000



PS C:\Users\carlos\Documents\Posh-SSH\Release> Set-SFTPItem -SessionId 0 -Destination /tmp -Path C:\deploy.ps1
VERBOSE: Uploading C:\deploy.ps1 to /tmp

Could I be missing something? this is against a Ubuntu system running Open-SSH.

@dbrennand
Copy link

Try using New-SFTPItem with an object created from New-SSHSession.

@darkoperator
Copy link
Owner

darkoperator commented Feb 28, 2020 via email

@dbrennand
Copy link

Ok, but the documentation to me implies that New-SSHSession can be used. It just says to use -SessionId. I interpreted this as being able to use New-SSHSession.

@darkoperator
Copy link
Owner

darkoperator commented Mar 3, 2020 via email

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

3 participants