A lot of log files need to be analyzed during an Active Directory migration.
In case there is a log, you mostly have to write an email, update a database or do something like that.
This is a simple example of a PowerShell log file monitoring script for a share:
PowerShell: Log file Supervision for Share
The code checks every 10 sec if there is a new logfile in $share (“\\server\share\”) which starts with $startsWith (“ADMT*”).
The asterisk is a wild-card and stands for any sequence of characters.
$share = “\\server\share\” # Change to your share
$startsWith = “ADMT*”
$logs = Get-ChildItem ($share + $startsWith) | Sort-Object LastWriteTime -Descending # Get all files starting with $startsWith in $share and sort by DateModified. Latest is first.
$lastWriteTime = $logs[0].LastWriteTime # The time stamp of the latest log file
while($true) {
$logs = Get-ChildItem ($share + $startsWith) | Where-Object {($_.LastWriteTime -gt $lastWriteTime)} # Get all files starting with $startsWith in $share that are newer than $lastWriteTime
if($logs -ne $null) { # If we have new logs…
$logs = $logs | Sort-Object LastWriteTime -Descending # Sort logs descending
if($logs.Count -eq $null) { # If we have one log…
$lastWriteTime = $logs.LastWriteTime # Set $lastWriteTime to DateModified of new log
}
else {
$lastWriteTime = $logs[0].LastWriteTime # Otherwise, use DateModified of latest log
}
foreach($logFile in $logs) { # for each log file found do…
# <YOUR CODE HERE>
}
}
else { # If there are no new logs
Write-Host “no new logs found. Sleeping for 10s”
}
Sleep 10 # Sleep for 10s
}
Please contact us if you want to know more about our services.
Leave a Reply
<p>Your email is safe with us.<br/>Information about our <a href="https://activedirectoryfaq.com/contact-us/">data protection policies</a></p>