Troubleshoot the InvokeMethodOnNull Error with PowerShell

Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting the InvokeMethodOnNull error in a Windows PowerShell script.

Hey, Scripting Guy! Question Hey, Scripting Guy! Ed…thank you for all you do for those of us who are spread so thin we can’t go deep on subjects like scripting. I have a question regarding a script you created in Use PowerShell to Create an HTML Uptime Report, called HTML_UptimeReport.ps1. I have downloaded the file, put my own list of hosts in, and it works, but there are errors regarding the $os.converttodatetime expression.

When I run the script I get an error message about not calling a method on a null-valued expression. Here is the actual error message:

You cannot call a method on a null-valued expression. At H:My FilesScriptsAD ScriptsGet-Uptime-Script.ps1:20 char:51 + uptime = (get-date) – $os.converttodatetime <<<< ($os.lastbootuptime)}}} + CategoryInfo : InvalidOperation: (converttodatetime:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

If you have a few minutes, would you mind helping me understand the reason for the error? Thank you again.

—DJ

Hey, Scripting Guy! Answer Hello DJ,

Microsoft Scripting Guy, Ed Wilson, is here. When you get an error such as “you cannot call an expression on a null value,” it means the script is attempting to do something, but another part of the script does not have any information to permit the first part of the script to work properly.

In a script that previously worked, this error is almost always a symptom of an earlier failure. What that earlier failure is could be something like permissions or configuration. In a script that is currently under development or being modified from a previously working script, this error usually arises from a problem with code that either returns information in an unexpected format or does not return any information at all.

To troubleshoot this issue in your script, you need to examine the code that populates the $os variable. Because I am calling the ConvertToDateTime method from the WMI object stored in the $os variable it means there must be a valid WMI object in the variable, or else we cannot call the method.

Upon closer examination, your error message tells me that $os does not contain a value for the LastBootUpTime property. The LastBootUpTime property is probably empty; and therefore, when the script attempts to call the ConvertToDateTime, method there is nothing there. Thus the error tells you that you cannot call an expression on a null value. It actually makes sense—there is nothing that can be converted to a DateTime object.

Your task is to find out why you are not getting a value for the LastBootUpTime property. I would troubleshoot this by using Get-WMIobject directly on the WMI class (I think it is WIN32_operating system, but as you said, I wrote this in August and I am writing this from memory) by making the query and seeking for a value in the LastBootUpTime property. You can do this simple enough by formatting as a list and using a *. The command would look like this (using aliases and querying the local computer):

gwmi win32_operatingsystem | fl *

DJ, that is all there is to troubleshooting the error message you are receiving. Join me tomorrow when I will have a guest blog by Microsoft PowerShell MVP, Jeff Wouters, as he talks about preparing for the 2013 Scripting Games.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy