Monitor new files

Rédigé par Sozezzo - - Aucun commentaire

It just monitor a folder for new files with Powershell.


#My function
function Do-MonitorNewFile-TODO($Event_name, $Event_FullPath, $Event_changeType, $Event_TimeGenerated)
{
    Write-Host "The file '$Event_name' was $Event_changeType at $Event_TimeGenerated" -ForegroundColor red
}

function Do-MonitorNewFile($Sourcepath, $MonitorName)
{
    # Remove previous event
    Unregister-Event -SourceIdentifier "$MonitorName" -ErrorAction SilentlyContinue
    Remove-Event     -SourceIdentifier "$MonitorName" -ErrorAction SilentlyContinue

    # Watcher
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.IncludeSubdirectories = 1
    $watcher.Path = $Sourcepath
    $watcher.EnableRaisingEvents = 1

    #action
    $action = {
        $Event_name          = $Event.SourceEventArgs.Name
        $Event_FullPath      = $Event.SourceEventArgs.FullPath
        $Event_changeType    = $Event.SourceEventArgs.ChangeType
        $Event_TimeGenerated = $Event.TimeGenerated

        # Call my function
        Do-MonitorNewFile-TODO "$Event_name" "$Event_FullPath" "$Event_changeType" "$Event_TimeGenerated"

    }

    $oEvent = Register-ObjectEvent $watcher Created -Action $action -SourceIdentifier "$MonitorName"

}

# Call - monitor a folder
Do-MonitorNewFile "C:\TEMP\" "MyMonitorName"

Les commentaires sont fermés.