try catch problem

I am trying to add some logic with catching the errors and doing something later on that error. But i can seem to cature the error. Can someone see what i am doing wrong?

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {

$sdate= get-date -UFormat %m/%d/%Y
$sdate
Try 
{
$procs = get-process -name dbbackex|select id, name, starttime|Where-Object {$_.starttime -NotContains $sdate}
}
Catch
{
Write-Host $_.Exception.Message
}
$procs|ft

}

 

Many common errors in PS are considered non-terminating and won’t trigger a try/catch block by default. You need to tell the command to treat it as terminating so the try/catch block picks it up, using the -ErrorAction Stop parameter. :slight_smile:

 

That did the trick, thanks!