Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $filePath = "C:\test"
- $logPath = "C:\test\file.csv"
- $log
- #Test if log exist, if exist import
- if (Test-Path -LiteralPath $logPath) {
- $log = Import-Csv -LiteralPath $logPath -Delimiter ';'
- } else {
- $log = $null
- }
- $fileList = [System.Collections.Generic.List[PsObject]]::new()
- $files = Get-ChildItem -LiteralPath $filePath -Recurse -File | where {$_.CreationTime -gt (Get-Date).AddHours(-2)}
- # Check if files are allready logged, else add to log
- foreach ($file in $files) {
- $hash = [ordered]@{
- 'dateScanned' = Get-Date
- 'name' = $file.Name
- 'fullName' = $file.FullName
- 'identifier' = "$($file.CreationTime.ToString("yyyyMMddhhmmss"))$($file.Name.Replace(' ',''))"
- }
- $obj = New-Object -TypeName PsObject -Property $hash
- if ($log.identifier -notcontains $obj.identifier) {
- $fileList.Add($obj)
- }
- }
- # send mail if there are new files, otherwise don't
- if ($fileList.count -ne 0) {
- $body = "Following files older than 2 hours have just been logged to $logPath`n"
- $fileList.fullname | foreach {$body += "`n$_"}
- #Fill in your own mail info
- $mailProperties = [ordered]@{
- From =
- Body = $body
- SmtpServer =
- Port =
- To =
- Subject =
- Encoding =
- }
- Send-MailMessage -BodyAsHtml -UseSsl @mailProperties
- }
- $fileList | Export-Csv -Path $logPath -Encoding UTF8 -Delimiter ';' -NoTypeInformation -Append
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement