3

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?

0

2 Answers 2

1

You need to split your $attachments variable because it's being treated as a single file.

Instead of

Get-ChildItem $attachments

Try

Get-ChildItem ($attachments -split ',')

Sign up to request clarification or add additional context in comments.

Comments

1

You're only passing one (quoted) string to your 'attachments' array, so you're only populating attachments[0]. Try passing multiple strings:

    PS C:\Windows\system32> [array]$wrong="there,you,go"

    PS C:\Windows\system32> $wrong
    there,you,go

    PS C:\Windows\system32> $wrong[0]
    there,you,go

    PS C:\Windows\system32> $wrong[1]

    PS C:\Windows\system32> $wrong[2]

    PS C:\Windows\system32> [array]$right="there","you","go";

    PS C:\Windows\system32> $right
    there
    you
    go

    PS C:\Windows\system32> $right[0]
    there

    PS C:\Windows\system32> $right[1]
    you

    PS C:\Windows\system32> $right[2]
    go

    PS C:\Windows\system32> 

From there it should be pretty clear that you can include leading and or trailing spaces like this:

    -attachments "value 1 has a trailing space "," value 2 has a leading space"

to get:

    attachments[0]="value 1 has a trailing space ";
    attachments[1]=" value 2 has a leading space";

You mentioned you're running it from within C#, so I'll remind readers to also escape each of these quotation marks (" -> \") within the C# string that holds this command.

1 Comment

+1 for the great detailed information! Unfortunately, I can't seem to get the -attachments part to work when invoking the script with -file as the asker indicated. It does, however, work when invoking the script with -command instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.