I'm always struggling with methods resulting in the error: You cannot call a method on a null-valued expression.
When the lastlogondate of the user is empty the method ToShortDateString() is giving the error.
Another example is with Get-MailboxStatistics for Exchange and the TotalItemSize you can convert to MB of GB by using TotalItemSize.Value.ToMB()
What is the best approach to intercept this?
Hi. Keep in mind I'm not the only on here, though ;). If you ask for me specifically you may keep some other helpful folks from offering an answer!
You'll want to use a Try...Catch block (help about_try*). For cmdlets that throw a non-terminating error, you have to use -ErrorAction to convert to a terminating exception, since you can only trap those. For a method, set $ErrorActionPreference='Stop' before trying the method, and $ErrorActionPreference='Continue' after the method, to put it back to normal.
Error handling is a pretty beefy topic, but hopefully that gets you started.
Result should look like this:
You must be logged in to reply to this topic.