这样做非常简单。
首先获取要移动到名为 $files 的数组中的文件列表:
$files=get-childitem "\\oltnas6uk\sd.test.com$\PROJECTS\Account Management\"
然后使用 foreach 循环遍历数组,并在正则表达式模式下使用开关来匹配文件上的模式并确定要做什么:
foreach ($f in $files) {
switch -regex ($f.Name) {
"AMM" {
try {
Move-Item -Path $f.FullName -Destination "\\ammnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
"BMM" {
try {
Move-Item -Path $f.FullName -Destination "\\bmmnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
}
}
}
我建议您在脚本中添加日志记录,或者花足够的时间确保您的模式匹配不会产生意外结果。如果目标中有很多文件,还值得对所有预期的匹配项进行比较,以查看有多少文件与您的任何规则不匹配。