Powershell复制,保留文件夹结构



我试图实现以下目标,从txt文件中获取计算机名称,搜索特定的文件扩展名,复制文件以及文件夹结构,并在目标中根据计算机名称创建文件夹并粘贴到那里,请参阅下面的powershell命令,当我尝试使用单台计算机时,它可以正常工作,但当添加多台计算机时会出错。

请告诉我需要什么修改?提前感谢

$computername = Get-Content -Path "C:Testcomputers.txt"
$src = "\$ComputerNamec$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
}

根据我的评论,迭代从computers.txt:读取的行

## Q:Test2018728SO_51572359.ps1
$computers = Get-Content -Path "C:Testcomputers.txt"
ForEach($ComputerName in $computers){
$src = "\$ComputerNamec$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

最新更新