Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Last active August 29, 2015 14:06
Show Gist options
  • Save mbrownnycnyc/3cd777471d768858b8ee to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/3cd777471d768858b8ee to your computer and use it in GitHub Desktop.
A quick script to email a report about file presence.
#this script is to check for the presence of files
#this will populate a variable with the specified format of today's date (see format specifier http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx)
$yyyy = $(Get-Date -format yyyy) # "2014"
$mmmm = $(Get-Date -format MMMM) # "September"
$mm = $(Get-Date -format MM) # "09"
$dd = $(Get-Date -format dd) # "22"
#if today is Friday
if ($(get-date -format dddd) -eq "Friday") {
$FilesToCheck = `
#for file-*, we will be checking yesterday's file
"\\filey\file-$(get-date -date $((get-date).adddays(-1)) -format MMddyyyy).txt",`
#for the rest, we will be checking today's file
"\\filey\file1-mmddyyyy.txt"
}
#if today is monday
elseif ($(get-date -format dddd) -eq "Monday") {
$FilesToCheck = `
"\\filey\file.txt",`
#for file1-*, we will be checking Saturday's file
"file1-$(get-date -date $((get-date).adddays(-2)) -format MM-dd-yyyy).txt"
}
#if today is Tuesday, wednesday, or Thursday
else {
$FilesToCheck = `
"\\filey\file.txt",`
"\\filey\file1.txt"
}
#create the presenceinfo object
# http://poshcode.com/paste/555/
$presenceinfo = @()
#check if the files exist
foreach ( $file in $FilesToCheck )
{
#if file exists:
try {
$objfile = get-item $file
$tempobj = New-Object PSObject -Property @{
Filename = $objfile.name
Presence = test-path $objfile
}
}
catch {
$tempobj = New-Object PSObject -Property @{
Filename = $file.substring($file.LastIndexOf('\')+1,$file.length-$file.LastIndexOf('\')-1)
Presence = $false
}
}
$presenceinfo += $tempobj
}
#http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title></title>
"@
send-mailmessage -smtpserver $smtpserver -to $recipient -from $sender -subject "file presence report" -bodyashtml -body ( $presenceinfo | select filename,presence | convertto-html -Head $Header | out-string )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment