-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemWatcher.ps1
More file actions
executable file
·142 lines (120 loc) · 13.6 KB
/
FileSystemWatcher.ps1
File metadata and controls
executable file
·142 lines (120 loc) · 13.6 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#By BigTeddy 05 September 2011
#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
#The advantage of this method over using WMI eventing is that this can monitor sub-folders.
#The -Action parameter can contain any valid Powershell commands. I have just included two for example.
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
#You need not subscribe to all three types of event. All three are shown for example.
# Version 1.1
# rutes on estan els fitxers que llisten les aplicacions a monitoritzar
$monitor_file_preprod='F:\Programas\packetizer\fitxers_auxiliars\monitor_preprod.txt'
$monitor_file_prod='F:\Programas\packetizer\fitxers_auxiliars\monitor_prod.txt'
$monitor_file_referencia='F:\Programas\packetizer\fitxers_auxiliars\monitor_referencia.txt'
$monitor_file_funcadd_proves='F:\Programas\packetizer\fitxers_auxiliars\monitor_funcadd_proves.txt'
$monitor_file_funcadd_prod='F:\Programas\packetizer\fitxers_auxiliars\monitor_funcadd_prod.txt'
function monitoritza_dir
(
[string]$entorn,
[string]$aplicacio,
[string]$ordre
)
{
#echo "entorn: $entorn"
#echo "ordre: $ordre"
# Enter the root path you want to monitor.
if ($entorn -eq 'preprod') {
$folder = "F:\DeploymentSharePreProd\Applications\$aplicacio"
$flagfile = "F:\Programas\packetizer\builds_preprod\$aplicacio.flg"
}
Elseif ($entorn -eq 'prod')
{
$folder = "F:\DeploymentShare\Applications\$aplicacio"
$flagfile = "F:\Programas\packetizer\builds_prod\$aplicacio.flg"
}
Elseif ($entorn -eq 'referencia')
{
$folder = "F:\DeploymentSharePreProd\Applications\$aplicacio"
$flagfile = "F:\Programas\packetizer\builds_referencia\$aplicacio.flg"
}
Elseif ($entorn -eq 'funcadd_proves')
{
$folder = "F:\Programas\packetizer\funcionalitats_addicionals\proves\$aplicacio"
$flagfile = "F:\Programas\packetizer\builds_funcadd_proves\$aplicacio.flg"
}
Elseif ($entorn -eq 'funcadd_prod')
{
$folder = "F:\Programas\packetizer\funcionalitats_addicionals\prod\$aplicacio"
$flagfile = "F:\Programas\packetizer\builds_funcadd_prod\$aplicacio.flg"
}
else
{
echo "El parametre entorn ha de valdre referencia, preprod, prod, funcadd_proves o funcadd_prod"
exit
}
if (($ordre -ne 'subscribe') -and ($ordre -ne 'unsubscribe')) {
echo "El ordre ha de valdre subscribe o unsubscribe"
exit
}
# echo "folder: $folder"
# echo "flagfile: $flagfile"
#exit
if (-Not (Test-Path $folder)) {
echo "El path a l'aplicació no existeix: " $folder
exit
}
if ($ordre -eq 'subscribe') {
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# Here, all four events are registerd. You need only subscribe to events that you need:
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated$entorn$aplicacio -MessageData $flagfile -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Out-File -FilePath $event.MessageData -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted$entorn$aplicacio -MessageData $flagfile -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore red
Out-File -FilePath $event.MessageData -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged$entorn$aplicacio -MessageData $flagfile -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
Out-File -FilePath $event.MessageData -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
Register-ObjectEvent $fsw Renamed -SourceIdentifier FileRenamed$entorn$aplicacio -MessageData $flagfile -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore yellow
Out-File -FilePath $event.MessageData -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
echo "Agregada monitoritzacio de: $folder"
echo "en: $flagfile"
}
else
{
# To stop the monitoring, run the following commands:
Unregister-Event FileDeleted$entorn$aplicacio
Unregister-Event FileCreated$entorn$aplicacio
Unregister-Event FileChanged$entorn$aplicacio
Unregister-Event FileRenamed$entorn$aplicacio
echo "Eliminada monitoritzacio de: $folder"
}
}
# agrega (o lleva) les monitoritzacions de preprod
Get-Content $monitor_file_preprod | Foreach-Object {monitoritza_dir 'preprod' $_ 'subscribe'}
#Get-Content $monitor_file_preprod | Foreach-Object {monitoritza_dir 'preprod' $_ 'unsubscribe'}
# agrega (o lleva) les monitoritzacions de prod
Get-Content $monitor_file_prod | Foreach-Object {monitoritza_dir 'prod' $_ 'subscribe'}
#Get-Content $monitor_file_prod | Foreach-Object {monitoritza_dir 'prod' $_ 'unsubscribe'}
# agrega (o lleva) les monitoritzacions de referencia
Get-Content $monitor_file_referencia | Foreach-Object {monitoritza_dir 'referencia' $_ 'subscribe'}
#Get-Content $monitor_file_referencia | Foreach-Object {monitoritza_dir 'referencia' $_ 'unsubscribe'}
# agrega (o lleva) les monitoritzacions de funcionalitats addicionals de proves
Get-Content $monitor_file_funcadd_proves | Foreach-Object {monitoritza_dir 'funcadd_proves' $_ 'subscribe'}
#Get-Content $monitor_file_funcadd_proves | Foreach-Object {monitoritza_dir 'funcadd_proves' $_ 'unsubscribe'}
# agrega (o lleva) les monitoritzacions de funcionalitats addicionals de prod
Get-Content $monitor_file_funcadd_prod | Foreach-Object {monitoritza_dir 'funcadd_prod' $_ 'subscribe'}
#Get-Content $monitor_file_funcadd_prod | Foreach-Object {monitoritza_dir 'funcadd_prod' $_ 'unsubscribe'}