How do you get back an empty array from a function? Unless you wrap the returned value in the array initialisation syntax @(), you allways seem to get a null see worked examples below:
function Invoke-TestZero(){
Get-Process
}
function Invoke-TestOne(){
Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' }
}
function Invoke-TestTwo(){
@(Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' })
}
function Invoke-TestThree
{
[CmdletBinding()]
[OutputType([System.Object[]])]
Param ( )
End
{
@(Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' })
}
}
try{
$x = Invoke-TestZero
$x.GetType()
write-host "Test Zero Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test Zero failed`r`n" -ForegroundColor Red }
try{
$x = Invoke-TestOne
$x.GetType()
write-host "Test One Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test One failed`r`n" -ForegroundColor Red }
try{
$x = Invoke-TestTwo
$x.GetType()
write-host "Test Two Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test two failed`r`n" -ForegroundColor Red }
try{
$x = Invoke-TestOne
$x.GetType()
write-host "Test Three Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test three failed`r`n" -ForegroundColor Red }
try{
$x = Invoke-TestTwo
$x.GetType()
write-host "Test Four Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test Four failed`r`n" -ForegroundColor Red }
try{
$x = @(Invoke-TestOne)
$x.GetType()
write-host "Test five Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test Five failed`r`n" -ForegroundColor Red }
try{
$x = @(Invoke-TestTwo)
$x.GetType()
write-host "Test Six Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test Six failed`r`n" -ForegroundColor Red }
try{
$x = Invoke-TestThree
$x.GetType()
write-host "Test Seven Suceeded`r`n" -ForegroundColor Green
} catch { write-host "Test Seven failed`r`n" -ForegroundColor Red }