简体   繁体   English

Powershell复制,保留文件夹结构

[英]Powershell copy, retain folder structure

Im trying to achieve the following, get computers name from a txt file, search for particulat file extension, copy the files along with folder structure and in destination create folder based on the computer name and paste there, please see my powershell commands below, when im trying with single computer it works fine, but when adding mutiple computers im getting error. 我正在尝试实现以下目标,从txt文件中获取计算机名称,搜索特定的文件扩展名,将文件与文件夹结构一起复制,并根据计算机名称在目标位置创建文件夹并将其粘贴到此处,请在下面查看我的powershell命令我尝试与单台计算机,它工作正常,但当添加多台计算机时,我得到错误。

Please tell me what are the modifications required? 请告诉我需要什么修改? Thanks in advance 提前致谢

$computername = Get-Content -Path "C:\Test\computers.txt"
$src = "\\$ComputerName\c$\test"
  Get-ChildItem $src -Recurse *.opt |  foreach {
  $split = $_.Fullname  -split '\\'
  $DestFile =  $split[1..($split.Length - 1)] -join '\' 
  $DestFile =  "C:\Test1\$DestFile"
  $null = New-Item -Path  $DestFile -Type File -Force
  Copy-Item -Path  $_.FullName -Destination $DestFile -Force
}

As per my comment, iterate the lines read from computers.txt: 根据我的评论,迭代从computers.txt中读取的行:

## Q:\Test\2018\07\28\SO_51572359.ps1

$computers = Get-Content -Path "C:\Test\computers.txt"

ForEach($ComputerName in $computers){
    $src = "\\$ComputerName\c$\test"
    Get-ChildItem $src -Recurse *.opt | ForEach-Object {
        $split = $_.Fullname  -split '\\'
        $DestFile =  $split[1..($split.Length - 1)] -join '\' 
        $DestFile =  "C:\Test1\$DestFile"
        $null = New-Item -Path  $DestFile -Type File -Force
        Copy-Item -Path  $_.FullName -Destination $DestFile -Force
    }
}

Sample tree after running script: 运行脚本后的示例树:

> tree c:\Test1 /F
Folder PATH listing for volume ThisPC
Volume serial number is 0000xxxx xxxx:xxxx
C:\TEST1
└───RemotePC
    └───c$
        └───test
                Test.opt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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