{"id":3945,"date":"2013-03-27T00:01:00","date_gmt":"2013-03-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/27\/troubleshoot-the-invokemethodonnull-error-with-powershell\/"},"modified":"2013-03-27T00:01:00","modified_gmt":"2013-03-27T00:01:00","slug":"troubleshoot-the-invokemethodonnull-error-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/troubleshoot-the-invokemethodonnull-error-with-powershell\/","title":{"rendered":"Troubleshoot the InvokeMethodOnNull Error with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting the I<strong>nvokeMethodOnNull<\/strong> error in a Windows PowerShell script.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! Ed&#8230;thank you for all you do for those of us who are spread so thin we can&#8217;t go deep on subjects like scripting. I have a question regarding a script you created in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/07\/use-powershell-to-create-an-html-uptime-report.aspx\" target=\"_blank\">Use PowerShell to Create an HTML Uptime Report<\/a>, called HTML_UptimeReport.ps1. I have <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Use-PowerShell-to-create-1d9e4831\" target=\"_blank\">downloaded the file<\/a>, put my own list of hosts in, and it works, but there are errors regarding the <strong>$os.converttodatetime<\/strong> expression.\nWhen 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:<\/p>\n<p style=\"padding-left: 30px\">You cannot call a method on a null-valued expression. At H:My FilesScriptsAD ScriptsGet-Uptime-Script.ps1:20 char:51 + uptime = (get-date) &#8211; $os.converttodatetime &lt;&lt;&lt;&lt; ($os.lastbootuptime)}}} + CategoryInfo : InvalidOperation: (converttodatetime:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull\nIf you have a few minutes, would you mind helping me understand the reason for the error? Thank you again.\n&mdash;DJ\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello DJ,\nMicrosoft Scripting Guy, Ed Wilson, is here. When you get an error such as &ldquo;you cannot call an expression on a null value,&rdquo; 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.\nIn 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.\nTo troubleshoot this issue in your script, you need to examine the code that populates the <strong>$os<\/strong> variable. Because I am calling the <strong>ConvertToDateTime<\/strong> method from the WMI object stored in the <strong>$os<\/strong> variable it means there must be a valid WMI object in the variable, or else we cannot call the method.\nUpon closer examination, your error message tells me that <strong>$os<\/strong> does not contain a value for the <strong>LastBootUpTime<\/strong> property. The <strong>LastBootUpTime<\/strong> property is probably empty; and therefore, when the script attempts to call the <strong>ConvertToDateTime<\/strong>, method there is nothing there. Thus the error tells you that you cannot call an expression on a null value. It actually makes sense&mdash;there is nothing that can be converted to a <strong>DateTime<\/strong> object.\nYour task is to find out why you are not getting a value for the <strong>LastBootUpTime<\/strong> property. I would troubleshoot this by using <strong>Get-WMIobject<\/strong> 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 <strong>LastBootUpTime<\/strong> property. You can do this simple enough by formatting as a list and using <strong>a *<\/strong>. The command would look like this (using aliases and querying the local computer):<\/p>\n<p style=\"padding-left: 30px\">gwmi win32_operatingsystem | fl *\nDJ, 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.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting the InvokeMethodOnNull error in a Windows PowerShell script. &nbsp;Hey, Scripting Guy! Ed&#8230;thank you for all you do for those of us who are spread so thin we can&#8217;t go deep on subjects like scripting. I have a question regarding a script you created in Use PowerShell [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,134,45],"class_list":["post-3945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-troubleshooting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting the InvokeMethodOnNull error in a Windows PowerShell script. &nbsp;Hey, Scripting Guy! Ed&#8230;thank you for all you do for those of us who are spread so thin we can&#8217;t go deep on subjects like scripting. I have a question regarding a script you created in Use PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3945"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}