简体   繁体   English

简化Powershell搜索脚本

[英]Streamline Powershell Searching Script

I have written the following script out to search 9 directories across my network. 我已写出以下脚本来搜索网络中的9个目录。 It functions as needed but is ungodly slow (i assume due to the amount of places its going). 它可以根据需要运行,但速度非常慢(我认为由于运行的地方数量而异)。 My question to you, as i am not very experienced with powershell, is there any way that something could be "streamlined" to speed up the end result? 我对您的问题是,因为我对Powershell的经验不是很丰富,是否可以“简化”某些方法以加快最终结果? It seems pretty messy having all these sources. 具有所有这些来源似乎很混乱。

Any help would be greatly appreciated. 任何帮助将不胜感激。

$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

You can use Start-Job to spin up separate background processes to carry out the job against each $source : 您可以使用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 will now contain the relevant files from all the locations in $sources $Files现在将包含$sources所有位置的相关文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM