Multi data types.

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

Guys please excuse me, I don’t know how to put data here with indents.

You could edit your post and Enclode the code blocks in [ pre ] and [/pre} to format it. No space between [ and pre and ].

Also -

Don’t Reply to Your Own Post Before Someone Else Does
Don’t leave the first reply to your own post. A lot of forums participants look for posts that have no replies, so that they know which posts need attention. If you reply first to your post, then your post will be less likely to be seen. You an edit your post directly for a short time after posting it, but otherwise you should wait for someone else to reply first.

Please see https://powershell.org/forum-etiquette/

The problem arises because you’e outputting a “host not pingable” row for non-reachable hosts. When you do so, you should provide a numeric value, like 0, so that your later math can work. I’m not entire sure I follow your logic, though. It looks like you’re inserting “free space” using a “DLetter” variable, which was intended for a drive letter? Anyway, keep in mind that some drives will not have a free space, like an optical drive. You may be better off writing this in a more script style than a bunch of one-liners, so you can include an If statement that can deal with drives which have no free space.