I have an export of a list of a bunch of file locations, usernames (n the format of domain/username) and other data i. I am pulling out just the file location and username exporting these to csv files for each unique username and I want to send an email message to each user with their csv as an attachment. I define emailattach as the full file path and use that in the message.attachments.add but I get this error message.
You cannot call a method on a null-valued expression.
At C:\Users\smith\Desktop\data\parse-incident.ps1:18 char:1
+ $message.Attachments.Add($emailattach)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I cannot use send-mailmessage due to our architecture. Any suggestions?
$data = import-csv .\file.csv|select Location, "File Owner"|Sort-Object "File Owner"
$dataowner = import-csv .\file.csv|select "File Owner"|Sort-Object "File Owner" -Unique
foreach($ownername in $dataowner)
{
$owner = $ownername.'File Owner'
$emailowner = $ownername.'File Owner'+"@test.com" -creplace '^[^\\]*\\', ''
$pathname = $ownername.'File Owner'+".csv" -creplace '^[^\\]*\\', ''
$emailattach = ("c:\users\smith\desktop\data\" + $pathname)
$data|Where {$_."File Owner" -eq "$owner"}|export-csv -Path "$pathname" -NoTypeInformation -Append
}
$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
#2 = high importance email header
$mail.importance = 2
$mail.subject = “Enterprise Security - Data Needs Reviewed“
$mail.body = "Test of DLP Incident Data"
$mail.To = “$emailowner"
$message.Attachments.Add("c:\users\smith\documents\" + $pathname)
sleep 3
$mail.Send()
# $o.Quit()