none
Issues properly formatting SMTPEventConsumer message body in PowerShell

    Question

  • I have a script I am developing to monitor the Win32_PrintJob class on our print servers for jobs over a certain size. Part of the solution involves using the SMTPEventConsumer to fire off an email message. The trouble I am running into is that standard escape characters don't seem to work in my message string. The code snippet is below

    $instanceConsumer_SMTP.Message = "Job Owner: %TargetInstance.Owner%`nJob Source: %TargetInstance.HostPrintQueue%`nJob Size(bytes): %TargetInstance.Size% `nJob ID: %TargetInstance.JobID% `nJob Queue: %TargetInstance.Name%"

    What is returned in the body of the email is this:

    From: WMI@host <WMI@host> Sent: Friday, January 4, 2019 11:08 AM To: notification@contoso.com Subject: Win32_PrintJob: Large Print Job detected Job Owner: employ


    What I am expecting is more like:

    From: WMI@host <WMI@host> 
    Sent: Friday, January 4, 2019 11:08 AM
    To: notification@contoso.com
    Subject: Win32_PrintJob: Large Print Job detected
    
    Job Owner: employee23
    Job Source: ComputerA
    Job Size(bytes): 1234459
    Job ID: 435
    Job Queue: PrinterQ
    Any thoughts on what escape sequence might work, or what I am doing wrong with the escape sequences I have used? I have tried many iterations of it with varying amounts of success, but nothing returns quite how I would like. Thanks for any advice!

    Friday, January 4, 2019 7:43 PM

Answers

  • Alright, updating to PowerShell 5.1 did the trick. Exact same code that was generating a truncated SMTP message in 4.0 is now displaying the proper formatting after the update. Thanks again for your work around code and the guidance you provided. I appreciate your time and efforts.

    Tuesday, January 8, 2019 12:18 AM

All replies

  • Without you code it is not possible to know why this is happening.


    \_(ツ)_/

    Friday, January 4, 2019 8:10 PM
  • This is a better way to format your message:

    $instanceConsumer_SMTP.Message = @'
        Job Owner: %TargetInstance.Owner%
        Job Source: %TargetInstance.HostPrintQueue%
        Job Size(bytes): %TargetInstance.Size%
        Job ID: %TargetInstance.JobID%
        Job Queue: %TargetInstance.Name%
    '@


    \_(ツ)_/

    Friday, January 4, 2019 8:12 PM
  • I have tried here strings with both single and double quotes, same truncated results with each variation. Here is the relevant code.

    ## Creating a new event filter
    $instanceFilter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance()
    $instanceFilter.QueryLanguage = 'WQL'
    $instanceFilter.Query = "SELECT * FROM __InstanceCreationEvent WITHIN .1 WHERE targetInstance ISA 'Win32_PrintJob'"
    $instanceFilter.Name = 'Win32_PrintJob_Creation_Filter'
    $instanceFilter.EventNamespace = 'root\cimv2'
    $result = $instanceFilter.Put()
    $newFilter = $result.Path
    
    ## Creating a new SMTPEventConsumer for notification
    $instanceConsumer_SMTP = ([wmiclass]"\\.\root\subscription:SMTPEventConsumer").CreateInstance()
    $instanceConsumer_SMTP.Name = 'Win32_PrintJob_Creation_SMTPConsumer'
    $instanceConsumer_SMTP.ToLine = 'Notification@contoso.com'
    $instanceConsumer_SMTP.Subject = 'Win32_PrintJob: Large Print Job detected'
    $instanceConsumer_SMTP.Message = @"
    Job Owner: %TargetInstance.Owner%
    Job Source: %TargetInstance.HostPrintQueue%
    Job Size(bytes): %TargetInstance.Size%
    Job ID: %TargetInstance.JobID%
    Job Queue: %TargetInstance.Name%
    "@
    $instanceConsumer_SMTP.SMTPServer = 'smtpserver.contoso.com'
    $result = $instanceConsumer_SMTP.Put()
    $newConsumer_SMTP = $result.Path
    
    ## Bind filter and SMTPEventConsumer
    $instanceBinding = ([wmiclass]"\\.\root\subscription:__FilterToConsumerBinding").CreateInstance()
    $instanceBinding.Filter = $newFilter
    $instanceBinding.Consumer = $newConsumer_SMTP
    $result = $instanceBinding.Put()
    $newBinding_SMTP = $result.Path
    
    ##Removing WMI Subscriptions using [wmi] and Delete() Method
    #([wmi]$newFilter).Delete()
    #([wmi]$newConsumer_SMTP).Delete()
    #([wmi]$newBinding_SMTP).Delete()

    Friday, January 4, 2019 8:15 PM
  • Quotes have no effect.  Either work the same here.

    The issue is odd.  I see no reason why the values fail to populate.  Unfortunately I cannot run this as I do not have an SMTP server that can accept unauthenticated messages.


    \_(ツ)_/

    Friday, January 4, 2019 8:30 PM
  • Try this.

    $msg = @'
    Job Owner: %TargetInstance.Owner%
    Job Source: %TargetInstance.HostPrintQueue%
    Job Size(bytes): %TargetInstance.Size%
    Job ID: %TargetInstance.JobID%
    Job Queue: %TargetInstance.Name%
    '@
    $enc = [System.Text.Encoding]::ASCII
    $bytes = $enc.GetBytes($msg)
    $msgAscii = $msg.ToString()
    $instanceConsumer_SMTP.Message = $msgAscii


    \_(ツ)_/

    Friday, January 4, 2019 11:04 PM
  • Another oddity, when I run my code from my print server (2012R2) I get the weird email output that prompted this post. When I run it on my Win10 workstation the email message is formatted exactly as expected.  I will do some further testing, perhaps the behavior is explained by the different versions of PowerShell executing the code.
    Saturday, January 5, 2019 12:10 AM
  • The issue was the version of PowerShell the code was being run from. When the code was run from PS 4.0 (2012R2) it generated an email similar to the first example in my original post. When I run the code form PS 5.1 the email is formatted exactly as expected. I wanted to update this post with my findings in case anybody else stumbled across this issue. Thanks!
    Saturday, January 5, 2019 12:30 AM
  • Did you try the workaround I posted.  The issue may be the Net cast of Unicode to Ascii.  It can also be an issue with a character set mismatch.

    Best to upgrade to 5.1.  It is much better than 4.


    \_(ツ)_/

    Saturday, January 5, 2019 12:34 AM
  • I appreciate your input on this and your example code! It was a perspective I hadn't considered and was certainly worth a shot! Unfortunately it resulted in the exact same behavior as my previous testing. It worked when I run it from PS 5.1, it didn't work when run from PS 4.0.

    I think I am going to chalk this one up to something weird in PS 4.0

    Saturday, January 5, 2019 12:40 AM
  • If it doesn't work then it doesn't work.  I wonder what could be different?


    \_(ツ)_/

    Saturday, January 5, 2019 12:49 AM
  • Me too! It would be interesting to understand the underlying problem, but certainly beyond my expertise to decipher. I am kind of surprised actually, when I bounced this off one of my senior techs he asked about the PS versioning. My head was so deep in WMI land I hadn't even considered that as a possibility. Anyway, thanks again for your help. Have a good weekend.
    Saturday, January 5, 2019 12:56 AM
  • I have a great suspicion that it is really the version of the Net Framework.  On another system try first installing Net 4.7 or newer to see if the issue goes away.


    \_(ツ)_/

    Saturday, January 5, 2019 1:02 AM
  • I had an opportunity today to play around with this a little bit more today. On my test box I am running a version greater than 4.7 and I still experience the truncating issues. I will upgrade PS to version 5.1 and test again.
    Monday, January 7, 2019 11:52 PM
  • Alright, updating to PowerShell 5.1 did the trick. Exact same code that was generating a truncated SMTP message in 4.0 is now displaying the proper formatting after the update. Thanks again for your work around code and the guidance you provided. I appreciate your time and efforts.

    Tuesday, January 8, 2019 12:18 AM
  • You should post your findings on UserVoice as a bug so other see it and, possibly MS will fix it.


    \_(ツ)_/

    Tuesday, January 8, 2019 12:28 AM
  • Thanks for the link! I will go report it right now.
    Tuesday, January 8, 2019 12:33 AM