Register-ObjectEvent only working after multiple re-runs

Welcome Forums General PowerShell Q&A Register-ObjectEvent only working after multiple re-runs

Viewing 3 reply threads
  • Author
    Posts
    • #241061
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      Hello,

      I want to monitor a specific folder for new created “Exchange Customer.csv”.

      The monitoring works fine and in the console he writes “some text” BUT no Exchange2.csv file is being created.

      If I start my script again, it writes 2 or 3 times “some text” in the console.

      After having started the script for +- 4 times, it writes 6 times “some text” in the console AND creates the file Exchange2.csv.

      Then it’s working all the time until I’m stopping it using:

      Get-EventSubscriber | Unregister-Event

      What am I doing wrong? 🙂

      Here this part of the scipt

      #watch folder and execute if new file has been added
      $folder = $path
      $filter = 'Exchange Customer.csv'
      $watcher = New-Object IO.FileSystemWatcher $folder, $filter

      $action =

      {

      #Get Content, remove first 14lines 

      Get-Content $file| Select-Object -Skip 14 | Set-Content "$path\Exchange2.csv"

      Write-Host "some text"

      }

      Register-ObjectEvent $watcher 'Created' -Action $action

       

      Thank you

      Regards

    • #241067
      Participant
      Topics: 9
      Replies: 701
      Points: 2,814
      Helping Hand
      Rank: Community Hero

      What is $file?

    • #241073
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      $path = "P:\Testing"
      $filename = 'Exchange Customer.csv'
      $file="$path\$filename"

    • #241460
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      Here a new test:

      [/crayon]

      After running this scipt, a new .csv file is created, but only 3 times. Afterwards is just writes “text” to the console, but “Get-Content “P:\Testing\Exchange Customer.csv” | Select-Object -Skip 14 | Set-Content “P:\Testing\Exchange2.csv”” isn’t executed …

      • This reply was modified 6 months, 1 week ago by jester71.
Viewing 3 reply threads
  • The topic ‘Register-ObjectEvent only working after multiple re-runs’ is closed to new replies.