Powershell html report of usage C-drive

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

janhoedtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sunil ChauhanConsultant Commented:
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)
janhoedtAuthor Commented:
Ok, but wil that solve my problem?
Sunil ChauhanConsultant Commented:
yes this should solve the problem.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

janhoedtAuthor 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?
Sunil ChauhanConsultant Commented:
-or (($Entry.CDrive) -eq 100) this piece of code is unnecessary, other than that I do not see anything else to be problematic.
janhoedtAuthor Commented:
Ok, but I do not see it as problematic on itself.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Glad to help!  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.