简体   繁体   中英

Optimization of Get-ChildItem in PowerShell script

I have a Powershell script in which I need to determine whether any files have been added to a folder within the last 60 minutes. If the answer is yes, then I take those files, copy them to another directory, and execute certain code. Right now, the source directory has ~10k-20k files and it is taking a long time for the code to execute. I have tried optimization technique for gci that I've found online (including piping outputs) but none has worked. I also tried executing as a batch file by using c/ dir but I get an error saying it does not support UNC paths. Any ideas?

Stop-Process -name excel
$PathMX1005 = "\\UNCsourcedir"
$numberoffilesMX1005 = Get-ChildItem $PathMX1005 -recurse -include *1Hz_1*.csv | Where { $_.CreationTime -ge [datetime]::Now.AddMinutes(-60) } | Measure-Object
If ($numberoffilesMX1005.Count -eq 0) {Exit-PSSession}
Else {   
$filesMX1005 = Get-ChildItem $PathMX1005 -recurse -include *1Hz_1*.csv | Where { $_.CreationTime -ge [datetime]::Now.AddMinutes(-60) } .... 

rest of code }

The user of -Filter is preferred over -Include as it passes the parameters to the provider instead of having PowerShell parse results after the fact. Also, as Etan Reisner suggested, you should only pull the directory listing once, and then get the count of it for your If statement, and again for processing.

Stop-Process -name excel
$PathMX1005 = "\\UNCsourcedir"
$filesMX1005 = Get-ChildItem $PathMX1005 -recurse -filter "*1Hz_1*.csv" | Where { $_.CreationTime -ge [datetime]::Now.AddMinutes(-60) } 
If ($filesMX1005.Count -eq 0) {Exit-PSSession}
Else {   
    rest of code }

I had a similar problem to what you are having now , I ended up ditching the get child method and instead I created a folder watcher in power shell so basically when ever a file is added to the folder it triggers and event the even checks if the file is then one I am looking for and then performs an action . This was way quicker but it can slow down the file/folder creation time.

I came up with this idea after reading the Scripting Guys blog post so I will just link it hear . If you need any help ask .

http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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