Best Practice Recommendation – Disk Space Report

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Kevin Kirkpatrick Kevin Kirkpatrick 3 years, 6 months ago.

  • Author
    Posts
  • #8975
    Profile photo of Kevin Kirkpatrick
    Kevin Kirkpatrick
    Participant

    Preface: I'm new to the forum and currently breaking out of the 'beginner' PowerShell user space and have been going through and re-writing any/all of my current scripts to include more information and/or increase their automation/function.

    That said, after hours of hacking around, I just finished re-writing a server disk space report that I had previously used, which initially did what I wanted it to do: read in a list of FQDN server names, retrieved disk space information using WMI, did a few calculations and spit the results to a .CSV file. I needed some more versatility and the original sort on the 'Percent Free Space' column wasn't working. What I have, today, is a script that relies more on variables, exports the report as an HTML file with some custom formatting and the sorting is fixed.

    The major hang up I ran into was figuring out the sorting. Long story, short, I figured out I was trying to sort an array, which wasn't working. After looking at the entire thing, when I was done, it just seems like there may be a more efficient way of getting what I want. I was thinking the use of functions may be useful, but I'm not knowledgable enough with the more advanced methods PowerShell has to offer.

    Any input or criticism is welcome!

    Attached is a generalized copy of the script and what the output looks like.

    I figured I'd post here, rather than the 'Q & A' section; feel free to move wherever it may be necessary.

    Thanks ahead.

  • #8997
    Profile photo of Art Beane
    Art Beane
    Member

    It seems like your script generates correct results — and it looks to be well organized and short. There are only 2 changes that *I* would make to it (neither of which change your logic):

    1. I'd sort by SystemName first and then by PercentFree(%)
    2. Several of the PowerShell separators ("|" and "," included) ignore following whitespace, so it's not necessary to use a backtick to break to a new line. I try to avoid using backticks whenever possible because they are usually hard to see in most of the IDEs (plus it's extra typing and a pain to remove when reformatting code).

  • #9818
    Profile photo of Kevin Kirkpatrick
    Kevin Kirkpatrick
    Participant

    Art, input was much appreciated. I did end up adding a second sort on the server name. Thanks again.

  • #9840
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In terms of "Best practices", here's what I would do differently:

    • Don't set $ErrorActionPreference to "SilentlyContinue". Sweeping errors under the rug and not handling them is a bad habit. You don't have to handle them with code, but the person running the script should at least see them.
    • Write-Output probably isn't the right choice for those lines. Write-Output sends objects down the pipeline; it's a way for functions or scripts to produce that sort of output. When you want to write text to the screen (for status updates, etc), but NOT send extra string objects down the pipeline, you can choose from Write-Host, Write-Verbose, or Write-Progress, depending on what type of visibility and functionality you want.
    • As Art mentioned, try to avoid backticks as the line continuation character wherever possible. It's a hard character to see in some fonts, and if you put any spaces after the backtick, you get errors.
    • Your use of spaces around operators is a bit inconsistent. $Servers = (gc "\\uncpath\to\servers.txt") is easier to read than $Sender="ServerDiskReport@company.com" , for example, and $PercentFree=@{name="PercentFree(%)";Expression={[int](($_.FreeSpace/$_.Size)*100)}} is really a headache to read. Try to write code that is clear and easy to read; add spaces or blank lines wherever they seem to enhance that quality.

    Other than that, it's minor stuff. Some people will say you should use single-quoted strings anytime you don't intend to perform variable expansion or embed special characters like "`r`n". That's personal preference, so long as you're aware of the differences between single and double quoted strings, and use them both correctly.

    When you're setting up your $html string at the beginning of the script, you could just use a multi-line string or here-string, rather than doing all the concatenation. With a short string, you won't notice the performance difference, but doing operations like $string += "Some new text" can get very expensive as the string gets long.

    Here's an example of what it might look like using a here-string. Check out Get-Help about_Quoting_Rules for more details on here-strings; it also has the skinny on single quotes versus double quotes.

    $html = @"
    
    "@
    

    All of that stuff aside, the code looks like it works, which is mostly what counts at the end of the day. Nice job!

  • #11734
    Profile photo of Windows LiveUser36
    Windows LiveUser36
    Participant

    Can this be edited so it can run against all servers in an estate and create a web page which lists the Servers with least % of space remaining.

    I'm new to powershell so this is way above my level at the present time, so any help much appreciated.

  • #11757
    Profile photo of Kevin Kirkpatrick
    Kevin Kirkpatrick
    Participant

    LiveUser36, can you be a bit more specific? Are you looking for servers with the least amount of free space on a particular partition, only? Do you only want to show servers and associated partitions that are below a certain threshold of percent free? The report, as written, sorts all servers that you feed it by the least amount of remaining free space on a given partition.

    Thanks.

You must be logged in to reply to this topic.