1

I created a form using PowerShell Studio 2014 Trial with two controls i.e. a command button & a text button. Below is the code for click event on button1-

$button1_Click={

$results=Get-WinEvent -FilterHashtable @{ LogName = "application"; StartTime = "10/30/2014 12:00:01 AM"; EndTime = "10/30/2014 11:59:59 PM" }

$textbox1.Text=$results
}

When I execute this, it does not give the expected output containing logs.

It displays- "System.Diagnostics.Eventing.Reader.EventLogRecord"

Form

However, if I run-

Get-WinEvent -FilterHashtable @{ LogName = "application"; StartTime = "10/30/2014 12:00:01 AM"; EndTime = "10/30/2014 11:59:59 PM" }

on PowerShell ISE, it shows all logs.

Cœur
  • 37,241
  • 25
  • 195
  • 267
S R
  • 657
  • 3
  • 10
  • 21
  • It is outputting what you are asking it to do which is the object `System.Diagnostics.Eventing.Reader.EventLogRecord` PowerShell console handles object output better with cmdlets like `Format-Default`. What do you want in the textbox? All of that information? Then you would need to format it as a string first. – Matt Nov 02 '14 at 15:52
  • @Matt- Yes, I want all that information. How do I format it as a string? Please help. Thanks – S R Nov 02 '14 at 15:55

1 Answers1

2

Like I said in my comment your getting the output you requested. PowerShell console handles object output with built-in cmdlets like Out-Default. You said you wanted all that information but I'm guessing you wanted what was display by default. Therefore I would make results the following

$results = Get-WinEvent -FilterHashtable @{ LogName = "application"; StartTime = "10/30/2014 12:00:01 AM"; EndTime = "10/30/2014 11:59:59 PM" } | Select provider,timecreated,id,leveldisplayname,message | Format-Table | Out-String -Width 1024

Use Select to get only what we want. Change this as you see fit. Use Format-Table to make fixed width output then pipe it into Out-String

Matt
  • 45,022
  • 8
  • 78
  • 119
  • @Matt- How can I format the data. As of now it is coming as - http://s27.postimg.org/vei7njmab/display_submit.png I want it to look properly formatted as in proper column wise (like it comes in PowerShell ISE). – S R Nov 03 '14 at 18:10
  • 1
    @SanchitRasiya that is happeneing since PowerShell is using a fixed width font like Courier. Change the font of your text box to a fixed width one and you will be fine. – Matt Nov 03 '14 at 23:09
  • @Matt- I tried changing the font to Lucida Console. It looks a bit good. Check- http://postimg.org/image/zeopudzw3/ How can I increase the size of column so that I can see full value of TimeCreated? It is trimming the time value. – S R Nov 04 '14 at 03:10
  • 1
    @SanchitRasiya try playing with the `-Width` of `Out-String`. Please remember that if you have other questions please either search SO for solutions or post new questions. Thanks – Matt Nov 04 '14 at 03:24