When I am using Where-Object with “-lt” or “-gt” operators against “FreeSpace(GB in %)” I am getting non numeric values as well; which shouldn’t be. I don’t know how to have multi data types for a parameter, suggest me the corrections to remediate the below bug.
PS C:\Users\lakshv> Get-FreeDiskSpaceInfo -ComputerName LocalHost,HOSTDOESNOTEXISTS,NYWEXMBX2132,NYWEXMBX2130 -DriveLetter C
Computer FreeSpace(GB in %)
LOCALHOST 28
HostNotPingable CannotGetInfo
NYWEXMBX2132 39
NYWEXMBX2130 22
PS C:\Users\lakshv> Get-FreeDiskSpaceInfo -ComputerName LocalHost,HOSTDOESNOTEXISTS,NYWEXMBX2132,NYWEXMBX2130 -DriveLetter C | ?{$_.“FreeSpace(GB in %)” -lt 25}
Computer FreeSpace(GB in %)
NYWEXMBX2130 22
PS C:\Users\lakshv> Get-FreeDiskSpaceInfo -ComputerName LocalHost,HOSTDOESNOTEXISTS,NYWEXMBX2132,NYWEXMBX2130 -DriveLetter C | ?{$_.“FreeSpace(GB in %)” -gt 25}
Computer FreeSpace(GB in %)
LOCALHOST 28
HostNotPingable CannotGetInfo
NYWEXMBX2132 39
**********Coding Starts here *****************
Function Get-FreeDiskSpaceInfo()
{
[cmdLetBinding()]
Param(
[parameter(Mandatory=$true,ValueFromPipeline=$True,Position=0)][string[]]$ComputerName,
[parameter(Mandatory=$true,ValueFromPipeline=$True,position=1)][string]$DriveLetter,
[parameter(Mandatory=$False)][switch]$RunAs=$False
)
BEGIN
{
[int]$i=1
}
PROCESS
{
$Count=$ComputerName.count
Function FomatOutput([string]$CName,[string]$DLetter)
{
$Table | Add-Member -type NoteProperty -Name Computer -Value $CName -force
$Table | Add-Member -type NoteProperty -Name "FreeSpace(GB in %)" -Value $DLetter -force
$table
}
$Table=New-object -type PSObject
$FreeSpace=$null
$PingTest=$Null
$Drive=$("DeviceID='"+$DriveLetter+":'")
Foreach($c in $ComputerName)
{
$C=$C.ToUpper()
Write-Progress -Activity "Free-DiskSpace" -Status "In-Prgress" -CurrentOperation $("Calculating FreeDiskSpace of the host: "+$C+"\"+$DriveLetter ) -PercentComplete (([int]$i/[int]$Count)*100)
Write-Verbose $("Trying To Ping To The Host: "+$C)
$PingTest=Get-WMIObject -Query "select * from win32_pingstatus where Address='$C'"
If($PingTest.StatusCode -eq 0)
{
Write-Verbose "Host is pingable !"
Write-Verbose "Trying To Validate The DriveLetter "
$FreeSpace=Get-WmiObject -Class Win32_LogicalDisk -ComputerName $c -Filter $Drive -ErrorVariable ERR -ea 1
if(-not ($FreeSpace -eq $Null))
{
Write-Verbose "Its A Valid DriveLetter"
[int]$FSP=((($FreeSpace.FreeSpace/1GB)/($FreeSpace.Size/1GB))*100) -as [int]
FomatOutput -CName $C -DLetter $FSP
}
else
{
Write-Verbose "Its A In-Valid DriveLetter"
FomatOutput -CName $C -DLetter "InValidDriveLetter"
}
}#
else
{
write-Verbose "Host Not Pingable !"
FomatOutput -CName "HostNotPingable" -DLetter "CannotGetInfo"
}
$i++
}
}
END{}
}
#Coding ends here