简化Powershell搜索脚本

时间:2018-05-01 23:46:56

标签: powershell search scripting

我已经编写了以下脚本来搜索我网络中的9个目录。它根据需要起作用但是速度很慢(我假设它的位置很多)。我对你的问题,因为我对powershell不是很有经验,有什么方法可以“精简”某些东西来加速最终结果?拥有所有这些来源似乎非常混乱。

非常感谢任何帮助。

$source1 = "\\Server_IP\d$\Folder\Err"
$source2 = "\\Server_IP\d$\Folder\Err"
$source3 = "\\Server_IP\d$\Folder\Err"
$source4 = "\\Server_IP\d$\Folder\Err"
$source5 = "\\Server_IP\d$\Folder\Err"
$source6 = "\\Server_IP\d$\Folder\Err"
$source7 = "\\Server_IP\d$\Folder\Err"
$source8 = "\\Server_IP\d$\Folder\Err"
$source9 = "\\Server_IP\d$\Folder\Err"

Get-Childitem –Path $source, $Source2, $Source3, $Source4, $Source5, $Source6 , $Source7, $Source8, $Source9 -Include *SEARCHITEM* -Recurse -ErrorAction SilentlyContinue

1 个答案:

答案 0 :(得分:1)

您可以使用Start-Job启动单独的后台进程来执行针对每个$source的工作:

$Sources = @(
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
    "\\Server_IP\d$\Folder\Err"
)
$Files = $Sources |ForEach-Object {
  Start-Job -ScriptBlock { 
    $args |Get-ChildItem -Filter *SEARCHITEM* -Recurse -ErrorAction SilentlyContinue 
  } -ArgumentList $_.FullName
} |Wait-Job |Receive-Job

$Files现在将包含$sources

中所有位置的相关文件