0

I am a little new to PowerShell and am having trouble properly formatting the output of a script I am writing.

The first line of the snippet returns an array of objects of what I'm looking to format (Event ID 372 in the Printer's Event log). The second line prints out the properties I need for the object in position 3:

$error_372 = Get-WinEvent -LogName 'Microsoft-Windows-PrintService/Admin'| ? {$_.Id -eq '372'}
$error_372[3].Properties[0,1,2,4]

The output of the above returns the following:

Value                                                     
-----                                                     
Print Document                                            
Ahmet                                                     
Canon MP560 series Printer                                
131072

I want all of the objects returned in a format like this:

Document Name        User Name        Printer Name        Document Size 
-------------        ---------        ------------        -------------
Print Document        Ahmet           Canon MP560...      131072
 yada yada            yada             yada                yada

Ultimately, I need this in a CSV but figured I would start with trying to print it on the screen properly.

4 Answers 4

1

You can also use the New-Object cmdlet (instead of multiple Add-Member calls):

Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-PrintService/Admin'; Id=372} | Foreach-Object{

    New-Object PSObject -Property @{
        DocumentName = $_.Properties[0].Value
        UserName = $_.Properties[1].Value       
        PrinterName = $_.Properties[2].Value
        DocumentSize = $_.Properties[4].Value
    }

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

2 Comments

I like this method better but the return outputs look like: System.Diagnostics.Ev... rather than the actual data. Do you know what I am doing wrong?
I appended ".value" after $_.Properties[0] and all is good. Thanks!
1

You can construct the objects needed like so:

$error_372 | % { 
    $out =  new-object psobject;
    $out | add-member -Type noteproperty -Name "Document Name" -Value $_.Properties[0];
    $out | add-member -Type noteproperty -Name "User Name" -Value $_.Properties[1]
    $out | add-member -Type noteproperty -Name "Printer Name e" -Value $_.Properties[2]
    $out | add-member -Type noteproperty -Name "Document Size" -Value $_.Properties[4] -PassThru 
}

Note the -PassThru on the last Add-Member line which returns the object constructed to the pipeline.

2 Comments

When I try doing what you suggested, this is the output:
you need to catch it with a pipeline and save, or create an array first $array = @() and add this line at the end of the foreachloop $array += $out (instead of -passthru).
1

try this:

$error_372 | % { 
    $out =  new-object psobject;
    $out | add-member -Type noteproperty -Name "Document Name" -Value ($_.Properties[0] | select -expa value)
    $out | add-member -Type noteproperty -Name "User Name" -Value ($_.Properties[1] | select -expa value)
    $out | add-member -Type noteproperty -Name "Printer Name e" -Value ($_.Properties[2]| select -expa value)
    $out | add-member -Type noteproperty -Name "Document Size" -Value ($_.Properties[4] | select -expa value) -PassThru 
}

or better:

$error_372 | % { 
    $out =  new-object psobject;
    $out | add-member -Type noteproperty -Name "Document Name" -Value ($_.Properties[0].value)
    $out | add-member -Type noteproperty -Name "User Name" -Value ($_.Properties[1].value)
    $out | add-member -Type noteproperty -Name "Printer Name e" -Value ($_.Properties[2].value)
    $out | add-member -Type noteproperty -Name "Document Size" -Value ($_.Properties[4].value) -PassThru 
}

Comments

-1

You can create a custom PSobject with the properties you need(documentname, username ...) for each event. Fill in the properties with the values from event-objects. Collect them in an array and export to csv. Creation of custom objects is explained here

$array = @()

$error_372 | % { 
$out =  new-object psobject;
$out | add-member -NotePropertyName "Document Name" -NotePropertyValue ($_.Properties[0].value)
$out | add-member -NotePropertyName "User Name" -NotePropertyValue ($_.Properties[1].value)
$out | add-member -NotePropertyName "Printer Name" -NotePropertyValue ($_.Properties[2].value)
$out | add-member -NotePropertyName "Document Size" -NotePropertyValue ($_.Properties[4].value)
$array += $out
}

$array | export-csv -path events.csv

EDIT: -Noteproperty... parameters requires PS3.0 it seems.

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.