Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

windows - Powershell Get-ChildItem seems to sometimes skip files

I wrote a powershell script to upload files that are created in a local folder to SharePoint with Add-PnPFile.

I loop the script every 20 seconds to check the folder for new files and then upload if the file is not on SharePoint yet. I use a Where-object to select only the last 5 minutes to not have to much files in the loop. (it needs to upload fast since flow is ERP->Local folder-> SharePoint and users are waiting)

This works like 95% of the time but sometimes it skips a files that is existing.

$Localfolder = "D:DATA"
$A_Files = Get-ChildItem -Path $Localfolder -Include @("*.pdf", "*.csv") | Where-Object {$_.LastWriteTime -gt (Get-Date).AddMinutes(-5)}

If I stop the script and run in the same Powershell session, it doesn't show the file. If I open another powershell session and run the command it shows the file.

Anyone know if there is caching or I'm missing something?

PS C:Users ob_admin> $PSVersionTable

Name Value


PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

question from:https://stackoverflow.com/questions/65937915/powershell-get-childitem-seems-to-sometimes-skip-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I start using a different approach which has a 100% success rate after IInspectable comment: $watcher = New-Object System.IO.FileSystemWatcher

So the trigger is now: Register-ObjectEvent $watcher 'Created' -Action $action


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...