We can use the count() method to count the returned number of results when we have more than one result returned, but if the function returns only one or no result at all the count() method cannot be used because the returned object will not be an array;
The solution is to use an array variable in which to save the result; This way you will allways have an arry even if it have only one or no elements in it;
function Retrive-Services{ Get-Service -DisplayName w* }
$var = @(Retrive-Services)
if ($var.count -ge 1){
$var.gettype()
"array-ul have " + $var.Count + " records."
foreach ($v in $var){
$v.name # here you will ussualy do some work;
}
}
elseif ($var.count -eq 0){
$var.gettype()
"array-ul have zero records"
}
$v.name # here you will ussualy do some work;
}
}
elseif ($var.count -eq 0){
$var.gettype()
"array-ul have zero records"
}