I am trying to catch an error nicely, as I would do in Java. The program is similar to this:
try
{
New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)
Write-Ouput "Continue program"
}
catch
{
Write-Output "Some error"
}
The exception is never captured. In fact, the catch method executes when everything goes well. From the different errors I can get, it doesn't look like an exception is raised:
format-default : The following exception occurred while retrieving member "distinguishedName": "The server is not operational.
"
+ CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
format-default : The following exception occurred while retrieving member "distinguishedName": "The user name or password is
incorrect.
"
+ CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
How can I capture these errors in a more user-friendly way?
Maybe in this case it is not possible.
After some testing, I think the problem is that no exception is raised for some reason, despite of what the error message says. So that is why I can't capture it. I have also tried to use getType
, close
and other methods but I get the same error:
The following exception occurred while retrieving member "GetType": "The user name or password is incorrect.
"
...
This solution does not seem to work either, same behaviour.