Powershell复制,保留文件夹结构

时间:2018-07-28 14:38:07

标签: powershell

我正在尝试实现以下目标,从txt文件获取计算机名称,搜索特定的文件扩展名,将文件与文件夹结构一起复制,并根据计算机名称在目标位置创建文件夹并将其粘贴在此处,请参阅我的powershell命令下面,当我尝试使用单台计算机时,它可以正常工作,但添加多台计算机时,我却报错。

请告诉我需要进行哪些修改?预先感谢

$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
}

1 个答案:

答案 0 :(得分:0)

根据我的评论,迭代从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
    }
}

运行脚本后的样本树:

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