-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-dog.ps1
140 lines (124 loc) · 4.16 KB
/
watch-dog.ps1
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
<#
.SYNOPSIS
Name: watch-dog.ps1
The purpose of this script is to genereate hashes of the files in a directory and its sub directory to check if they have been changed.
.DESCRIPTION
This script is for Windows environment where you want to check if the files have been changed. It can be run with task scheduler at a
regular interval and will report which files have been edited or added.
.PARAMETER InitialDirectory
$directoryPath is the directory where you check the files
$wokringdir is where the files with the hashes will be put. Default c:\watchdog\
.PARAMETER Add
A switch parameter that will cause the example function to ADD content.
Add or remove PARAMETERs as required.
.NOTES
Updated: 09.02.2019 - First script
Release Date: 9th of February 2019
Author: Kjetil Grun
.EXAMPLE
Watch-dog.ps1 -directoryPath "C:\inetpub\nasa.gov\" -customer "NASA" -workingdir "f:\watchdog-hashes\" -exclude "*.png,*.jpg,*.pdf"
#requires -version 4
#>
param (
# Location of the files to be checked
[Parameter(Mandatory=$true)]
[string] $directoryPath,
# Customername
[Parameter(Mandatory=$true)]
[string] $customer,
# Full path to working dir. C:\watchdog\ if nothing is specified
[Parameter(Mandatory=$false)]
[AllowNull()]
[string] $workingdir,
# Full or partly name of the path to exclude.
[Parameter(Mandatory=$false)]
[array] $exclude
)
function set-variables {
if ($workingdir.Length -eq 0) {
$workingdir = "c:\watchdog\"
}
if (!(Test-Path $workingdir)) {
mkdir $workingdir
}
$newHashFile = "$workingdir\$customer-new.txt"
$oldHashFile = "$workingdir\$customer-old.txt"
$messageFile = "$workingdir\$customer.message"
# Checking if this is first run.
if ((Test-path $oldHashFile) -eq $true) {
$firstRun = $false
}
else {
$firstRun = $true
}
$hostname = hostname
New-item $messageFile -force
new-item $newHashFile -force
start-hashing
}
function start-hashing {
foreach ($file in (Get-ChildItem -path $directoryPath -recurse -file -Exclude $exclude)) {
$filehash = Get-Filehash $file.fullname
add-content -path $newHashFile -value $filehash.hash -nonewline
add-content -path $newHashFile -value "," -nonewline
add-content -path $newHashFile -value $filehash.path
}
compare-hashfiles
}
function compare-hashfiles {
if (!(Test-path $oldHashFile)) {
Write-Output "First run. No old hash to compare with"
}
else {
$difference = (Compare-Object (Get-Content $newHashFile) (Get-Content $oldHashFile))
write-host $difference
if ($difference -ne $null) {
$changedFiles = New-Object System.Collections.ArrayList
$newFiles = New-Object System.Collections.ArrayList
$difference | foreach-object {
if ($_.SideIndicator -like "<=") {
# Extracting the path from the Compare-Object result
$newFiles.Add(($_.InputObject -split(","))[1]) > $null
}
elseif ($_.SideIndicator -like "=>") {
# Extracting the path from the Compare-Object result
$changedFiles.Add(($_.InputObject -split(","))[1]) > $null
}
}
}
else {
Write-host "No Changes"
}
}
write-result
}
function write-result {
if ($changedFiles.count -gt 0) {
Add-Content -Path $messageFile -Value "Files Changed for $customer on folder $directoryPath on host $hostname"
$changedFiles | ForEach-Object {
Add-Content -Path $messageFile -Value $_
}
}
if ($newFiles.count -gt 0) {
Add-Content -Path $messageFile -Value "Files New for $customer on folder $directoryPath on host $hostname"
$newFiles | ForEach-Object {
Add-Content -Path $messageFile -Value $_
}
}
if ($newFiles.Count -eq 0 -and $changedFiles.Count -eq 0 -and $firstRun -eq $false) {
Add-Content -Path $messageFile -Value "No new files for $customer on folder $directoryPath on host $hostname."
}
elseif ($newFiles.Count -eq 0 -and $changedFiles.Count -eq 0 -and $firstRun -eq $false) {
Add-Content -Path $messageFile -Value "First run for $customer on folder $directoryPath on host $hostname."
}
foreach ($line in (get-content $messageFile)) {
# insert preferred channel for messaging; email, slack, SMS, push....
Write-Host $line
}
stop-script
}
function stop-script {
#Set-location -path c:\scripts
Move-item $newHashFile $oldHashFile -force
}
set-variables