【发布时间】:2018-07-28 23:48:12
【问题描述】:
我试图实现以下目标,从 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
}
【问题讨论】:
-
你遇到了什么错误?
-
$computername是一个字符串数组,你也必须迭代。 -
您正在寻找
robocopy。除非有充分的理由,否则不要尝试在 PowerShell 中重新发明它。
标签: powershell