繁体   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