Formatting output

This topic contains 3 replies, has 3 voices, and was last updated by  JC 1 year, 4 months ago.

  • Author
    Posts
  • #37050

    JC
    Participant

    My script makes multiple checks on remote machines like whether certain services are running, tasks are enabled, settings are set, groups exist. This time I would need to create some sort of report of the output rather than just display it on my screen . The output is of variable length hence difficult to format in a csv. Possibly html would be better as on consecutive runs I'll be only looking for deltas. My question is do I need to follow every line with something like convertto-html | out-file or is there a better way to consolidate the output and then pass it to out-file in the last step?

  • #37052

    David Senior
    Participant

    Gather all of your output in an array and then pipe your array to the ConvertTo-htlm

    Simple example

    $Arr = New-Object -TypeName System.Collections.Arraylist
    
    Get-Service | foreach {$Arr.Add($_)} | Out-Null
    
    Get-Process | foreach {$Arr.Add($_)} | Out-Null
    
    $Arr | ConvertTo-Html | Out-File -FilePath $home\Documents\Report.html
    
  • #37055

    Don Jones
    Keymaster

    Have a look at our HTML Reporting ebook, from the Resources menu here. It covers doing some fairly complex, multi-part reports.

    I'll disagree with David on the array – an array is a poor way to do this, and gets slower the larger you make the array. Outputting custom objects to the pipeline, in a modular, function-based approach (which is how the ebook proceeds) is a much more PowerShell-native pattern.

  • #37099

    JC
    Participant

    HTML reporting, Networking Guide, Error handling and even DSC... wow! Funny I never checked that section. Thanks for pointing me there although the wife won't be happy I'll skip a couple of weekends out 😉

You must be logged in to reply to this topic.