तो मेरे पास एक पावरहेल स्क्रिप्ट है, जो मुख्य रूप से एक समान प्रश्न यहां से ली गई है, जो यह पता लगा सकती है कि किसी निश्चित प्रकार की फ़ाइल को किसी निश्चित नेटवर्क में कब जोड़ा जाता है और ऐसा होने पर ईमेल भेजता है:

Function Watcher{

param ($folder, $filter, $to, $Subject)



$watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true
    EnableRaisingEvents = $true
}

$changeAction = [scriptblock]::Create('

        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"

        $Body = "The file $name was $changeType at $timeStamp"

        Email $to $Subject $Body

 ')

    Register-ObjectEvent $Watcher -EventName "Created" -Action $changeAction


}

हालांकि, मैं इसे इस तरह संशोधित करना चाहता हूं कि यह इस एप्लिकेशन के लिए उपयोगी हो सकता है: अभी नेटवर्क में डेटा (.datx फाइलें) जोड़ने वाला एक टूल है (प्रति मिनट कई फाइलें) और मैं उस पल ईमेल अधिसूचना प्राप्त करना चाहता हूं डाटा रिकॉर्ड किया जा रहा है। मैं इसे सबसे आसानी से कैसे संशोधित कर सकता हूं कि जब प्रारंभिक द्रष्टा चालू हो जाता है, तो यह देखने के लिए प्रतीक्षा करता है कि क्या यह फिर से होता है और यदि ऐसा है तो रीसेट करता है, लेकिन फिर जारी रहता है? या एक पूरी नई स्क्रिप्ट बनाना सबसे अच्छा होगा? मूल रूप से, मैं नेटवर्क पर अपलोड की जा रही एक अकेली .datx फ़ाइल द्वारा द्रष्टा को सक्रिय कैसे कर सकता हूं, लेकिन यह उनमें से एक धारा द्वारा ट्रिगर नहीं किया गया है (पिछले एक को छोड़कर)

1
Andy 28 जिंदा 2020, 02:48

1 उत्तर

सबसे बढ़िया उत्तर

आप निम्न बैचिंग दृष्टिकोण का उपयोग कर सकते हैं:

try {

  # Specify the target folder: the system's temp folder in this example.
  $dir = (Get-Item -EA Ignore temp:).FullName; if (-not $dir) { $dir = $env:TEMP }

  # Create and initialize the watcher.
  # Note the [ordered] to ensure that .EnableRaisingEvents is set last.
  $watcher = [System.IO.FileSystemWatcher] [ordered] @{
    Filter              = '*.datx'
    Path                = $dir
    EnableRaisingEvents = $true
  }

  # To simulate file creation, create *.datx files in the folder printed
  # mentioned in the following status message.
  Write-Host "Watching $dir for creation of $($watcher.Filter) files..."

  # Register for (subscribe to) creation events:
  # Determine a unique event-source ID...
  [string] $sourceId = New-Guid
  # ... and register for the watcher's Created event with it.
  Register-ObjectEvent $watcher -EventName Created -SourceIdentifier $sourceId

  # Initialize the ordered hashtable that collects all file names for a single 
  # batch.
  # Note: Since any given file creation can trigger *multiple* events, we 
  #       use an ordered hashtable (dictionary) to ensure that each file is 
  #       only reported once.
  #       However, *across batches* duplicates can still occur - see below.
  $batch = [ordered] @{}

  # Determine the sliding time window during which newly created files are 
  # considered part of a single batch.
  # That is, once a file has been created, each additional file created
  # within that time window relative to previous file becomes part of the
  # same batch.
  # When a time window elapses without a new file having been created, the
  # batch is considered complete and processed - see max. batch-size exception
  # below.
  # IMPORTANT:
  #  * The granularity is *seconds*, so the time window must be at least 1 sec.
  #  * Seemingly independently of the length of this window, duplicate events
  #    are likely to occur across batches the less time has elapsed between
  #    the end of a batch and the start of a new one - see below.
  $batchTimeWindowSecs = 5

  # How many names to allow a batch to contain at most, even if more
  # files keep getting created in the sliding time window.
  $maxBatchSize = 100

  while ($true) {
    # Run indefinitely; use Ctrl-C to exit.

    # Wait for events in a sliding time window of $batchTimeWindowSecs length.
    # Note: Using Wait-Event in a loop (1 event per iteration) is *more* 
    #       predictable than the multi-event collecting Get-Event in terms of
    #       avoiding duplicates, but duplicates do still occur.
    $batch.Clear()
    while ($evt = Wait-Event -SourceIdentifier $sourceId -Timeout $batchTimeWindowSecs) {
      $evt | Remove-Event  # By default, events linger in the queue; they must be removed manually.
      # Add the new file's name to the batch (unless already present)
      # IMPORTANT:
      #  * Duplicates can occur both in a single batch and across batches.
      #  * To truly weed out all duplicates, you'd have to create a session-level
      #    dictionary of the files' names and their creation timestamps.
      #    With high-volume file creation, this session-level dictionary could
      #    grow large; periodic removal of obsolete entries would help.
      $batch[$evt.SourceArgs.Name] = $null # dummy value; it is the *keys* that matter.
      Write-Host ✔ -NoNewline # status output: signal that a new file was created
      # If the max. batch size has been reached, submit the batch now, even if further
      # events are pending within the timeout window.
      if ($batch.Count -ge $maxBatchSize) { 
        Write-Warning "Max. batch size of $maxBatchSize reached; force-submitting batch."
        break
      }
    }

    # Completed batch available?
    if ($batch.Count) {      
      # Simulate processing the batch.
      Write-Host "`nBatch complete: Sending email for the following $($batch.Count) files:`n$($batch.Keys -join "`n")"                                            #`
      # Start a new batch.
      $batch.Clear()
    }
    else {
      Write-Host . -NoNewline # status output: signal that no new files were created in the most recent time window.
    }

  }

}
finally {
  # Clean up:
  # Unregister the event subscription.
  Unregister-Event -SourceIdentifier $sourceId
  # Dispose of the watcher.
  $watcher.Dispose() 
}

पहले 3 फाइलों का एक बैच बनाने से नमूना आउटपुट, फिर दूसरा 5 के साथ:

Watching C:\Users\jdoe\AppData\Local\Temp for creation of *.datx files...
............✔✔✔
Batch complete: Sending email for the following 3 files:
1.datx
2.datx
3.datx
.✔✔✔✔✔
Batch complete: Sending email for the following 5 files:
4.datx
5.datx
6.datx
7.datx
8.datx
....................
1
mklement0 29 जिंदा 2020, 07:01