Aug 09 2022 12:12 AM
I've written a module for a specific O365 function - onboarding users to mobile in InTune.
The module works, but I needed to add the ability for users to specify alternative credentials to run the module.
To achieve this, I'm using the splat method, which enables me to add credentials in the hash table
Unfortunately, I'm getting error "Object reference not set to an instance of an object" with this method and cannot understand why.
The parameters appear to be passed through OK as I can print them to host, but the command doesn't like it.
Hopefully it's something obvious.
FYI, if I rem out @Params and use the $User variable, it will work.
$Params = @{
User = $User
}
if ($Null -ne $Credential) {$Params.add('Credential', $Credential)}
Try {
get-EXOEnabled @Params #Custom function
} Catch {...}
Aug 09 2022 12:52 AM
Solution
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:
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
Aug 09 2022 04:22 AM
Aug 09 2022 04:53 AM
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
Aug 09 2022 08:19 AM
May 19 2024 03:14 AM
Aug 09 2022 12:52 AM
Solution
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:
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