Troubleshoot the InvokeMethodOnNull Error with PowerShell

Troubleshoot the InvokeMethodOnNull Error with PowerShell

  • Comments 2
  • Likes

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 Files\Scripts\AD Scripts\Get-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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Ed, thanks for all that you do.  Your explanations are top-notch and help the rest of us immensely.  

    Is there a way to squelch the "You cannot call a method on a null-valued expression" and continue?

    I am writing a program to check a list of patches against a list of computers.

    Sometimes, the DisplayName of an object is null.  When this occurs, an IF statement I have throws the error above.

    Well, if the DisplayName is null, there's no need to run that line or display the error, but I desire the script to continue on as if nothing happened.  

    Is there a way to squelch the error (not have it display to the screen) and continue the rest of the script?

  • Sharing as a follow-up to my earlier post.  I found technet forum which provided me the key.

    if ($objToBeUsed -ne $NULL) {#then do the rest of the operations};

    This allows me to skip any item which would be $null and, therefore, alleviates the errors going to the screen.

    Thanks again for all that you share with us.