function get-checkdrive($server) { write-verbose "check drive -started" $check= 'D:\' # no escape $testobj=get-wmiobject win32_volume -filter "DriveLetter= 'D:'" -computername $server $caption= $testobj.Caption if($check -contains ($caption) ) { $TotalGB=@{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/1073741824),2)}} $FreeGB=@{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace /1073741824),2)}} $test=$testobj|Select Name , $TotalGB,$FreeGB write-verbose "check drive -Completed" return ($test) } else { write-output "$check doesnt exist" } }
I have wriitten this script to check whether the D DRIVE exist or not. the script works fine but the problem is if D doesn't exist it doesn;t print drive doesnt exist instead print
@{Name=D: , Freespace(GB)=0 , Capacity(GB)=0). please help.