locked
get-winevent data RRS feed

  • Question

  • Hi,

    i need to help very new to all this powershell

    i want some data from event logs, when i run this from a sheduled task the email generates with no data like the below:-

    Document: 

    IP Address: 

    printer sent to: 

    Time:

    $pcName = "PSE01D"

    $event=get-winevent Microsoft-Windows-PrintService/Operational | where {$_.eventID -eq 307} | Sort-Object index -Descending | select -first 1

    $Document = $event.Properties[8].value
    $printer = $event.Properties[7].value
    $IP = $event.Properties[5].value


    $MailSubject= "Print released 1: "
    $MailBody = "Document: " + $Document + " `r`n" + "IP Address: " + $IP + " `r`n" + "printer sent to: " + $Printer + " `r`n" + "Time: " + $Event.TimeGenerated
    $SmtpClient = New-Object system.net.mail.smtpClient
    $SmtpClient.host = "smtp.xxxxxxxxxxxxxxx"
    $MailMessage = New-Object system.net.mail.mailmessage
    $MailMessage.from = "xxxxxxxxxxxxx"
    $MailMessage.To.add("xxxxxxxxxxxxx")
    $MailMessage.IsBodyHtml = 0
    $MailMessage.Subject = $MailSubject
    $MailMessage.Body = $MailBody
    $SmtpClient.Send($MailMessage)

    any ideas ?

    TIA Gavin

    • Moved by Bill_Stewart Friday, April 28, 2017 5:51 PM This is not "scripts on demand"
    Monday, February 27, 2017 3:04 PM

All replies

  • The correct way to return the latest event is like this:

    $event = get-winevent -FilterHashtable @{Logname='Microsoft-Windows-PrintService/Operational';ID=307} -MaxEvents 1

    The correct way to send a mail message is with Send-MailMessage

    https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Utility/Send-MailMessage


    \_(ツ)_/


    • Edited by jrv Monday, February 27, 2017 5:47 PM
    Monday, February 27, 2017 5:46 PM
  • This will fully decode the embedded data:

    $event = get-winevent -FilterHashtable @{Logname='Microsoft-Windows-PrintService/Operational';ID=307} -MaxEvents 1
    [xml]$xml=$event.ToXml()
    $htmlBody = $xml.Event.UserData.DocumentPrinted | Out-String
    
    


    \_(ツ)_/

    Monday, February 27, 2017 5:54 PM