Link to home
Start Free TrialLog in
Avatar of Interlink_IT
Interlink_IT

asked on

Sending an attachment via powershell is timing out

Hi,
I have a powershell script to look in a folder for existence of a file, and email it to an address as follows:

(Note: I have inserted the xxx's for addresses and server, the script has real details here)

#Path to driver sheets folder
$checkForFiles = "c:\predict\sheets\*"
#Test for the existence of files
$fileExistence = test-path $checkForFiles

#check for the existence of files in the C:\Predict\Sheets folder
            if ($fileExistence -eq $true)
      {
            
            # If files exist send them as attachments
            send-mailmessage -subject "Run Sheets" -from depot@xxx.xx -to predict@xxx.xx -smtpserver xxxxx -attachments (get-childitem $checkForFiles)
        sleep 5
        Move-Item (get-childitem $checkForFiles) C:\predict\archive -force
          }
      
      # If no files exist, do nothing
      else
      {write-host "nothing to process"}


I have this running via a scheduled task in 37 locations, but it is failing in just one place.

The error is:


Send-MailMessage : The operation has timed out.
At C:\Predict\email-runsheets.ps1:11 char:19
+         send-mailmessage <<<<  -subject "Run Sheets" -from depot@xxx.xx -to predict@xxx.xx -smtpserver xxxx -attachments (get-childitem $checkForFiles)
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [
   Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
 



I do not get this error with a very small attachments, (less than 1mb), but I am getting it with a 1.8Mb
file.
It appears that the session is timing out before it gets to complete sending the mail.

I see scripts on the internet that allow you to put a timeout in place for exactly this purpose (e.g. http://mspowershell.blogspot.com/2007/12/send-smtpmail-update.html)  but I would like to use my
script as it works in 37 different places except this one, and I want it to be standard script.

Is there some cmdlet in powershell 2 that I can add to the script to keep the mail session "alive" until
it is complete, or can anyone suggest a suitable fix?  

Thank you.
Dermot.
Avatar of daveTechSearch
daveTechSearch
Flag of Canada image

can you try changing this:

            send-mailmessage -subject "Run Sheets" -from depot@xxx.xx -to predict@xxx.xx -smtpserver xxxxx -attachments (get-childitem $checkForFiles)


to this:
get-childitem $checkForFiles | foreach{$_.fullName} | send-mailmessage -subject "Run Sheets" -from depot@xxx.xx -to predict@xxx.xx -smtpserver xxxxx
Avatar of Interlink_IT
Interlink_IT

ASKER

Hi,
Thanks for replying, but that gives the same timeout error message when I try and send a 1.8mb file.

Dermot.
Hi,
Is there a cmdlet that could zip up the file before attaching it maybe?  If this got the size down, then the emailing will probably work.

Dermot
For zipping you may want to check out the PowerShell community extentions....

http://pscx.codeplex.com/

there is a cmdlet called 'write-zip'.  I haven't had a need to use it for myself, but I believe you need to install 7-zip for it all to work.
Avatar of Brent Challis
Have you considered testing for the size of the file and then if it is over 1Mg, using a different timeout?

As an aside,  if ($fileExistence -eq $true)  should work as  if ($fileExistence), which ,IMO, is a bit cleaner.

Cheers.
@bchallis: The problem is that I can't set a timeout anywhere, unless you can advise on how?

Dermot.
ASKER CERTIFIED SOLUTION
Avatar of Brent Challis
Brent Challis
Flag of Australia 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