Forum Discussion
Object reference not set to an instance of an object - when splatting to a custom function
- Aug 09, 2022
We can't really give you any definitive answers here as there's not enough information.
We'd need to know which line the error is referring to at a minimum, and if that error line is not in the example below, we'd need that area included.
All I can infer from the example is:
- It is not possible for Params to be null, since even if $User is null, a key will be added named "User" for which the corresponding object will be a null reference;
- It is possible for the "Credential" key to not be added to the hashtable but not possible for it to be null if it is added.
Still, without concise error information, that doesn't help much.
Prior to calling your custom Get-EXOEnabled function, you could simply dump the $Params to output for confirmation.
I'm not connected to Exchange at the moment but here's a "similar enough" example using a user from Azure AD instead. This shows I have two members in the hashtable, i.e. neither entry is null.
If you find the same then the issue is likely something within your function, not the example you've provided in here. But again, this is still an assumption as there's not enough code and only an imprecise error to work from.
Cheers,
Lain
We can't really give you any definitive answers here as there's not enough information.
We'd need to know which line the error is referring to at a minimum, and if that error line is not in the example below, we'd need that area included.
All I can infer from the example is:
- It is not possible for Params to be null, since even if $User is null, a key will be added named "User" for which the corresponding object will be a null reference;
- It is possible for the "Credential" key to not be added to the hashtable but not possible for it to be null if it is added.
Still, without concise error information, that doesn't help much.
Prior to calling your custom Get-EXOEnabled function, you could simply dump the $Params to output for confirmation.
I'm not connected to Exchange at the moment but here's a "similar enough" example using a user from Azure AD instead. This shows I have two members in the hashtable, i.e. neither entry is null.
If you find the same then the issue is likely something within your function, not the example you've provided in here. But again, this is still an assumption as there's not enough code and only an imprecise error to work from.
Cheers,
Lain
I had printed the values being passed to the function (before and after), and they appeared correct.
RE the error, I was struggling to pull the full error back from the function, with my limited PS experience.
As it turns out, it all works fine from another machine. I've been chasing my tail over something that I can only assume is a local PS issue.
thanks again.
- LainRobertsonAug 09, 2022Silver Contributor
No problem.
If you get curious and want to surface the underlying error without having to change your existing code, include a "throw" statement in your "catch {}" block, like this:
try { # Normal stuff ... # ... such as calling your cutsom function, etc. } catch { # Do whatever you need here before calling the throw statement to bubble the caught exception up one more level. # .. do stuff ... # .. do even more stuff ... # Finished doing stuff ... throw; # This re-throws the exception that caused us to get in here in the first place, so you can see the precise exception details such as line numbers, etc. }Cheers,
Lain
- iCer19Aug 09, 2022Copper ContributorCheers,
Yeah, I'm using throw. (But not with a semicolon)
problem I found is that my ISE and VCS are crashing when it bombs out. but even before it crashes, I don't get the error detail, only what I'm capturing as the -errorvariable.