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

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
Avatar of Crusadin
Crusadin

Here ya go:

NOTE: SMTP Relay needs to be configured for the server you're running this script on!!

$emailbody = "File existance script"
$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)
$Date = (Get-Date -Date ([DateTime]::Today) -Format "ddMMyyyy")
$Share = Get-Item "\\US3\Cactus\Interface Files"
$Items = $Share | Get-ChildItem | ?{(($_.Name -eq ("CO" + $Date + "I1.txt")) -or ($_.Name -eq ("CO" + $Date + "A1.txt")) -or ($_.Name -eq ("CO" + $Date + "P1.txt")))}
$i = 0
Foreach ($Item in $Items)
			{
				If ($Item.LastWriteTime.AddDays(-1).Date -eq [DateTime]::Today)
					{
						$i++
					}
			}
If ($i -eq 0)
	{
		$SMTPClient.Send($mailmessage)
	}

Open in new window

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"
}

Open in new window

@Coraxal,

Nicely done,

Only you should change your regex to "CO\d{8}[AIP]1.txt"
Avatar of footech
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.
$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
}

Open in new window

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.
Avatar of namerg

ASKER

Wow, you guys rock. I will check the respective codes.
Avatar of namerg

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.
Avatar of namerg

ASKER

@footech: I get the following error:
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

Open in new window


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)

Open in new window

Avatar of namerg

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"

Open in new window

Avatar of namerg

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."
Avatar of namerg

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.
Avatar of namerg

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 ?
This script will check for the three files.

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")

Open in new window


and:

$SMTPClient = New-Object Net.Mail.SmtpClient("smtp.DOMAIN.LOCAL", 25)

Open in new window


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)
		}

Open in new window

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?
Avatar of namerg

ASKER

@Crusadin, almost there....
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 

Open in new window

But, I have removed CO03202013P1.txt, not all of them
Oh you're using a different date format.

Change line 1:

$Date = (Get-Date -Date ([DateTime]::Today) -Format "ddMMyyyy")

Open in new window


"ddMMyyyy" should be "MMddyyyy" I think for you.
Avatar of namerg

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
Sorry, I see another mistake,

change line 3:
$Share = "\\US3\Cactus\Interface Files"

Open in new window


to $Share = "\\US3\Cactus\Interface Files\"

an extra "\" at the end
Avatar of namerg

ASKER

:( Same thing.
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt 

Open in new window

The LastAccessTime attribute must be today and 09:30,

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 + $_))
			   }

Open in new window

Oh wait, you want the lastwritetime.

Sorry,

Change line 7: "GetLastAccessTime" to "GetLastWriteTime"
Avatar of namerg

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

Open in new window

Yeah that is the LastAccessTime,

You have to change line 7: "GetLastAccessTime" to "GetLastWriteTime"
Avatar of namerg

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)
		}

Open in new window

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?
Avatar of namerg

ASKER

The same:
Missing file:CO03202013A1.txt CO03202013P1.txt CO03202013I1.txt 

Open in new window

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:

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)
		}

Open in new window


Of course you would need to edit the mail details.
Avatar of namerg

ASKER

Upss. Now i did not get anything. :( Just a blank header
Forgot to change one variable,

Change line 12, $ItemsArray to $MailArray
Avatar of namerg

ASKER

Same thing: Nada :(
Line 18 as well, $ItemsArray to $MailArray
Avatar of namerg

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

Open in new window

Avatar of namerg

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)
		}

Open in new window

$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)
		}

Open in new window

Avatar of namerg

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

Open in new window

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)
		}

Open in new window

Avatar of namerg

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

Open in new window

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)
		}

Open in new window

Avatar of namerg

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

Open in new window

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)
		}

Open in new window

Avatar of namerg

ASKER

Same as before:
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt 

Open in new window

Echo:
PS C:\scripts\FTP_Tasks> .\cactus.ps1
CO03202013A1.txt
CO03202013P1.txt
CO03202013I1.txt

Open in new window

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)
		}

Open in new window


User generated imageUser generated image
User generated image
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)
		}

Open in new window

Avatar of namerg

ASKER

Same as before:
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt 

Open in new window


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
Avatar of namerg

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.
SOLUTION
Avatar of Crusadin
Crusadin

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
@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.
True ;-)
Avatar of namerg

ASKER

:( Same thing :(
Missing file:CO03202013A1.txtCO03202013P1.txtCO03202013I1.txt 

Open in new window

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.

$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" }

Open in new window

In my testing it produced output like the following.User generated image
Avatar of namerg

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.
In my testing I ran against a folder with the following files present.  The ones that were named incorrectly were listed in the email.User generated image
Avatar of namerg

ASKER

ok, sooo.....what's the plan ?
ASKER CERTIFIED SOLUTION
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
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.
Avatar of namerg

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.
Avatar of namerg

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.
Avatar of namerg

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
Avatar of namerg

ASKER

ok