Script output that will work on the console as well as with Export-Csv

前端 未结 2 1643
生来不讨喜
生来不讨喜 2021-01-24 03:40

I\'m working on a basic PowerShell script that inputs a pair of dates then gets all accounts with passwords expiring between those times. I\'d like to output the data to the con

相关标签:
2条回答
  • 2021-01-24 04:20

    Replace

    Write-Output ($accts | Format-Table | Out-String)
    

    with

    $accts
    

    That way your users can run your script any way they like, e.g.

    .\your_script.ps1 | Format-Table
    .\your_script.ps1 | Format-List
    .\your_script.ps1 | Export-Csv
    .\your_script.ps1 | Out-GridView
    ...
    

    Format-Table | Out-String converts your output to a single string whereas Export-Csv expects a list of objects as input (the object properties then become the columns of the CSV). If Export-Csv is fed a string, the only property is Length, so you get a CSV with one column and one record.

    0 讨论(0)
  • 2021-01-24 04:28
    $accts | ConvertTo-Csv | Tee -File output.csv | ConvertFrom-Csv
    
    0 讨论(0)
提交回复
热议问题