0
votes

I have made some progress, and stuck with the attachment. Below script now sends an email but with not an attachment

$sub = "APERAK/INVRPT Report "
$to="[email protected]"
$folder = "C:\script\APERAK"
$files = Get-ChildItem "C:\script\APERAK"
$tstmp = Get-Date -UFormat "%H%M"
$dstamp = Get-Date -UFormat "%Y%m%d"
$from="[email protected]"
$smtpserver = "mail.XXXXXXXX.com"
for ($i=0; $i -lt $files.Count; $i++) {

$subject=  $sub + $dstamp #+ " " +$tstmp
$filename =  $files[$i].FullName
$abpath = $folder + $files[$i].FullName

$attachment = new-object Net.Mail.Attachment($abpath)

$body= Get-Content $filename
$SMTP = new-object Net.Mail.SmtpClient($smtpserver)
$MSG = new-object Net.Mail.MailMessage($from, $to, $subject, $body)
$MSG.attachments.add($attachment)
$SMTP.send($msg)
}

Exception calling "Add" with "1" argument(s): "Value cannot be null. Parameter name: item" At C:\GentranScripts\APERAK_REPORT_EMAIL1.ps1:22 char:21 + $MSG.attachments.add <<<< ($attachment) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

1
for some reason, the script is entering into the for loop. ... so what is the problem here ? - Dhaval
do you mean is NOT entering? or is entering but not sending the email? more details, please? - Lee_Dailey
I dont know what the problem is, the cursor is not entering the for loop. - chandan T
Are you sure "C:\Reports\APERAK" is the correct path on your server? Try to output $files.Count on its own and look at the output. - Paxz
Why don't you use foreach? As I can see you do not use $i for anything beyond addressing of an array's element. - montonero

1 Answers

2
votes

The problem is that Get-Childitem will return two types of objects. If there's just a single file, $files = Get-ChildItem "C:\Reports\APERAK" will contain a FileInfo. If there's more, it'll contain an array of FileInfo objects. Let's look at a sample case:

md foo
cd foo
set-content -Path "foo.txt" -Value ""
$files = gci
$files.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     FileInfo                                 System.IO.FileSystemInfo

set-content -Path "foo2.txt" -Value ""
$files2 = gci
$files2.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

As for a solution, wrap gci results into an array. Like so,

$files = @(Get-ChildItem "C:\Reports\APERAK")