Author Posts

September 7, 2014 at 3:11 am

Hello,

I try to send a file from a Windows 2012 server to a Linux server using a script.

For that I have tested the following module: SSH-NET Librairy because if it is true ssh is not implemented natively in powershell. The trouble is that I manage to connect in my linux server has powershelle entering my login and password. But whenever I généère a public private key nothing works.

Can you tell me what is the method to do this kind of script?

thank you

September 7, 2014 at 3:19 am

Have you added your public key to the .ssh/authorized_keys file of the user on the Linux machine you're trying to log into?

Please share what you already have as PowerShell script (attach as .txt file or use the code block). It is kind of difficult to help you without you sharing what you already have.

Thanks
Daniel

September 7, 2014 at 7:29 am

Thanks for the reply

The connection with linux is ok , i have all desinstalled adn installed and it's work. Just the scp will not work with my script :
(pre){New-SshSession -Computer 192.168.0.1 -Username test -Keyfile C:\Users\Administrateur\Documents\ssh\id_private -Scp -Sftp -Reconnect
Get-SshSession | ft -AutoSize

Get-DhcpServerv4Lease -ScopeId 192.168.0.0 -ComputerName winserver |
Select-Object -Property ClientId, IPAddress, HostName |
Export-Csv -NoTypeInformation -Path C:\Users\Administrateur\mysql.csv

Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath \home\ubuntu\
}(/pre)

i have read this tutorial

Thanks for help

September 7, 2014 at 8:03 am

I don't have a Linux machine installed to test myself at the moment but I've spotted three possible issues in your Send-ScpItem command.

Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath \home\ubuntu\

1. You've specified the destination path with backslash's instead of forward-slash's which Linux expects.
2. The destination path you've specified is not the "test" users home directory. You need to you use /home/test or give the test user access to /home/Ubuntu.
3. I believe SCP expects the file in the destination path like below:

Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv

September 7, 2014 at 8:58 am

Examples that work for me:

Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath mysql.csv
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath ./mysql.csv
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv

Cheers
Daniel

September 7, 2014 at 10:18 am

Have you mount a linux server ?

That work not by me , i send you the error ( sorry for the language, im french)

can you send me a example with the copy-item command other know you a another methode to send a file ?

Thanks

September 7, 2014 at 10:31 am

I didn't mount the Linux machine. I've used the New-SshSession command as you did to establish the connection with the private/public key and used any of the example commands below to send the file successfully to the Linux machine.

Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath mysql.csv
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath ./mysql.csv
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test
Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv

September 8, 2014 at 9:43 am

I think it comes from that I not made ​​the following module:
[url]http://www.powershelladmin.com/wiki/SSH-Sessions_Add-on_with_SCP/SFTP_Support[/url].

But i try with a copy-item cmdl

Copy-item -v C:\Users\administrateur\mysql.csv \\192.168.0.1\home\test\mysql.txt

and nothing work the message error indicated that the network way is not found ....

Have you a idéee from what that comming ?

Thanks

September 8, 2014 at 1:52 pm

The Copy-Item does not work out of the box with Linux machines. The Linux machine would need to have the Samba server installed and configured correctly to allow the access via UNC path (\\192.168.0.1\home\test).

You were on the right track with the SSH-Sessions module. It would be better if you would continue with getting the SSH-Sessions PowerShell module work for you.

Cheers
Daniel

September 9, 2014 at 10:59 am

I will try to install winscp.exe then...

Thanks for the help !