![](/img/trans.png)
[英]PowerShell - Copy files to all folders matching a certain pattern
[英]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
info
包含两个文件 -> logger.jsx
和date.js
。另一个是远程目标目录-> /mnt/media/Two 。 它目前是空的。 它的用户和主机是: $userAndHost = "user@foo.bar"
我想将所有扩展名.jsx
和.js
的包和文件从 package 一复制到 package 二。 这里需要使用 scp 因为这是两个不同平台之间的副本。
我尝试了什么:
Get-ChildItem -Path "C:\Users\One" -Recurse
.jsx
和.js
: Get-ChildItem -Path "C:\Users\One" -Recurse | Where-Object {$_.extension -in ".js",".jsx"}
请帮我完成剧本。
嗨,我想你需要这样的东西。
我为你写了一个代码,测试工作。
#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.