PowerShell - copy files via SSH

Опубликовано: 12 Сентябрь 2021
на канале: Mr Automation
557
11

(How to copy files from Windows to Linux or Unix and visaversa)
I am sorry for the suboptimal audio quality, I am working on it.

In this video I demonstrate how you can copy files from windows to a Unix or Linux server using SSH powershell module. With this module you will be able to automate these kinds of actions.

*how to copy files from windows to linux
*how to copy files from windows to unix
*how to copy files from linux without using winscp
*powershell
*learn powershell
*automation
*learn automation
*windows
*windows powershell
*automatic deployment
*automatic installations
*configuration as code


Code :


#HOWTO SECURE CREDENTIAL INPUT FOR SCRIPTS :    • PowerShell - Secure input  
param(
[Parameter(Mandatory)] [String]$servername,
[Parameter(Mandatory)] [ValidateSet("upload", "download")]$action,
[Parameter(Mandatory)] [String]$localpath,
[Parameter(Mandatory)] [String]$remotepath
)

$creds = Get-Credential
if ($action -eq "upload"){
$SshSession = New-SSHSession -ComputerName $servername -Credential $creds -AcceptKey
Invoke-SSHCommand -SSHSession $SshSession -Command "mkdir $remotepath"
Set-SCPItem -ComputerName $servername -Credential $creds -Path "$localpath" -Destination "/"
}
else{
Get-SCPItem -ComputerName $servername -Credential $creds -Path $remotepath `
-Destination "$localpath" -PathType Directory -AcceptKey:$true
}

Remove-SSHSession $SshSession














Code from demo:

$creds = Get-Credential
Get-SCPItem -ComputerName unix1000.home.local -Credential $creds -Path /tmp -Destination "$env:temp\mark-unix" -PathType Directory -AcceptKey:$true
Get-SCPItem -ComputerName linux1000.home.local -Credential $creds -Path /tmp -Destination "$env:temp\mark-linux" -PathType Directory -AcceptKey:$true

#$creds = Get-Credential
#Get-SCPItem -ComputerName unix2000.home.local -Credential $creds -Path /tmp -Destination "$env:temp\mark-unix" -PathType Directory -AcceptKey:$true
Get-SCPItem -ComputerName linux2000.home.local -Credential $creds -Path /tmp -Destination "$env:temp\mark-linux" -PathType Directory -AcceptKey:$true

$SshSession = New-SSHSession -ComputerName linux2000.home.local -Credential $creds -AcceptKey
Invoke-SSHCommand -SSHSession $SshSession -Command "mkdir /tmp/mark-linux"

Set-SCPItem -ComputerName linux2000.home.local -Credential $creds -Path "$env:temp\mark-linux" -Destination /tmp -Force -acceptkey

$SshSession = New-SSHSession -ComputerName unix2000.home.local -Credential $creds -AcceptKey
Invoke-SSHCommand -SSHSession $SshSession -Command "mkdir /tmp/mark-unix"
Set-SCPItem -ComputerName unix2000.home.local -Credential $creds -Path "$env:temp\mark-unix" -Destination /tmp -Force -acceptkey


Смотрите видео PowerShell - copy files via SSH онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Mr Automation 12 Сентябрь 2021. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 557 раз и оно понравилось 11 посетителям.