Page 1 of 1
Powershell Snd-MailMessage Fail to Attach File
Posted: Fri Sep 02, 2022 3:21 pm
by kenship
Hi all,
I have a functioning TI process to send email. I'm trying to modify it to attach file but failed.
Code: Select all
sFrom = 'TM1_Alert@123XYZ.com';
sTo1 = pRecipient1;
sSubject =pSubject;
sBody = pBody;
sSMTPServer = 'smtp.123XYZ.com';
sAttachment = 'E:\System\ExtractFiles\Test.csv';
# build powershell command
sPsCmd1 = '"& {
Send-MailMessage
-From ' | sFrom | '
-To ' | sTo1 | '
-Subject ' | '\"' | sSubject | '\
-Body ' | '\"' | sBody | '\"
-Attachments ' | sAttachment | '
-SmtpServer ' | sSmtpServer | '
}"';
# execute command
ExecuteCommand ( 'powershell.exe -command ' | sPsCmd1, 1 );
" -Attachments ' | sAttachment | ' " is the new line I added. The process runs fine if I exclude it, but failed (not email generated) if included.
Regarding the file path, "E:\System\Data" is where the TM1 data is stored.
Any help is appreciated.
Kenneth
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Fri Sep 02, 2022 3:30 pm
by MarenC
Hi,
You do have 2 spaces between:
-Attachments '
Maren
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Fri Sep 02, 2022 3:33 pm
by gtonkin
And more likely -Attachments should be -Attachment
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sat Sep 03, 2022 12:32 am
by Niko
kenship wrote: ↑Fri Sep 02, 2022 3:21 pm
Hi all,
I have a functioning TI process to send email. I'm trying to modify it to attach file but failed.
Code: Select all
sFrom = 'TM1_Alert@123XYZ.com';
sTo1 = pRecipient1;
sSubject =pSubject;
sBody = pBody;
sSMTPServer = 'smtp.123XYZ.com';
sAttachment = 'E:\System\ExtractFiles\Test.csv';
# build powershell command
sPsCmd1 = '"& {
Send-MailMessage
-From ' | sFrom | '
-To ' | sTo1 | '
-Subject ' | '\"' | sSubject | '\
-Body ' | '\"' | sBody | '\"
-Attachments ' | sAttachment | '
-SmtpServer ' | sSmtpServer | '
}"';
# execute command
ExecuteCommand ( 'powershell.exe -command ' | sPsCmd1, 1 );
" -Attachments ' | sAttachment | ' " is the new line I added. The process runs fine if I exclude it, but failed (not email generated) if included.
Regarding the file path, "E:\System\Data" is where the TM1 data is stored.
Any help is appreciated.
Kenneth
Can powershell cmd send email with attachment?
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sat Sep 03, 2022 1:59 pm
by kenship
To answer all:
https://edu.cubewise.com/2019/11/13/tip ... using-tm1/
- Tested 2 spaces and 1 space have no difference
- "Attachments" not "Attachment"
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sat Sep 03, 2022 2:37 pm
by shahhere
If you want to use VBS and a Batch script I have a script I've used and modified to send attachments along with an optional HTML body as well...let me know.
Shahhere
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sat Sep 03, 2022 3:22 pm
by ascheevel
kenship wrote: ↑Fri Sep 02, 2022 3:21 pm
Code: Select all
sAttachment = 'E:\System\ExtractFiles\Test.csv';
Regarding the file path, "E:\System\Data" is where the TM1 data is stored.
Are you saying the file you're trying to attach and your tm1 data directory are in similar location on the E:? If not, have you confirmed your TM1 server can read/write files from E:\System\ExtractFiles\? You perhaps already know this, but for anyone stumbling on this who doesn't, when you run a command from a TI process using ExecuteCommand, it's executed on the server and user account that's hosting that specific TM1 server.
When I've done things like this in powershell in the past, I always make sure the powershell works first on my local machine before trying to run it from TI process on tm1 server. Have you done that already?
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sun Sep 04, 2022 7:47 pm
by kenship
Just wondering but I suppose I can still call the script from TI right? If so, then yes please share I definitely appreciate it.
Kenneth
shahhere wrote: ↑Sat Sep 03, 2022 2:37 pm
If you want to use VBS and a Batch script I have a script I've used and modified to send attachments along with an optional HTML body as well...let me know.
Shahhere
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Sun Sep 04, 2022 7:56 pm
by kenship
The file to be attached is actually on the same drive as TM1 data (E: drive).
I suppose I can test it in my local machine but then if works in local machine but not on TM1 server then what's does it mean?
Kenneth
ascheevel wrote: ↑Sat Sep 03, 2022 3:22 pm
kenship wrote: ↑Fri Sep 02, 2022 3:21 pm
Code: Select all
sAttachment = 'E:\System\ExtractFiles\Test.csv';
Regarding the file path, "E:\System\Data" is where the TM1 data is stored.
Are you saying the file you're trying to attach and your tm1 data directory are in similar location on the E:? If not, have you confirmed your TM1 server can read/write files from E:\System\ExtractFiles\? You perhaps already know this, but for anyone stumbling on this who doesn't, when you run a command from a TI process using ExecuteCommand, it's executed on the server and user account that's hosting that specific TM1 server.
When I've done things like this in powershell in the past, I always make sure the powershell works first on my local machine before trying to run it from TI process on tm1 server. Have you done that already?
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Mon Sep 05, 2022 1:04 pm
by Elessar
Hi,
Here is mine:
Code: Select all
$serverSmtp = "smtp.XXXXXX.XXX"
#May be different! Check with mail server administrator
$port = 587
$encoding = [System.Text.Encoding]::UTF8
$From = "XX@XXXX.XXX"
$To1 = "XXX"
$To2 = "XXXXX"
$subject = "My subject"
$user = "XXX"
$pass = "XXXX"
$date = Get-Date -Format "dd.MM.yyyy"
$mes = New-Object System.Net.Mail.MailMessage
$mes.BodyEncoding = $Encoding
$mes.SubjectEncoding = $Encoding
$mes.From = $from
$mes.To.Add($to1)
$mes.To.Add($to2)
$mes.Subject = $subject
$mes.IsBodyHTML = $true
$mes.Body = "<h1>Bla bla blah " + $date + " </h1>"
$file = "D:\Folder\MyFile.txt"
$mes.Attachments.Add($file)
$smtp = New-Object Net.Mail.SmtpClient($serverSmtp, $port)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$smtp.Send($mes)
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 1:26 pm
by ascheevel
kenship wrote: ↑Sun Sep 04, 2022 7:56 pm
I suppose I can test it in my local machine but then if works in local machine but not on TM1 server then what's does it mean?
It rules out the obvious culprits. Briefly looking at your ps code, I don't see anything glaringly wrong. I normally don't construct powershell code right in a TI, preferring to save off to a ps1 file and then call that via TI ExecuteCommand. This allows me to ensure that the script itself is sound before trying to call via TI and if TI call doesn't work then it may be an execution policy or some other factor.
This is grasping at straws, but although I don't see any spaces in your attachment filename, you could try wrapping it in double quotes.
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 1:57 pm
by kenship
After trying a few things I got the answer.
The command has to be run in Epilog not Prolog. Not sure why because the working TI has been in Prolog all the time.
Also the attachment operator should be: -Attachments ' | ' \ " ' |pAttachment| '\"
Now it's working.
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 2:01 pm
by ascheevel
kenship wrote: ↑Tue Sep 06, 2022 1:57 pm
The command has to be run in Epilog not Prolog. Not sure why because the working TI has been in Prolog all the time.
Were you doing something else in the prolog like generating the export file you were trying to attach to email?
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 2:15 pm
by kenship
When I was testing -Attachments, it's all run from Prolog. I was testing to attach a file already in the data directory.
Now it's run from Epilog, attaching a file in the data directory. There's no asciiOutput run at all.
Kenneth
ascheevel wrote: ↑Tue Sep 06, 2022 2:01 pm
kenship wrote: ↑Tue Sep 06, 2022 1:57 pm
The command has to be run in Epilog not Prolog. Not sure why because the working TI has been in Prolog all the time.
Were you doing something else in the prolog like generating the export file you were trying to attach to email?
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 2:32 pm
by ascheevel
Great, thanks for confirming. Wanted to ensure it wasn't due to file lock issue by some other script above your ExeucteCommand.
Glad to hear you've got it working now.
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 2:33 pm
by shahhere
kenship wrote: ↑Tue Sep 06, 2022 2:15 pm
When I was testing -Attachments, it's all run from Prolog. I was testing to attach a file already in the data directory.
Now it's run from Epilog, attaching a file in the data directory. There's no asciiOutput run at all.
Kenneth
ascheevel wrote: ↑Tue Sep 06, 2022 2:01 pm
kenship wrote: ↑Tue Sep 06, 2022 1:57 pm
The command has to be run in Epilog not Prolog. Not sure why because the working TI has been in Prolog all the time.
Were you doing something else in the prolog like generating the export file you were trying to attach to email?
I am attaching the Process, VBS script and the Batch file that's used in conjunction with a separate HTML create process. The items I have will create an HTML email body along with an attachment if you need that as well.
Shahhere
Re: Powershell Snd-MailMessage Fail to Attach File
Posted: Tue Sep 06, 2022 2:52 pm
by kenship
Thanks! I'll probably stick with what I have for now. We wanna keep things as simple as possible since we don't have the admin. right to the Windows server and we can only work with whatever we can in Architect.
Kenneth
shahhere wrote: ↑Tue Sep 06, 2022 2:33 pm
kenship wrote: ↑Tue Sep 06, 2022 2:15 pm
When I was testing -Attachments, it's all run from Prolog. I was testing to attach a file already in the data directory.
Now it's run from Epilog, attaching a file in the data directory. There's no asciiOutput run at all.
Kenneth
ascheevel wrote: ↑Tue Sep 06, 2022 2:01 pm
Were you doing something else in the prolog like generating the export file you were trying to attach to email?
I am attaching the Process, VBS script and the Batch file that's used in conjunction with a separate HTML create process. The items I have will create an HTML email body along with an attachment if you need that as well.
Shahhere