Write-Output Question

Hi guys. I am trying to write a simple script to send me a monthly report on a couple of things. I’d like to keep it nice and simple and include the items I want reported back to me in one CSV report. I’m running the following commands:

$old= get-date
$oldtime= $old.AddMonths(-3)

Write-output “AD USERS”; get-aduser -filter {lastlogondate -lt $oldtime} |select name | ? {$_.distinguishedname -notlike “ou=service accounts”} | Move-ADObject -TargetPath ‘ou=service accounts,ou=users,ou=organization,dc=***,dc=local’ | Export-Csv C:\test.csv -NoTypeInformation

Write-Output “AD COMPUTERS” ; get-adcomputer -filter {lastlogondate -lt $oldtime} | select name | Export-Csv C:\test.csv -NoTypeInformation -Append -Force
Write-Output “DHCP INFO” ;Get-DhcpServerv4Statistics -ComputerName ctl01 | select TotalScopes, TotalAddresses, AddressesInUse,AddressesaAailable,PercentageInUse | Export-Csv c:\test.csv -NoTypeInformation -append -Force

The problem is that although the script runs and exports what I have requested in to the csv, the write-outputs that I have tried to include are Excluded from the report. So all the information is on top of each other instead of being separated by the Header that I want separating the different info I have collected. Can anyone advice on how I can change the script to include the write-output in my report to separate the info collected?

A csv file is a data table. Field names in the top row, the rest of the rows are data. There is no concept of titles or subtitles or grouping. Just a simple table of data.

Sounds like you want to build a report and I’d say building an html report would probably be ideal. Have you looked into convertto-html at all? In particular the -Fragment and -precontent parameters.

Fragment lets you build individual tables, and PreContent lets you add something like AD Users before the table. Then you can inject your tables into a string that has all the html tags and send that to out-file.

Thank you, Craig. I hadn’t thought of that. To be honest, I haven’t used convertto-html at all yet, but I will definitely give it a try and see if it can give me the results I’m after. Thank you again for the advice.

Hi Craig. Thanks again for the advice. Convertto-html got me exactly what I wanted and will actually make reporting much easier, and more presentable. Thank you again for the advice.