ASKER
foreach ($row in $dt1.Rows)
{
$sqlRow = $dt2.Select("ID="+$row.ID)
if ($sqlRow.Length -ne 0)
{
Write-Host "Update action."
}
else
{
Write-Host "Insert action."
}
}
foreach ($row in $dt2.Rows)
{
$compareRow = $dt1.Select("ID="+$row.ID)
if ($compareRow.Length -eq 0)
{
Write-Host "Remove Action."
}
}
ASKER
ASKER
$smtpParam = @{
SmtpServer = 'mail.domain.com'
from = 'me@domain.com'
to = 'you@domain.com'
}
foreach ($folder in 'C:\Monitoring\Folder1', 'C:\Monitoring\Folder2')
{
$del = $new = $chg = $null
$snapshot_file = 'C:\Monitoring\'+(split-path $folder -leaf)+'-files.txt'
$snapshot_new = Get-ChildItem -recurse $folder -Name
if (test-path $snapshot_file)
{
$snapshot_old = Get-Content $snapshot_file
$cmp = compare-object $snapshot_old $snapshot_new
$new = ($cmp | ? { $_.SideIndicator -eq '=>' }).InputObject
$del = ($cmp | ? { $_.SideIndicator -eq '<=' }).InputObject
$snapshot_date = (Get-ChildItem $snapshot_file).LastWriteTime
$chg = Get-ChildItem -recurse $folder | ? { $_.LastWriteTime -gt $snapshot_date } | Select -Expand Name
}
if ($new) { Send-MailMessage @smtpParam -Subject "$folder - new files" -Body ($new -join "`r`n") }
if ($del) { Send-MailMessage @smtpParam -Subject "$folder - deleted files" -Body ($del -join "`r`n") }
if ($chg) { Send-MailMessage @smtpParam -Subject "$folder - changed files" -Body ($chg -join "`r`n") }
$snapshot_new | Out-File $snapshot_file
}
ASKER
$smtpParam = @{
SmtpServer = 'mail.domain.com'
credential = New-Object System.Management.Automation.PsCredential('MyUser', (ConvertTo-SecureString 'MyPwd' -AsPlainText –force))
Port = 65432
from = 'me@domain.com'
to = 'you@domain.com'
}
You'll have to replace 'MyUser' and 'MyPwd', and of course the port number, by the real-life data.
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.
TRUSTED BY
Option 1 is to record all the files in the folder into a CSV or equivalent and then the scheduled task will compare what changed and send out an email. This doesn't require a running service, etc.
The other option is to create a file system watcher in a dummy service and have that record the changes.
Do you need to see every time the files change or just a list of new, deleted, changed files?