Formatting output of a script

This topic contains 6 replies, has 4 voices, and was last updated by  Liam Kemp 11 months, 1 week ago.

  • Author
    Posts
  • #54025

    Asrar
    Participant

    I have a disk space report generated as a CSV file/custom object . I want to highlight values that are less than 3 Gb in freespace. How can I achieve this

  • #54029

    Liam Kemp
    Participant

    Hi Asrar,
    Can you clarify – You have a CSV report generated by PowerShell, and you want to highlight the values when you open the CSV file. OR You have a CSV file generated by something, PowerShell or other, and you want to parse that with PowerShell to highlight the values inside the shell?

    I'm not sure if the first is possible with PowerShell, just by the nature of CSV files being text based – I don't know if they can store text colour information – my guess is no.

    The second option is definitely possible to either colour the lines in the shell.
    Or simply only return objects where the size is <3 Gb. This would be my preference, but you may have different requirements.

    $data = Import-CSV C:\drivespace.csv
    $data | Where-Object {$_.freespace -lt 3}

    Or something along those lines. Obviously you would need to adjust depending on how the size is displayed in the report.

    Liam

    • #54084

      Asrar
      Participant

      Hi Liam,

      I have the csv file generated from a Powershell script. I would like to parse this using powershell and then display in HTML or Excel.
      My code is

      if (($result = Read-host "Enter either c: or D: [default is C:]") -eq '') { $drive = "C:"} else {$drive = $result }
      
      $x = $hostnames | % { gwmi -cl win32_logicaldisk -computer $_ | ? { $_.deviceid -eq $drive } | select size, freespace, pscomputername }
      
      $outfile = "$($env:userprofile)" + "\" + "DriveSpaceStats-$(get-date -f MMddyy-hhmmss).csv"
      $output1 = $x | % { "$($_.pscomputername) $(($_.size/1.0GB).ToString("#.##")) $(($_.freespace/1.0GB).ToString("#.##"))" }
      
      $output1 -replace "\s","," | out-file $outfile
      
  • #54041

    Rob Simmers
    Participant

    There are a lot of examples of building disk space reports with Powershell. Just do a search "Powershell Disk Space Report" and you'll find a lot of variations of generating reports in HTML, Excel and everything in between.

  • #54043

    Wilfredo Perez
    Participant

    Check out the ebook on Creating HTML Reports in PowerShell

  • #54086

    Liam Kemp
    Participant

    Hi Asrar,
    In that case I would definitely suggest checking out the eBook Wilfredo suggests, you will get much better results.
    https://www.gitbook.com/book/devopscollective/creating-html-reports-in-powershell/details

    However, if you want to do something quick and dirty – I'll walk you through my steps when I'm in a hurry – you should be able to work it around your script. You do need to understand a small amount of HTML to adjust it to your needs.
    I cant post the code here though as the HTML disappears in the comments – I'll find somewhere to put it up and link to it shortly

    Liam

    • This reply was modified 11 months, 1 week ago by  Liam Kemp. Reason: HTML didnt work in pre tags
    • This reply was modified 11 months, 1 week ago by  Liam Kemp. Reason: HTML Doesnt appear
  • #54089

    Liam Kemp
    Participant

    Basically,
    1 – I start by opening a table in the $body variable
    2 – Gather the information
    3 – loop through the drives
    4 – create an object out of the drive info – I use hash tables. Don Jones has a great article on building custom objects – https://technet.microsoft.com/en-us/magazine/hh750381.aspx
    5 – Using an If statement based on the amount of free space, I add table rows to the $body variable
    6 – Close the table
    7 – Convert it to HTML and save it into a file

    You should be able to adapt this to your needs. If you have a csv file look at importing the csv file.

    Liam

    • This reply was modified 11 months, 1 week ago by  Liam Kemp.

You must be logged in to reply to this topic.