none
Power shell script error RRS feed

  • Question

  • Hey Everyone,

    I am using a batch file to launch a power shell script and the script fails with the error below. 

    Missing ')' in method call.
    At C:\Installed Software\RestartMicros.ps1:12 char:13
    + $msg.To.Add( <<<< â?oposadmin@wgresorts.comâ??)
        + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], Paren
       tContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

    But when I run the same commands in power shell it works fine. This is the line in my batch file where I call it.

    PowerShell -NoProfile -ExecutionPolicy Bypass -File RestartMicros.ps1

    And this is the power shell script. 

    $comp =$env:COMPUTERNAME
    $filepath ="C:\Installed Software\Reports\"
    $filename =$filepath+=$comp+="RestartMicros.err"
    $smtpServer = “smtp.company.com”

    $msg = new-object Net.Mail.MailMessage
    $att = new-object Net.Mail.Attachment($filename)
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)


    $msg.From = “Micros_Reports@company.com”
    $msg.To.Add(“posadmin@company.com”)
    $msg.Subject = “Restart Micros Error”
    $msg.Body = “This restaurant had a problem stopping the Micros DB and was not restarted”
    $msg.Attachments.Add($att)

    $smtp.Send($msg)

    I literally copied this from another script that runs every night and changed the file name and path, report name and the outlook address and now it fails. 

    • Moved by Bill_Stewart Friday, March 15, 2019 5:39 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, January 16, 2019 7:10 PM

All replies

  • I notice that not all of your double quote characters are the same. Besides the normal " character, I see slanted "fancy" quotes, that should be avoided.

    Which is line 12 in your copy of the script? That is the line that raises the error.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, January 16, 2019 7:42 PM
  • I strongly recommend using PowerShell to do this.  The MSG primitives can be troublesome and add nothing to what PowerShell can natively do especially if you are not a programmer or experienced scripter.  Attachments are a big issue because they lock the file and repeated attempts to access the same attachment will create issues.

    $filename = 'C:\Installed Software\Reports\{0}RestartMicros.err' -f $env:COMPUTERNAME
    $mailsplat = @{
        SmtpServer = 'smtp.company.com'
        From = 'Micros_Reports@company.com'
        To = 'posadmin@company.com'
        Subject = 'Restart Micros Error'
        Body = 'This restaurant had a problem stopping the Micros DB and was not restarted'
        Attachments = $filename
    }
    
    Send-MailMessage @mailsplat


    \_(ツ)_/


    • Edited by jrv Wednesday, January 16, 2019 7:57 PM
    Wednesday, January 16, 2019 7:56 PM
  • Also you should avoid using double quotes unless you really want an expandable string.  Single quotes do no expand and can be safely posted to the web and copied avoiding smart quotes.


    \_(ツ)_/

    Wednesday, January 16, 2019 7:59 PM
  • It would be best to test for the file before trying to send it.

    $filename = 'C:\Installed Software\Reports\{0}RestartMicros.err' -f $env:COMPUTERNAME
    $mailsplat = @{
        SmtpServer = 'smtp.company.com'
        From = 'Micros_Reports@company.com'
        To = 'posadmin@company.com'
        Subject = 'Restart Micros Error'
        Body = 'This restaurant had a problem stopping the Micros DB and was not restarted'
        Attachments = $filename
    }
    if(Test-Path $filename){
        Send-MailMessage @mailsplat
    } else {
        Write-Warning "Not found - $filename"
    }


    \_(ツ)_/

    Wednesday, January 16, 2019 8:01 PM