I am having issues with -ErrorVariable capturing an error if the argument is null or empty. Here is an 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'. The argument is
null or empty. Supply an argument that is not null or empty and then try the
command again.
At 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'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
Any thoughts as to why -ErrorVariable would not work in this case? I have tested it on other cmdlets and PowerShell 3.0/4.0 and still see the same results.