Skip to content

Instantly share code, notes, and snippets.

@jmreicha
Created January 17, 2017 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmreicha/2051719a201a2fb6a7c4c92d8e0f664b to your computer and use it in GitHub Desktop.
Save jmreicha/2051719a201a2fb6a7c4c92d8e0f664b to your computer and use it in GitHub Desktop.
File watcher report
$csv = "c:\Users\$env:USERNAME\report.csv"
$html = "c:\Users\$env:USERNAME\report.html"
$logfile = "c:\Users\$env:USERNAME\logfile.txt"
Function createReports() {
# Bail if the logfile doesn't exist
if (!(Test-Path "$logfile")) {
Write-Host -fore red "$logfile doesn't exist, quitting..."
exit
}
# Create the CSV report if it doesn't exist
if (!(Test-Path "$csv")) {
New-Item -path "$csv" -type file | Out-Null
}
# Blow out the old report and make a new one
else {
#Write-Host -fore yellow "$csv exists, recreating"
Remove-Item "$csv"
New-Item -path "$csv" -type file | Out-Null
}
# Create headers for CSV report
$inputcsv = Import-Csv "$logfile" -Header Filename,ChangeType,Date
# Populate the report with data from the text file
$inputcsv | Export-Csv "$csv" -NoType -Append
# Create the HTML report if it doesn't exist
if (!(Test-Path "$html")) {
New-Item -path "$html" -type file | Out-Null
}
# Blow out the old report and make a new one
else {
#Write-Host -fore yellow "$html exists, recreating"
Remove-Item "$html"
New-Item -path "$html" -type file | Out-Null
}
# Convert the CSV report into HTML
Get-Content "$csv" | ConvertFrom-Csv | ConvertTo-Html | Out-File "$html"
}
<#
Function emailReports() {
# Set up the email
$From = "<from-email>"
$To = "<to-email>"
$Credential = "<sending-email-creds>"
$Attachment = $csv, $html
$Subject = "File watcher daily report"
$Body = "File watcher report for $(Get-Date)."
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
# Send the message
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort `
-Credential $Credential -UseSsl -Attachments $Attachment
}
#>
createReports
#emailReports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment