0

I have a bigger script that sends emails when disk of various computer is under 20%. Now I have one computer that is under 20%, but has 40 GB, so I want to add a second filter to only send the email when the FreeSpace < 10 GB.

I can't get the additional filter to work, even though it seems like what I'm doing is logical.

[decimal]$thresholdPercentFree = 20
[decimal]$thresholdFreespaceGB = 10 

Write-Host "TEST1" 
 Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.FreeSpace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdPercentFree } 

Write-Host "TEST2" 
 Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.FreeSpace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdPercentFree -and [decimal]($_.FreeSpace/1gb) -lt $thresholdFreespaceGB} 

Output: The desire is that Test2 will not show any servers because the FreeSpace > 10.

TEST1
__SERVER       : MyServerName
DriveType      : 3
VolumeName     : 
Name           : C:
Size (Gb)      : 249.66
FreeSpace (Gb) : 46.18
PercentFree    : 18.50

TEST2
__SERVER       : MyServerName
DriveType      : 3
VolumeName     : 
Name           : C:
Size (Gb)      : 249.66
FreeSpace (Gb) : 46.18
PercentFree    : 18.50
NealWalters
  • 17,197
  • 42
  • 141
  • 251

1 Answers1

0

Fix based on @AdminOfThings comments above. I changed Freespace (GB) to Freespace_GB and used that (and took out the divide in the second test).

Write-Host "TEST1" 
 Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace_GB';e={"{0:n2}" -f ($_.FreeSpace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdPercentFree } 

Write-Host "TEST2" 
 Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace_GB';e={"{0:n2}" -f ($_.FreeSpace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdPercentFree -and [decimal]($_.FreeSpace_Gb) -lt $thresholdFreespaceGB} 
NealWalters
  • 17,197
  • 42
  • 141
  • 251
  • 4
    On a side note: you should filter the objects before selecting. That is, use `Where-Object` before `Select-Object`. That way you will discard any unwanted items before you then generate your custom objects. For small collections, this will likely make no difference, but if you do this with a very large collection where you only require a small subset, then your current approach will be much less efficient since you are generating huge numbers of objects you then immediately throw away. – boxdog Oct 29 '19 at 22:17