Link to home
Start Free TrialLog in
Avatar of namerg
namergFlag for United States of America

asked on

Powershell Project 3: File Pattern verification(95% Variable) and Time Stamps (95%) Constant

Hello Powershell gurus,

I have a challenging task 5 of 13 tasks. Similar to Question:
Powershell Project 2: File Pattern verification(50% Variable) and Time Stamps (90%) Constant
Task5: EmpReport
There is the following shared \\jupiter\Shared\IS\HelpDesk\EmpReport with a bunch of files.
To do:
1. Check today's date.
2. Check for 5 files with the following patterns:
*4 files modified at 2:00 PM of today's date - 1 day with the following patterns:EE.mdb, EE.ldb, upiemps.txt, upiemps.xls
*1 file with the following pattern and does not matter the date: IS Weekly Report.xls
Note: On this scenario, yes, we need to be sure about the time at 2:00pm
This is footech's code:
$fileNames = "EE.mdb","upiemps.txt","upiemps.xls","EE.ldb","IS Weekly Report.xls"
$strDate = "{0:MMddyyyy}" -f (Get-Date)
$folder = "\\jupiter\Shared\IS\HelpDesk\EmpReport"
$files = Get-ChildItem $folder | Where { !($_.PsIsContainer) -and $_.LastWriteTime.ToString("MMddyyyy") -eq $strDate } | ForEach { $_.Name }
If ( !($files) )
{
  $missing = $fileNames
}
Else
{
  $missing = Compare-Object $files $fileNames -passthru | Where { $_.SideIndicator -eq "=>" }
}
If ( $missing )
{
  Send-MailMessage -from "EmdeonArrivalTask@domain.com" `
    -to "Me" `
    -subject "EMP REPORT file missing" `
    -body "The following files were not found:`n$($missing -join "`n")" `
    -smtpServer "exchange.domain.com"
}
Else
{ "All files found" }

Open in new window

Avatar of footech
footech
Flag of United States of America image

Here's my mods for this scenario.
$fileNames1 = "EE.mdb","upiemps.txt","upiemps.xls","EE.ldb"
$fileNames2 = "IS Weekly Report.xls"
$fileNames = $fileNames1 + $fileNames2
$strDate = "{0:MMddyyyy}" -f (Get-Date).AddDays(-1)
$timeStamp = "$($strDate)1400"
$folder = "\\jupiter\Shared\IS\HelpDesk\EmpReport"
$files = gci $folder | Where { !($_.PsIsContainer) `
  -and (($fileNames1 -contains $_.Name `
  -and $_.LastWriteTime.ToString("MMddyyyyHHmm") -eq $timeStamp) `
  -or ($fileNames2 -contains $_.Name)) `
} | ForEach { $_.Name }

If ( !($files) )
{
  $missing = $fileNames
}
Else
{
  $missing = Compare-Object $files $fileNames -passthru | Where { $_.SideIndicator -eq "=>" }
}
If ( $missing )
{
  Send-MailMessage -from "EmdeonArrivalTask@domain.com" `
    -to "Me" `
    -subject "EMP REPORT file missing" `
    -body "The following files were not found:`n$($missing -join "`n")" `
    -smtpServer "exchange.domain.com"
}
Else
{ "All files found" }

Open in new window

Avatar of namerg

ASKER

@Footech: PERFECT. But, you know what, with the other scripts that we worked on it and this one. I forgot to mention the scenario that if it is Monday, we might need to check for files either dated Sunday or Saturday or Friday.. So, can you code the scenario that if it is Monday, check for Sunday, Saturday or Friday ? So, I can implement that code in the other tasks ?
Thanks for your help.
We'll need to know the exact behavior.  For example, on Monday should the check be for files on only one of the days (Friday, Saturday, Sunday), or possibly some mix (some files Friday, some Saturday, etc.), or something else.

I could see this getting real messy.
Perhaps the better solution is to just run the scheduled tasks that generate all these files on Sunday (or whatever day), so that the files are created in a manner that is consistent with the checks as they are written now.  However, I could see how that could create problems or not even be possible given how other processes function or what those scheduled tasks do exactly.  Just throwing the idea out there...  Take a closer look at what's happening, decide what you think is the best course, and post back some detail.  We'll see where we go from there.
Avatar of namerg

ASKER

You are right, this can be messy. I am on call next week and is my turn to do the file checking, So, I will let you know on Monday, what are the status of the files. Thanks for your help
Avatar of namerg

ASKER

@footech, i just found a doc that I did last December and for this task, the EmpReport that you coded here 39011977 On Monday, this code needs to check for those files timestamped on Friday, 2:00pm and I checked the other tasks and gets more complicated because the files has mm-dd-yy on it. But, let's solve this one which on Monday, needs to check for those files dated Friday and timestamped 2:00pm
For that, replace line 4 with the following lines.
$now = Get-Date
If ($now.DayOfWeek -eq "Monday")
{ $strDate = "{0:MMddyyyy}" -f $now.AddDays(-3) }
Else
{ $strDate = "{0:MMddyyyy}" -f $now.AddDays(-1) }

Open in new window

Avatar of namerg

ASKER

hmm, not quite. I got an email saying: The following files were not found:EE.mdb upiemps.txt upiemps.xls eventhoug those files exist but timestamped 3/22/2013 2:00pm
I get the correct string when I run that.

Can you post your entire code?  I want to make sure it's integrated correctly.
Avatar of namerg

ASKER

Here it goes:
#At Today's Date - 1 Day at 2:00pm. IS Weekly Report.xls
#$fileNames1 = "EE.mdb","upiemps.txt","upiemps.xls","EE.ldb"
$fileNames1 = "EE.mdb","upiemps.txt","upiemps.xls"
$fileNames2 = "IS Weekly Report.xls"
$fileNames = $fileNames1 + $fileNames2
$strDate = "{0:MMddyyyy}" -f (Get-Date).AddDays(-1)
$now = Get-Date
If ($now.DayOfWeek -eq "Monday")
{ $strDate = "{0:MMddyyyy}" -f $now.AddDays(-3) }
Else
{ $strDate = "{0:MMddyyyy}" -f $now.AddDays(-1) }
$folder = "\\jupiter\Shared\IS\HelpDesk\EmpReport"
$files = gci $folder | Where { !($_.PsIsContainer) `
  -and (($fileNames1 -contains $_.Name `
  -and $_.LastWriteTime.ToString("MMddyyyyHHmm") -eq $timeStamp) `
  -or ($fileNames2 -contains $_.Name)) `
} | ForEach { $_.Name }

If ( !($files) )
{
  $missing = $fileNames
}
Else
{
  $missing = Compare-Object $files $fileNames -passthru | Where { $_.SideIndicator -eq "=>" }
}
If ( $missing )
{
  Send-MailMessage -from "EmdeonArrivalTask@domain.com" `
    -to "Me" `
    -subject "EMP REPORT file missing" `
    -body "The following files were not found:`n$($missing -join "`n")" `
    -smtpServer "exchange.domain.com"
}
Else
{ "All files found" }

Open in new window

Looks like you replaced line 5 instead of line 4 (working off the mods as seen in my first post).

To correct what you just posted, remove line 6 (where $strDate is being defined, instead it is set later in the If-Else statement), and add back the line $timeStamp = "$($strDate)1400" which needs to be after $strDate is set.
Avatar of namerg

ASKER

Will do.
Avatar of namerg

ASKER

Nada. I am still getting The following files were not found: EE.mdb upiemps.txt upiemps.xls
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

@footech: PERFECT.