簡體   English   中英

Powershell 復制所有具有特定擴展名的文件夾和文件

[英]Powershell copy all folders and files with certain extension

我在我的 Windows 機器上有一個 package,在遠程服務器上有另一個 package。

第一個是-> C:\Users\One 它包含以下文件:

  • adapter.jsx
  • result.js
  • system.jsx
  • moment.js
  • readme.txt
  • package 稱為info包含兩個文件 -> logger.jsxdate.js

另一個是遠程目標目錄-> /mnt/media/Two 它目前是空的。 它的用戶和主機是: $userAndHost = "user@foo.bar"

我想將所有擴展名.jsx.js的包和文件從 package 一復制到 package 二。 這里需要使用 scp 因為這是兩個不同平台之間的副本。

我嘗試了什么:

  1. 獲取 package 中的所有項目:

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

  1. 按特定擴展名過濾項目,在我的情況下它們是.jsx.js

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

  1. 做安全副本(scp)-我沒有在這里想出腳本。

請幫我完成劇本。

嗨,我想你需要這樣的東西。
我為你寫了一個代碼,測試工作。

#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\$_"
}

希望這可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM