-2

How to display less than 20 '% freespace' in red color.

PS C:>> $AllServersInfo[0].Disks | Format-Table  

DriveLetter VolumeName Capacity(GB) FreeSpace(GB) % FreeSpace  
----------- ---------- ------------ ------------- -----------  
C:                     99.66        12.85                12.9  
E:          SQL Data   200.00       44.02               22.01  
Kushal Solanki
  • 127
  • 1
  • 13
  • 1
    Possible duplicate of [Powershell - Output Color for certain results](https://stackoverflow.com/questions/46016763/powershell-output-color-for-certain-results) –  Dec 18 '18 at 13:23

1 Answers1

0

You could do that by capturing tyhe output of Format-Table in a string. Split that on newlines and work your way through each line like this:

# get the table-styled info in a string
$table = $AllServersInfo[0].Disks | Format-Table -AutoSize | Out-String

# do we have a string to work with?
if ([string]::IsNullOrWhiteSpace($table)) {
    Write-Warning 'Empty output for $AllServersInfo[0].Disks'
}
else {
    # split the string on newlines and loop through each line
    $table -split '\r?\n' | ForEach-Object {
        # do not process empty or whitespace-only strings
        if (!([string]::IsNullOrWhiteSpace($_))) {
            # get the last part of each line to capture the value for FreeSpace
            $temp = $_.TrimEnd().Substring($_.Length - 12).TrimStart()
            # test if this represents a number
            if ($temp -match '\d+(?:\.\d+)?$') {
                # and if so, check if it is below the threshold value of 20
                if ([double]$Matches[0] -lt 20) {
                    Write-Host $_ -ForegroundColor Red
                }
                else {
                    Write-Host $_
                }
            }
            else {
                Write-Host $_
            }
        }
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Hi Theo..thanks for the response. It's throwing an error out at line $temp = $_.TrimEnd().Substring($_.Length - 12).TrimStart() Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero. Parameter name: startIndex" At C:\...................................ps1:46 char:9 + $temp = $_.TrimEnd().Substring($_.Length - 12).TrimStart() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentOutOfRangeException – Kushal Solanki Dec 19 '18 at 18:11
  • Theo, i have debugged it and found that first line returned is a empty '' in $table -split '\r?\n'. $_ -eq '' -> True. Any way to rectify that? – Kushal Solanki Dec 20 '18 at 07:07
  • had to put in if($_ -eq $Null -or $_ -eq '') {} at the top which resolved my issue. Correct code is: $table -split '\r?\n' | ForEach-Object {    if($_ -eq $Null -or $_ -eq '') {} else { $temp = $_.TrimEnd().Substring($_.Length - 12).TrimStart() if ($temp -match '\d+(?:\.\d+)?$') { if ([double]$Matches[0] -lt 20) { Write-Host $_ -ForegroundColor Red } else { Write-Host $_ } } else { Write-Host $_ } } } – Kushal Solanki Dec 20 '18 at 07:35
  • @KushalSolanki Thanks for pointing this out. I have updated the code to handle empty or whitespave-only strings now. – Theo Dec 20 '18 at 09:26