0

When I run my Powershell script from the command line, it fails, however if I copy line by line and run it in Powershell Console it runs fine.

powershell -ExecutionPolicy Bypass -File "F:\email.ps1" -FFFeatureOff

yields:

At F:\email.ps1:16 char:126 + ... ential("username", "password"); + ~~~ The string is missing the terminator: ".

At F:\email.ps1:9 char:1 + { + ~ Missing closing '}' in statement block or type definition.

Its just really odd if I open powershell window and paste the script in, it works, but running the ps1 file I get the error, even in the editor I get the same issues

Full Script:

$EmailTo = "xxxxx"
$EmailFrom = "xxxxxx"
$Subject = "LicenceKey & Instructions"
$Body = "This is an automated email"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$files=Get-ChildItem "C:\Users\alber\Desktop\LicenceKey\newuser"
Foreach($file in $files)
{
    Write-Host “Attaching File :- ” $file
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList "C:\Users\alber\Desktop\LicenceKey\newuser\$file"
    $SMTPMessage.Attachments.Add($attachment)
}
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$SMTPClient.Send($SMTPMessage)
5
  • This might be duplicate of this since in line with Attaching File you seem to have different quotes. Other possibility is, that for example password contains double quotes. They would need to be escaped using ` Commented Nov 12, 2017 at 5:25
  • 1
    Unlike almost every other Language, Powershell is fine with Smart quotes “ ”. Personally I don't use them but that's habit from the days before PS. Commented Nov 12, 2017 at 10:23
  • The script you've posted does not explain your symptoms. As an aside: better to avoid method syntax when calling cmdlets; e.g., instead of New-Object System.Net.NetworkCredential("username", "password") call New-Object System.Net.NetworkCredential -ArgumentList "username", "password". Commented Nov 12, 2017 at 12:14
  • 1
    Script file should contain BOM for UTF8 to be properly recognized. Commented Nov 12, 2017 at 16:08
  • 1
    Possible duplicate of Block of code prevents script from running, but runs interactively Commented Nov 12, 2017 at 16:15

2 Answers 2

2

The fact that James C.'s answer was accepted by the OP suggests that it was motivated by the OP's gratitude for the generally helpful, but non-specific - and ultimately potentially confusing - pointers contained in it.

Unfortunately, it falls short as an answer of general interest to future readers and is likely to cause confusion; as of this writing:

  • It contains an overtly incorrect claim:

The issue here is that the $file is outside the param for the message, moving the quote will fix that: Write-Host “Attaching File :- $file”

However, there is no problem with Write-Host “Attaching File :- ” $file, because Write-Host simply concatenates multiple arguments with spaces - no strict need for a single argument.
(As stated, PowerShell recognizes "smart quotes" (non-ASCII quotes), assuming they're properly encoded - see below).

  • It does not explain the OP's symptoms as posted.

  • It almost makes it sound as if it weren't possible to embed a literal " - or $, for that matter - in a double-quoted string due to a limitation in PowerShell, which, of course, isn't true; escaping with backticks is all that is needed: "Pa`$`$`"w0rd".


Instead, PetSerAl's helpful comments on the question presumably point to the actual answer:

  • There may be a character encoding issue, given that "smart quotes" (non-ASCII quote characters) are used, which would surface in an incorrectly encoded script file.

  • PetSerAl's answer here suggests that UTF-8-encoded script content must be saved to a file with a BOM in order for PowerShell to recognize the encoding.

  • Update: PetSerAl surmises that the "actual troublemaker is an EN DASH here:
    –ArgumentList, which, when improperly decoded: [Text.Encoding]::GetEncoding(1250).GetString([Text.Encoding]‌​::UTF8.GetBytes('–')‌​), yields –, an unpaired double quote."

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

1 Comment

Thanks for all the info guys. I found the issue. It was something with my password that was causing the issue. I changed it and now it works, cant believe I missed that. Also All this caused me to look into another question I had and found the answer. FYI: For multi line of body, $mail.BodyFormat = MailFormat.Html Then $Body = @" and end with "@
1

At F:\email.ps1:9 char:1 + { + ~ Missing closing '}' in statement block or type definition.

The issue here is that the $file is outside the param for the message, moving the quote will fix that:

Write-Host “Attaching File :- $file”

(Smart Quotes “ ” are accepted by PowerShell but aren't but most other languages, I personally avoid them to be sure.)


At F:\email.ps1:16 char:126 + ... ential("username", "password"); + ~~~ The string is missing the terminator: ".

The double quotes you're using here: ("username", "password") can cause Powershell to incorrectly interpret where the password ends if it contains certain special characters.

For example the password Pa$$"w0rd will cause issues when used in your script because of the double quote within.

It will be interpreted as "Pa$$" with the remaining w0rd" remaining ( which only contains a single quoteation mark and is not correctly terminated as a string)

To avoid this you can use single quotes instead, they are not evaluated and are interpreted as literals:

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential('username', 'password')

See about about_quoting_rules for more info on single and double quotes.

Comments

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.