Hello Technet,
I have a file that gets created every night by a batch job and I need to send that file every night. So, I came up with a method (if there's a better way, I'd like to hear). I created a 2 hour timer object to run a while loop. While the time elapsed is less than 2 hours, the loop will run. In the loop, I have a if condition to test for a file. If the file is not there, then sleep for 10 minutes. If the file is there, then send it to me with send-mailmessage. The problem is, I will get the file sent to me at every interval because the loop is terminated by the timer. I would like the loop to terminate once the email message is sent to me. Here is my code snip. Can someone please help on getting the loop to terminate when the message is sent?
###
$file = "c:\file.txt"
$timeout = new-timespan -hours 2
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout)
{
if(!(test-path -path "$file1"))
{
"
file is not there...yet
"
start-sleep -seconds 600
}
else
{
$SmtpServer = "relay"
$To = "my@emailaddr"
$From = "me@emailaddr"
$Subj = "Got it"
$Attm = "$file1"
$Body = "Please see Attachment"
Send-MailMessage -smtpserver $SmtpServer -from $From -to $To -subject $Subj -body $Body -attachment $Attm
}