简体   繁体   中英

Powershell copy all folders and files with certain extension

I have one package on my Windows machine and another package on a remote server.

The first one is -> C:\Users\One . It contains the following files:

  • adapter.jsx
  • result.js
  • system.jsx
  • moment.js
  • readme.txt
  • package called info that contains two files -> logger.jsx and date.js .

Another one is a remote target directory -> /mnt/media/Two . It is currently empty. The user and host for it are: $userAndHost = "user@foo.bar"

I want to copy all the packages and files of extensions .jsx and .js from package One to package Two. It's required to use scp here since this is a copy between two different platforms .

What I tried:

  1. get all the items within the package:

Get-ChildItem -Path "C:\Users\One" -Recurse

  1. filter items by certain extension, in my case they are .jsx and .js :

Get-ChildItem -Path "C:\Users\One" -Recurse | Where-Object {$_.extension -in ".js",".jsx"}

  1. do the secure copy (scp) - I didn't come up with the script here.

Please, help me finish the script.

Hi i think you need something like this.
I write a code for you, tested working.

#Set execution policy to bypass
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force

#install Posh-SSH from powershell gallery
#https://www.powershellgallery.com/packages/Posh-SSH/2.0.2
Install-Module -Name Posh-SSH -RequiredVersion 2.0.2 

#import module
Import-Module Posh-SSH

#get all the items within the package in the path:
$path = 'C:\Users\One'
$items = (Get-ChildItem -Path $path -Name -File -Include ( '*.jsx', '*.js') -Recurse)

#Need destination credential
$credential = Get-Credential

#copy selected items to destination via scp
$items | ForEach-Object {
    Set-SCPFile -ComputerName 'SCP-SERVER-HOST-HERE' -Credential $credential -RemotePath '/mnt/media/Two' -LocalFile "$path\$_"
}

Hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM