I'm trying to pass several parameters to the following script in the $attachments parameter, and this seems to work well with a single attachment, or when there are no spaces in the path, however when passing more than one value to the array when one or more have spaces in the path the script fails with an error indicating that it can't find the file specified.
To increase the difficulty, this is being launched from c#. I've tried several permutations of the command below. The entire command line looks something like this:
powershell.exe -executionpolicy unrestricted -file "c:\program files (x86)\App\Samples\SendEmail.ps1" -attachments "c:\Program Files (x86)\App\Logs\1234.log,c:\Program Files (x86)\App\Logs\12345.log"
The script:
param(
[array]$attachments,
[string]$from = '[email protected]',
[array]$to = '[email protected]',
[string]$subject = 'Threshold Exceeded',
[string]$body = 'Testing. Please ignore.',
[string]$smtpServer = 'testsmptserver.com'
)
$mailParams = @{
From = $from
To = $to
Subject = $subject
Body = $body
SMTPServer = $smtpServer
}
if ($attachments)
{
Get-ChildItem $attachments | Send-MailMessage @mailParams
}
else
{
Send-MailMessage @mailParams
}
Has anyone encountered something like this? How did you resolve it?