namerg
asked on
Powershell Project: File Pattern verification and Time Stamps
Hello Powershell gurus,
I have another challenging task of 13 tasks.
Task1: Cactus
There is the following shared \\US3\Cactus\Interface Files with a bunch of files.
To do:
1. Check today's date.
2. Check for three files with the following pattern COmmddyyyyI1.txt, COmmddyyyyA1.txt, COmmddyyyyP1.txt
3. Check the Date modified time stamp for a day before today's date at 9:30am
Conventions:
mm = Month
dd = Day = Today's date - 1 day
yyyy = Current Year
4. If none of those files exist send an email
I have another challenging task of 13 tasks.
Task1: Cactus
There is the following shared \\US3\Cactus\Interface Files with a bunch of files.
To do:
1. Check today's date.
2. Check for three files with the following pattern COmmddyyyyI1.txt, COmmddyyyyA1.txt, COmmddyyyyP1.txt
3. Check the Date modified time stamp for a day before today's date at 9:30am
Conventions:
mm = Month
dd = Day = Today's date - 1 day
yyyy = Current Year
4. If none of those files exist send an email
If I understood your task correctly, you can try something like this:
$Today = [DateTime]::Today.AddHours(9).AddMinutes(30)
$OneDayAgo = $Today.AddDays(-1)
$Files = Get-ChildItem "\\US3\Cactus\Interface Files" |
? {$_.Name -match "CO\d{8}[a-zA-Z]1.txt"} |
? {$_.LastWriteTime -gt $OneDayAgo -and $_.LastWriteTime -lt $Today}
if($Files -eq $null){
Send-MailMessage -From "Me@domain.com" `
-To "You@domain.com" `
-Subject "Didn't find files" `
-Body "No files found." `
-SmtpServer "emailserver.domain.com"
}
@Coraxal,
Nicely done,
Only you should change your regex to "CO\d{8}[AIP]1.txt"
Nicely done,
Only you should change your regex to "CO\d{8}[AIP]1.txt"
I also found myself wondering about the requirement for #3. After reading coraxal's post, his approach makes more sense than what I had thought the question was asking for. Some clarification would be helpful if you're not getting the matches you want.
My own contribution (including what I now think is the wrong comparison on the timestamp), is a little more specific on the file name matching.
My own contribution (including what I now think is the wrong comparison on the timestamp), is a little more specific on the file name matching.
$strDate = (Get-Date).AddDays(-1) -f MMddyyyy
$timeStamp = "$($strDate)0930"
$folder = "\\US3\Cactus\Interface Files"
$files = gci $folder | Where { !($_.PsIsContainer) `
-and $_.Name -match "CO$strDate[IAP]1.txt" `
-and $_.LastWriteTime.ToString("MMddyyyyHHmm") -eq $timeStamp `
}
If ( !($files) )
{
Send-MailMessage -from "User01 <user01@yourcompany.com>" `
-to "User02 <user02@yourcompany.com>" `
-subject "No files found" `
-body "Didn't find any files for $($strDate)" `
-smtpServer smtp.yourcompany.com
}
You line 5 would'nt cut it,
OP is looking for a pattern on file name, not an exact date.
Coraxal's script with the changes for regex on line 4 would provide the wanted solution.
OP is looking for a pattern on file name, not an exact date.
Coraxal's script with the changes for regex on line 4 would provide the wanted solution.
ASKER
Wow, you guys rock. I will check the respective codes.
ASKER
Damn, I have to wait past 10:00am MST to get the new set of files for today's date and run the script. I will keep you posted.
It may not be needed to be that specific with the filename match, especially considering the date/time filter, but if the file(s) are actually named with yesterdays's date, then mine should work just fine.
ASKER
@footech: I get the following error:
Also, as an attachment those are the files that they just got generate it and need to check. Brb, I have a meeting
Capture.PNG
You must provide a value expression on the right-hand side of the '-f' operator.
At C:\scripts\FTP_Tasks\cactus.ps1:13 char:37
+ $strDate = (Get-Date).AddDays(-1) -f <<<< MMddyyyy
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression
Also, as an attachment those are the files that they just got generate it and need to check. Brb, I have a meeting
Capture.PNG
You can substitute the following line instead. One thing to note, with only the files that are in your screenshot, it wouldn't find a match until you ran the script tomorrow. Today it would have a result of "03192013".
$strDate = "{0:MMddyyyy}" -f (Get-Date).AddDays(-1)
ASKER
@footech: Ok, I did add the modification you suggested, and deleted CO03202013P1.txt from the share and ran the script but did not get an email :(
Have you run coraxal's or Crusadin's? You may just have a problem with the email portion. You might want to try just running the following to see whether any changes need to be made for email to flow.
Send-MailMessage -from "you@yourcompany.com>" `
-to "someone@yourcompany.com>" `
-subject "No files found" `
-body "Test email" `
-smtpServer "smtp.yourcompany.com"
ASKER
Dahhh, I have had removed the "`" :)
So, now testing the functionality of the script, i did remove CO03202013P1.txt from that share but did not get the email though. I am using your script because you said "ut if the file(s) are actually named with yesterdays's date, then mine should work just fine."
So, now testing the functionality of the script, i did remove CO03202013P1.txt from that share but did not get the email though. I am using your script because you said "ut if the file(s) are actually named with yesterdays's date, then mine should work just fine."
ASKER
ohh sorry guys, file naming should be dated today and date modified today's date at 9:30am
Does the Send-MailMessage command like I posted in #a39004322 work? Just want to know where to focus troubleshooting.
ASKER
Got it.. I just removed the -f (Get-Date).AddDays(-1)
In the email header i got Didn't find any files for {0:MMddyyyy}
Is there any way to say this whatever file is missing ?
In the email header i got Didn't find any files for {0:MMddyyyy}
Is there any way to say this whatever file is missing ?
This script will check for the three files.
If anyone is missing, it will e-mail you the name
You'll have to edit:
and:
with the correct information.
Script:
If anyone is missing, it will e-mail you the name
You'll have to edit:
mailmessage.To.add("namerg@gmail.com")
$mailmessage.from = ("No-Reply@DOMAIN.LOCAL")
and:
$SMTPClient = New-Object Net.Mail.SmtpClient("smtp.DOMAIN.LOCAL", 25)
with the correct information.
Script:
$Date = (Get-Date -Date ([DateTime]::Today) -Format "ddMMyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files"
$ItemsArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastAccessTime(($Share + $_)) -eq $Date2))
{
$ItemsArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$ItemsArray | %{$emailbody += [String]$_;$emailbody += [System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("namerg@gmail.com")
$mailmessage.from = ("No-Reply@DOMAIN.LOCAL")
$mailmessage.Subject = "File existance script"
$mailmessage.Body = $emailbody
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("smtp.DOMAIN.LOCAL", 25)
$SMTPClient.Send($mailmessage)
}
A couple questions.
1) Does the file modified time have to be 9:30 am or should some variability be allowed?
2) Are you wanting to send an email if any one (or more) of the three files is missing, or only when all of the three files are missing?
1) Does the file modified time have to be 9:30 am or should some variability be allowed?
2) Are you wanting to send an email if any one (or more) of the three files is missing, or only when all of the three files are missing?
ASKER
@Crusadin, almost there....
The following is what i got in the email:
The following is what i got in the email:
From: No-Reply@Domain.com
Sent: Wednesday, March 20, 2013 1:10 PM
To: Me
Subject: Didn't find Cactus files
Missing file:CO20032013A1.txt CO20032013P1.txt CO20032013I1.txt
But, I have removed CO03202013P1.txt, not all of them
Oh you're using a different date format.
Change line 1:
"ddMMyyyy" should be "MMddyyyy" I think for you.
Change line 1:
$Date = (Get-Date -Date ([DateTime]::Today) -Format "ddMMyyyy")
"ddMMyyyy" should be "MMddyyyy" I think for you.
ASKER
Ok, but still saying:
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt
Which the only missing file in this scenario is CO03202013P1.txt but could be any or all of them
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt
Which the only missing file in this scenario is CO03202013P1.txt but could be any or all of them
Sorry, I see another mistake,
change line 3:
to $Share = "\\US3\Cactus\Interface Files\"
an extra "\" at the end
change line 3:
$Share = "\\US3\Cactus\Interface Files"
to $Share = "\\US3\Cactus\Interface Files\"
an extra "\" at the end
ASKER
:( Same thing.
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt
The LastAccessTime attribute must be today and 09:30,
can you run this script and tell me the output:
can you run this script and tell me the output:
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$ItemsArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
$_
[System.IO.File]::GetLastAccessTime(($Share + $_))
}
Oh wait, you want the lastwritetime.
Sorry,
Change line 7: "GetLastAccessTime" to "GetLastWriteTime"
Sorry,
Change line 7: "GetLastAccessTime" to "GetLastWriteTime"
ASKER
Same thing. The following is what you requested on 39004708
PS C:\scripts\FTP_Tasks> .\test.ps1
CO03202013A1.txt
Wednesday, March 20, 2013 12:48:12 PM
CO03202013P1.txt
Wednesday, March 20, 2013 1:28:02 PM
CO03202013I1.txt
Wednesday, March 20, 2013 9:58:00 AM
Yeah that is the LastAccessTime,
You have to change line 7: "GetLastAccessTime" to "GetLastWriteTime"
You have to change line 7: "GetLastAccessTime" to "GetLastWriteTime"
ASKER
Hmm I did...This is the code:
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$ItemsArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime(($Share + $_)) -eq $Date2))
{
$ItemsArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$ItemsArray | %{$emailbody += [String]$_;$emailbody += [System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@domain.com")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.Body = $emailbody
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
Seems about right,
Except for the $mailmessage.To.add("Me"), but I assume you will fill that in with an working emailaddress.
What are the results?
Except for the $mailmessage.To.add("Me"),
What are the results?
ASKER
The same:
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt
Ok I did a little test on my side, it seems you cannot remove an item in a collection within an pipeline of the collection, So I added an additional array.
This should work:
Of course you would need to edit the mail details.
This should work:
cls
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$ItemsArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$ItemsArray | %{$MaiLArray += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("namerg@gmail.com")
$mailmessage.from = ("No-Reply@DOMAIN.LOCAL")
$mailmessage.Subject = "File existance script"
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("smtp.DOMAIN.LOCAL", 25)
$SMTPClient.Send($mailmessage)
}
Of course you would need to edit the mail details.
ASKER
Upss. Now i did not get anything. :( Just a blank header
Forgot to change one variable,
Change line 12, $ItemsArray to $MailArray
Change line 12, $ItemsArray to $MailArray
ASKER
Same thing: Nada :(
Line 18 as well, $ItemsArray to $MailArray
ASKER
Got errors:
You cannot call a method on a null-valued expression.
At C:\scripts\FTP_Tasks\cactus.ps1:42 char:44
+ @("A1","P1","I1") | %{[Void]$ItemsArray.Add <<<< (("CO" + $Date + $_ + ".txt"))}
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\scripts\FTP_Tasks\cactus.ps1:42 char:44
+ @("A1","P1","I1") | %{[Void]$ItemsArray.Add <<<< (("CO" + $Date + $_ + ".txt"))}
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\scripts\FTP_Tasks\cactus.ps1:42 char:44
+ @("A1","P1","I1") | %{[Void]$ItemsArray.Add <<<< (("CO" + $Date + $_ + ".txt"))}
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
ASKER
Code:
Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$MailArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$MailArray .Add(("CO" + $Date + $_ + ".txt"))}
$MailArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$ItemsArray | %{$MaiLArray += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$MailArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$MailArray .Add(("CO" + $Date + $_ + ".txt"))}
$MailArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
ASKER
Got this:
Unexpected token '.Add' in expression or statement.
At C:\scripts\FTP_Tasks\cactus.ps1:6 char:44
+ @("A1","P1","I1") | %{[Void]$MailArray .Add <<<< (("CO" + $Date + $_ + ".txt"))}
+ CategoryInfo : ParserError: (.Add:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
removed space:
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$MailArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$MailArray.Add(("CO" + $Date + $_ + ".txt"))}
$MailArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($ItemsArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
ASKER
Error: :(
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At C:\scripts\FTP_Tasks\cactus.ps1:7 char:1
+ <<<< $MailArray | %{
+ CategoryInfo : InvalidOperation: (System.Collecti...numeratorSimple:ArrayListEnumeratorSimple) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
Wait, I just noticed you changed all the ItemsArray except one,
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$ItemsArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$MailArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($MailArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
ASKER
Error:
PS C:\scripts\FTP_Tasks> .\cactus.ps1
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At C:\scripts\FTP_Tasks\cactus.ps1:7 char:1
+ <<<< $MailArray | %{
+ CategoryInfo : InvalidOperation: (System.Collecti...numeratorSimple:ArrayListEnumeratorSimple) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
But got a blank email header
Yeah forgot one i saw:
cls
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "C:\test\"
$ItemsArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
$MailArray
If (!([System.String]::IsNullOrEmpty($MailArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$mailmessage.Body += $emailbody
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
ASKER
Same as before:
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt
Echo:PS C:\scripts\FTP_Tasks> .\cactus.ps1
CO03202013A1.txt
CO03202013P1.txt
CO03202013I1.txt
cls
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "C:\test\"
$ItemsArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($MailArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$mailmessage.Body += $emailbody
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
cls
$Date = (Get-Date -Date ([DateTime]::Today) -Format "MMddyyyy")
$Date2 = [DateTime]::Today.AddHours(9.5)
$Share = "\\US3\Cactus\Interface Files\"
$ItemsArray = New-Object System.Collections.ArrayList
$MailArray = New-Object System.Collections.ArrayList
@("A1","P1","I1") | %{[Void]$ItemsArray.Add(("CO" + $Date + $_ + ".txt"))}
$ItemsArray | %{
[Void]$MailArray.Add($_)
If(([System.IO.File]::Exists($Share + $_)) -and ([System.IO.File]::GetLastWriteTime($Share + $_)) -eq $Date2)
{
$MailArray.Remove($_)
}
}
If (!([System.String]::IsNullOrEmpty($MailArray)))
{
$emailbody = "Missing file:"
$MailArray | %{$emailbody += [String]$_;[System.Environment]::NewLine}
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.To.add("Me")
$mailmessage.from = ("CactusTask@upicolo.pvt")
$mailmessage.Subject = "Didn't find Cactus files"
$mailmessage.IsBodyHTML = $true
$mailmessage.Body += $emailbody
$SMTPClient = New-Object Net.Mail.SmtpClient("exchange.domain.com", 25)
$SMTPClient.Send($mailmessage)
}
ASKER
Same as before:
Do you understand, what I am trying to do, right ?
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt
Do you understand, what I am trying to do, right ?
3 files (CO03202013A1.txt, CO03202013P1.txt, CO03202013I1.txt ) within the folder "\\US3\Cactus\Interface Files\"
With a LastWriteTime of todays date of 09:30, in this case, 03-20-2013 09:30
Send an email containing the files name for files that do not have a LastWriteTime of 03-20-2013 09:30
With a LastWriteTime of todays date of 09:30, in this case, 03-20-2013 09:30
Send an email containing the files name for files that do not have a LastWriteTime of 03-20-2013 09:30
ASKER
Correct in what you are saying.
So, On the following scenario, I deleted from the share CO03202013P1.txt, ran the script and I should received an email: Missing file CO03202013P1.txt. but I could get an email for the invidual files that are missing from that set or all of them from that set.
So, On the following scenario, I deleted from the share CO03202013P1.txt, ran the script and I should received an email: Missing file CO03202013P1.txt. but I could get an email for the invidual files that are missing from that set or all of them from that set.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Crusadin - I think you're right, however my opinion that getting that granular on time checking is dangerous simply because the source generating these files may or may not be exact (exporting routine too longer than usual, or any other reason), which will ultimately yield unexpected results (not getting an email when you should get the email, etc.) For this type of checking, I'd expect to check a time window (between 930am yesterday and 930 today, or within last hour etc. vs. checking "exact" LastWriteTimes. Nice script btw.
@Coraxal,
I'm 100% with you on that,
However, I think it might be quite the oppositefor this request.
I think that is what the opening author wants the script to do.
If the files aren't last modified on exactly 09:30 (leaving out the seconds), then an e-mail will be generated containing the name of the file that isnt last modified at 09:30 or if the file does not exist at all.
I'm 100% with you on that,
However, I think it might be quite the oppositefor this request.
I think that is what the opening author wants the script to do.
If the files aren't last modified on exactly 09:30 (leaving out the seconds), then an e-mail will be generated containing the name of the file that isnt last modified at 09:30 or if the file does not exist at all.
True ;-)
ASKER
:( Same thing :(
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt
I was in the same spot, wondering how much variability should be allowed in the timestamp.
@Crusadin - Looks like you took a different route for narrowing down the time to just hours and minutes than I used. It's always interesting to me the different ways people approach a problem.
@Crusadin - Looks like you took a different route for narrowing down the time to just hours and minutes than I used. It's always interesting to me the different ways people approach a problem.
$strDate = "{0:MMddyyyy}" -f (Get-Date)
$timeStamp = "$($strDate)0930"
$folder = "\\US3\Cactus\Interface Files"
$objCompare = @()
"I","A","P" | ForEach { $objCompare += "CO$strDate" + $_ + "1.txt"}
$files = gci $folder | Where { !($_.PsIsContainer) `
-and $_.Name -match "CO$strDate[IAP]1.txt" `
-and $_.LastWriteTime.ToString("MMddyyyyHHmm") -eq $timeStamp `
} | ForEach { $_.Name }
If ( $files.count -lt 3 )
{
$missing = Compare-Object $files $objCompare -passthru | Where { $_.SideIndicator -eq "=>" }
Send-MailMessage -from "someone@yourcompany.com" `
-to "someoneelse@yourcompany.com" `
-subject "Files missing" `
-body "The following files were not found:`n$($missing -join "`n")" `
-smtpServer "smtp.yourcompany.com"
}
Else
{ "All files found" }
In my testing it produced output like the following.
ASKER
@footech. To test the functionality as I said before, I deleted from the share CO03202013P1.txt, then i ran the script. I should get an email that CO03202013P1.txt is missing for this case, though.
ASKER
ok, sooo.....what's the plan ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The plan? It appears to be working in all cases so I'm not sure what you mean. In my testing I just renamed a file which would give the same result as deleting the file. In both cases a match isn't found so an email is sent saying that the file is missing.
ASKER
PERFECT @Footech. I will split the points for the knowledge contributed. Is that ok?
I will post the 2nd task if i need, maybe i can use ur code as a guide for the other tasks.
I will post the 2nd task if i need, maybe i can use ur code as a guide for the other tasks.
ASKER
Hmm, I think I might need to see how the script is going to behave if it is Monday, it will need to look the info for Friday.
Are you sure? I know the question started as checking the files for yesterday's date but then it changed to today's date. You know your environment and the process behind all these files, but I'm thinking that unless files are not created on Monday there shouldn't be a problem.
ASKER
Well, there are some schedule tasks that run overnight during the weekend although not all of them. So, the fact that today it is Friday, do not remember what is being generated during the weekend. Right know we do not have an automated way to do the file checking, we follow a spreadsheet, browse into the share, see with our eyes if the file or files are there and eventually we might need to add the holiday part, because if the office is closed, the file will not be generated or will have a wrong time stamp.
OK. We'll need to know the exact behavior - when files are generated and when they are not - to be able to check for it.
I suggest we continue this in the other question so all the comments are together. I'm including a link for others.
Q_28073394.html#a39012005
I suggest we continue this in the other question so all the comments are together. I'm including a link for others.
Q_28073394.html#a39012005
ASKER
ok
NOTE: SMTP Relay needs to be configured for the server you're running this script on!!
Open in new window