1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
$boutonLancer.add_Click(
{
$boutonViderTable.Enabled = 1
$folder = "C:\"
# Création d'un watcher qui surveillera le système de fichier
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $folder
$watcher.IncludeSubdirectories = $true # Surveillance des répertoire fils
$watcher.EnableRaisingEvents = $true
# Option de notification (que surveiller?)
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
[System.IO.NotifyFilters]::LastAccess
[System.IO.NotifyFilters]::DirectoryName
[System.IO.NotifyFilters]::FileName
[System.IO.NotifyFilters]::Attributes
# Boucle de surveillance
while(1)
{
# Dans l'attente d'un changement
$resultat = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bOr [System.IO.WatcherChangeTypes]::Created -bor System.IO.WatcherChangeTypes]::Deleted -bOr [System.IO.WatcherChangeTypes]::renamed);
if($resultat.TimedOut){
continue
}
$horodatage= Date -format G
if ($resultat.ChangeType -eq "Renamed" ) {
Write-Host $($horodatage + " Renommage: "+ " 'C:\" + $resultat.OldName + "' =====> 'C:\" + $resultat.Name +"' " )
$dw.Rows.Add($horodatage, "Renommage", "'C:\" + $resultat.OldName + "' =====> 'C:\" + $resultat.Name +"' ")
}
elseif ($resultat.ChangeType -eq "Created"){
Write-Host $($horodatage + " Création: "+ " 'C:\" + $resultat.Name +"'")
$dw.Rows.Add($horodatage, "Création", "'C:\" + $resultat.Name +"'")
}
elseif ($resultat.ChangeType -eq "Changed"){
Write-Host $($horodatage + " Modification: "+ " 'C:\" + $resultat.Name + "'" )
$dw.Rows.Add($horodatage, "Modification", "'C:\" + $resultat.Name +"'")
}
elseif ($resultat.ChangeType -eq "Deleted"){
Write-Host $($horodatage + " Suppression: "+ " 'C:\" + $resultat.Name + "'")
$dw.Rows.Add($horodatage, "Suppression", "'C:\" + $resultat.Name +"'")
}
elseif ($resultat.ChangeType -eq "Error"){
Write-Host $($horodatage + " Erreur de FileSystemWatcher")
}
$dw.Refresh()
$dw.Update()
}
}
) |
Partager