if ($FullPath.StartsWith("$BlogRepoPath\.git") -and-not($FullPath.Contains(".github")) ){ <# Action to perform if the condition is true #> return } # node_modules/ # if ($FullPath.StartsWith("$BlogRepoPath\node_modules")){ # <# Action to perform if the condition is true #> # return # } # public/ if ($FullPath.StartsWith("$BlogRepoPath\public")){ <# Action to perform if the condition is true #> return } # db.json if ($FullPath.StartsWith("$BlogRepoPath\db.json")){ <# Action to perform if the condition is true #> return }
# type of change: $ChangeType = $details.ChangeType
# when the change occured: $Timestamp = $event.TimeGenerated
# save information to a global variable for testing purposes # so you can examine it later # MAKE SURE YOU REMOVE THIS IN PRODUCTION! $global:all = $details
# now you can define some action to take based on the # details about the change event:
# let's compose a message: $text = "{0} was {1} at {2}"-f$FullPath, $ChangeType, $Timestamp # Write-Host "" # Write-Host $text -ForegroundColor DarkYellow # Out-File -Append -FilePath $logFile -InputObject "" Out-File-Append-FilePath$logFile-InputObject$text
# you can also execute code based on change type here: switch ($ChangeType) { 'Changed' { "CHANGE" # 修改的时候提交也要等 5 秒 Start-Sleep-Seconds1 } 'Created' { "CREATED" } 'Deleted' { "DELETED" # to illustrate that ALL changes are picked up even if # handling an event takes a lot of time, we artifically # extend the time the handler needs whenever a file is deleted # Write-Host "Deletion Handler Start" -ForegroundColor Gray Out-File-Append-FilePath$logFile-InputObject"Deletion Handler Start" Start-Sleep-Seconds1 # Write-Host "Deletion Handler End" -ForegroundColor Gray Out-File-Append-FilePath$logFile-InputObject"Deletion Handler End" } 'Renamed' { # this executes only when a file was renamed $text = "File {0} was renamed to {1}"-f$OldName, $Name # Write-Host $text -ForegroundColor Yellow Out-File-Append-FilePath$logFile-InputObject$text }
# any unhandled change types surface here: default { # Write-Host $_ -ForegroundColor Red -BackgroundColor White Out-File-Append-FilePath$logFile-InputObject$_ } }
# subscribe your event handler to all event types that are # important to you. Do this as a scriptblock so all returned # event handlers can be easily stored in $handlers: $handlers = . { Register-ObjectEvent-InputObject$watcher-EventName Changed -Action$action Register-ObjectEvent-InputObject$watcher-EventName Created -Action$action Register-ObjectEvent-InputObject$watcher-EventName Deleted -Action$action Register-ObjectEvent-InputObject$watcher-EventName Renamed -Action$action }
# Write-Host "Watching for changes to $BlogRepoPath" # Out-File -Append -FilePath $logFile -InputObject '------------------------------------------------------------------------------------------------------------------------' # Out-File -Append -FilePath $logFile -InputObject "Watching for changes to $BlogRepoPath"
# since the FileSystemWatcher is no longer blocking PowerShell # we need a way to pause PowerShell while being responsive to # incoming events. Use an endless loop to keep PowerShell busy: do { # Wait-Event waits for a second and stays responsive to events # Start-Sleep in contrast would NOT work and ignore incoming events Wait-Event-Timeout60
# write a dot to indicate we are still monitoring: # Write-Host "." -NoNewline # Out-File -Append -FilePath $logFile -InputObject "."
} while ($true) } finally { # this gets executed when user presses CTRL+C: