powershell - ErrorVariable not working with null or empty agrument -


i having issues -errorvariable capturing error if argument null or empty. here example:

ps> get-process $svc  handles  npm(k)    pm(k)      ws(k) vm(m)   cpu(s)     id processname           -------  ------    -----      ----- -----   ------     -- -----------               374      14     5180       4996    49             596 svchost    ps> get-process $noprocess -errorvariable myerr  get-process : cannot validate argument on parameter 'name'. argument  null or empty. supply argument not null or empty , try  command again. @ line:1 char:13 + get-process $noprocess -errorvariable myerr +             ~~~~~~~~~~     + categoryinfo          : invaliddata: (:) [get-process], parameterbinding     validationexception     + fullyqualifiederrorid : parameterargumentvalidationerror,microsoft.power     shell.commands.getprocesscommand   ps> $myerr  # <----errorvariable did not capture error  ps> $error[0] get-process : cannot validate argument on parameter 'name'. argument null or empty. supply argument not null or empty , try command again. 

any thoughts why -errorvariable not work in case? have tested on other cmdlets , powershell 3.0/4.0 , still see same results.

the error didn't show in error variable because error wasn't thrown get-process. thrown command processor trying validate parameters run get-process. get-process never got run, never had chance put variable.

possible workaround:

try{      get-process $noprocess -errorvariable myerr    }  catch { $myerr = $error[0] }   $myerr get-process : cannot validate argument on parameter 'name'. argument null or empty. provide argument not null or  empty, , try command again. @ line:3 char:17 +     get-process $noprocess -errorvariable myerr +                 ~~~~~~~~~~     + categoryinfo          : invaliddata: (:) [get-process], parameterbindingvalidationexception     + fullyqualifiederrorid : parameterargumentvalidationerror,microsoft.powershell.commands.getprocesscommand 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -