• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 119
  • Last Modified:

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

0
janhoedt
Asked:
janhoedt
  • 3
  • 3
  • 2
1 Solution
 
Sunil ChauhanExpertise in Exchange Server, Office 365 & Powershell ScriptingCommented:
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)
0
 
janhoedtAuthor Commented:
Ok, but wil that solve my problem?
0
 
Sunil ChauhanExpertise in Exchange Server, Office 365 & Powershell ScriptingCommented:
yes this should solve the problem.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
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?
0
 
Sunil ChauhanExpertise in Exchange Server, Office 365 & Powershell ScriptingCommented:
-or (($Entry.CDrive) -eq 100) this piece of code is unnecessary, other than that I do not see anything else to be problematic.
0
 
janhoedtAuthor Commented:
Ok, but I do not see it as problematic on itself.
0
 
Ben Personick (Previously QCubed)Lead Network 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

1
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help!  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now