Powershell html report of usage C-drive

janhoedt
janhoedt used Ask the Experts™
on
Hi,

I have an html report which shows CDrive usage of a server as red when over 80%, yello when between 70 and 80 and green
when other value.

It works fine except for some occasions, f.e. when 90% it shows green.

Hope example is clear. Please advise howto correct.

J.



#Get C-Drive percentage
		$volC = Get-WmiObject -Class win32_Volume -ComputerName $Computer -Filter "DriveLetter = 'C:'" | Select-Object -Property @{
        Name       = 'C PercentUsed'
        Expression = {
          '{0:N0}' -f  ((($_.Capacity - $_.FreeSpace ) / $_.Capacity)*100)
        }
      }
	  

#Get overviewtable	  
		$Result += [PSCustomObject] @{ 
		CDrive                = $volC.'C PercentUsed'
		...
		
		$CDriveAsPercent = "$($Entry.CDrive)%"
		
#Make the html
		Foreach ($entry in $Result)
		{
				  
		  # check C: Drive Usage
          if((($Entry.CDrive) -ge 80) -or (($Entry.CDrive) -eq 100))
          {
            $ComputerHealthOverviewFragment += "<TD bgcolor=red align=center>$($CDriveAsPercent)</TD>"
          }
          elseif((($Entry.CDrive) -ge 70) -and (($Entry.CDrive) -lt 80))
          {
            $ComputerHealthOverviewFragment += "<TD bgcolor=yello align=center>$($CDriveAsPercent)</TD>"
          }
          else
          {
            $ComputerHealthOverviewFragment += "<TD bgcolor=green align=center>$($CDriveAsPercent)</TD>"
          }
		  ...
		}
		

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It doesn't require to put a bracket when the drive is greater than 80% so remove the same.

if((($Entry.CDrive) -ge 80) -or (($Entry.CDrive) -eq 100))

replace with

if (($Entry.CDrive) -ge 80)

Author

Commented:
Ok, but wil that solve my problem?
yes this should solve the problem.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Don't get that, if the value is gt 80 ... then the -or doesn't matter indeed but why would it be the root cause?
-or (($Entry.CDrive) -eq 100) this piece of code is unnecessary, other than that I do not see anything else to be problematic.

Author

Commented:
Ok, but I do not see it as problematic on itself.
Lead SaaS Infrastructure Engineer
Commented:
I suspect there is a missing brace in the original code, although it is obviously just a partial.  (If not then, there is certainly a missing brace in your object creation step)

For what you are doing I think the logic should be simplified and you should use functions so you can call this across many computers.

Here this fixes the bracing problem, simplifies much of the logic, and creates functions to do everything.

#Function Get-C-Drive Returns system name and C Drive percentage
function Get-C-Drive {
	Param(
		$Computer=$env:computername
	)
	$volC = Get-WmiObject -Class win32_Volume -ComputerName $Computer -Filter "DriveLetter = 'C:'"
	$volCPercent = [math]::round((($VolC.Capacity - $VolC.FreeSpace)/$VolC.Capacity)*100)
	New-Object PSObject -Property @{
		System = $Computer
		CPercent = $volCPercent
	}
}

# Function OutputHTML - Evaluate CDrive percentage and output corrosponding entry for each recieved result in the object. 
function OutputHTML {
	Param(
		$Object=@()
	)
	if ($Object) {
		Foreach ($Entry in $Object) {
			$Color="green"
			write-host "if ($($Entry.CPercent) -ge $OrangeLimit) {"
			if ($($Entry.CPercent) -ge $OrangeLimit) {
				$Color="orange"
			}
			if ($($Entry.CPercent) -ge $RedLimit) {
				$Color="red"
			}
			"<TD bgcolor=$Color align=center>$($Entry.CPercent)%</TD>"
		}
	} else {
		Write-host "No Results submitted"
	}
}

Open in new window

####  Above Functions are utilized by this code:

$ComputerToQuery=$env:computername
$DriveResults=@()

$OrangeLimit = 70
$RedLimit = 80

$DriveResults += ( Get-C-Drive $ComputerToQuery )
$ComputerHealthOverviewFragment += ( OutputHTML $DriveResults)


$ComputerHealthOverviewFragment

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Glad to help!  :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial