locked
Register-ObjectEvent not consistent RRS feed

  • General discussion

  • $PathToMonitor = "P:\Testing"
    
    #    Unregister-Event -SourceIdentifier FSCreate
    #    $handler | Remove-Job
    #    $FileSystemWatcher.EnableRaisingEvents = $false
    #    $FileSystemWatcher.Dispose()
    #    "Event Handler disabled."
    
    explorer $PathToMonitor
    
    $FileSystemWatcher = New-Object System.IO.FileSystemWatcher
    $FileSystemWatcher.Path  = $PathToMonitor
    $FileSystemWatcher.IncludeSubdirectories = $false
    $FileSystemWatcher.Filter= 'Exchange Customer.csv'
    
    $FileSystemWatcher.EnableRaisingEvents = $true
    
    $Action = {
        $details = $event.SourceEventArgs
        $Name = $details.Name
        $FullPath = $details.FullPath
        $OldFullPath = $details.OldFullPath
        $OldName = $details.OldName
        $ChangeType = $details.ChangeType
        $Timestamp = $event.TimeGenerated
        $text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp
        Write-Host ""
        Write-Host $text -ForegroundColor Green
    
        Get-Content "P:\Testing\Exchange Customer.csv" | Select-Object -Skip 14 | Set-Content "P:\Testing\Exchange2.csv"
            
        Write-Host "text"
    
        }
    
    $handler = . {
        
        Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreate
    
    }
    
    Write-Host "Watching for changes to $PathToMonitor"

    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 …

    e.g.

    PS C:\Users\jean-marc.papi> 
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 08:39:10
    text
    
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 08:39:19
    text
    
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 08:39:28
    text
    
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 08:40:10
    text
    
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 11:34:54
    text
    
    P:\Testing\Exchange Customer.csv was Created at 10/07/2020 11:35:05
    text

    8:39:10 ok

    and again 11:34:54

    all the other times no file created.


    • Edited by Jester00 Friday, July 10, 2020 9:37 AM
    • Changed type jrv Thursday, July 23, 2020 5:43 AM abandoned
    • Moved by jrv Thursday, July 23, 2020 5:44 AM abandoned
    Friday, July 10, 2020 9:06 AM

All replies

  • Change to this:

    (Get-Content 'P:\Testing\Exchange Customer.csv') |
    Select-Object -Skip 14 | Set-Content 'P:\Testing\Exchange2.csv'


    \_(ツ)_/

    Friday, July 10, 2020 6:17 PM
  • Hi,
    As this thread has been quiet for a while, so here is a quick question.Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, July 23, 2020 5:34 AM