{"id":89,"date":"2023-06-16T16:19:50","date_gmt":"2023-06-16T23:19:50","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/?p=89"},"modified":"2023-06-16T16:19:50","modified_gmt":"2023-06-16T23:19:50","slug":"how-to-determine-if-your-runcommand-script-failed","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/how-to-determine-if-your-runcommand-script-failed\/","title":{"rendered":"How to determine if your Runcommand script failed"},"content":{"rendered":"<p>Today, we&#8217;re going to cover what can be a maddening issue in just determining whether your script failed.<\/p>\n<p>Let&#8217;s start with a very simple operation. Open a Powershell window and run the following:<\/p>\n<pre style=\"padding-left: 40px;\">PS C:\\&gt; this is an error\r\n\r\nthis: The term 'this' is not recognized as a name of a cmdlet, function, script file, or executable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.<\/pre>\n<p>This shouldn&#8217;t be rocket science here. It&#8217;s very clear that &#8220;this is an error&#8221; is, in fact, an error. So, with this universal fact determined, let&#8217;s run it on an Azure VM.<\/p>\n<pre>Set-AzVmRunCommand -ResourceGroupName $rg -VMName $vm -Location $loc -RunCommandName \"MyRuncommand\" -SourceScript \"this is an error\"<\/pre>\n<p>And now let&#8217;s see the result.<\/p>\n<pre>Get-AzVMRunCommand -ResourceGroupName $rg -VMName $vm -RunCommandName \"MyRuncommand\" -Expand \"instanceView\" | select-object *\r\n\r\nAsyncExecution : False\r\nErrorBlobUri :\r\nId : {removed}\r\nInstanceViewEndTime : 16.06.2023 22:55:11\r\n<strong>InstanceViewError : this : The term 'this' is not recognized as the name of a cmdlet, function, script file, or operable program. Check<\/strong>\r\n<strong>the spelling of the name, or if a path was included, verify that the path is correct and try again.<\/strong>\r\n<strong>At C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandHandlerWindows\\2.0.5\\Downloads\\Script_MyRuncommand_0.ps1:1 char:1<\/strong>\r\n<strong>+ this is an error<\/strong>\r\n<strong>+ ~~~~<\/strong>\r\n<strong>+ CategoryInfo : ObjectNotFound: (this:String) [], CommandNotFoundException<\/strong>\r\n<strong>+ FullyQualifiedErrorId : CommandNotFoundException<\/strong>\r\n\r\nInstanceViewExecutionMessage :\r\n<strong>InstanceViewExecutionState : Succeeded<\/strong>\r\n<strong>InstanceViewExitCode : 0<\/strong>\r\nInstanceViewOutput :\r\nInstanceViewStartTime : 16.06.2023 22:55:08\r\nInstanceViewStatuses :\r\nLocation : westus2\r\nName : MyRuncommand\r\nOutputBlobUri :\r\nParameter :\r\nProtectedParameter :\r\nProvisioningState : Succeeded\r\nRunAsPassword :\r\nRunAsUser :\r\nSourceCommandId :\r\nSourceScript : this is an error\r\nSourceScriptUri :\r\nTag : Microsoft.Azure.PowerShell.Cmdlets.Compute.Models.Api20210701.ResourceTags\r\nTimeoutInSecond : 0\r\nType : Microsoft.Compute\/virtualMachines\/runCommands<\/pre>\n<p>Note that you need to pass in -expand &#8220;instanceView&#8221; to see the actual error. Without this, you&#8217;ll receive the following.<\/p>\n<pre>AsyncExecution : False\r\nErrorBlobUri :\r\nId : {removed}\r\nInstanceViewEndTime :\r\n<strong>InstanceViewError :<\/strong>\r\nInstanceViewExecutionMessage :\r\nInstanceViewExecutionState :\r\n<strong>InstanceViewExitCode :<\/strong>\r\nInstanceViewOutput :\r\nInstanceViewStartTime :\r\nInstanceViewStatuses :\r\nLocation : westus2\r\nName : MyRuncommand\r\nOutputBlobUri :\r\nParameter :\r\nProtectedParameter :\r\nProvisioningState : Succeeded\r\nRunAsPassword :\r\nRunAsUser :\r\nSourceCommandId :\r\nSourceScript : this is an error\r\nSourceScriptUri :\r\nTag : Microsoft.Azure.PowerShell.Cmdlets.Compute.Models.Api20210701.ResourceTags\r\nTimeoutInSecond : 0\r\nType : Microsoft.Compute\/virtualMachines\/runCommands<\/pre>\n<p>This leads one to believe everything worked, when it didnt&#8217;t.<\/p>\n<p>So, the first lesson of Runcommand error viewing is: <strong>always check the instanceView<\/strong>!<\/p>\n<p>Of course, most of us are keen enough to not write scripts like this. Instead, we&#8217;re probably going to detect problems and throw, so let&#8217;s try that.<\/p>\n<pre>Set-AzVmRunCommand -ResourceGroupName $rg -VMName $vm -Location $loc -RunCommandName \"Myruncommand\" -SourceScript \"throw 'blah'\"\r\n\r\nGet-AzVMRunCommand -ResourceGroupName $rg -VMName $vm -RunCommandName \"MyRuncommand\" -Expand \"instanceView\" | select-object *\r\n\r\nAsyncExecution : False\r\nErrorBlobUri :\r\nId : {removed}\r\nInstanceViewEndTime : 16.06.2023 23:02:52\r\n<strong>InstanceViewError : blah<\/strong>\r\n<strong>At C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandHandlerWindows\\2.0.5\\Downloads\\Script_MyRuncommand_1.ps1:1 char:1<\/strong>\r\n<strong>+ throw 'blah'<\/strong>\r\n<strong>+ ~~~~~~~~~~~~<\/strong>\r\n<strong>+ CategoryInfo : OperationStopped: (blah:String) [], RuntimeException<\/strong>\r\n<strong>+ FullyQualifiedErrorId : blah<\/strong>\r\n\r\nInstanceViewExecutionMessage :\r\n<strong>InstanceViewExecutionState : Failed<\/strong>\r\n<strong>InstanceViewExitCode : 1<\/strong>\r\nInstanceViewOutput :\r\nInstanceViewStartTime : 16.06.2023 23:02:51\r\nInstanceViewStatuses :\r\nLocation : westus2\r\nName : MyRuncommand\r\nOutputBlobUri :\r\nParameter :\r\nProtectedParameter :\r\nProvisioningState : Succeeded\r\nRunAsPassword :\r\nRunAsUser :\r\nSourceCommandId :\r\nSourceScript : throw 'blah'\r\nSourceScriptUri :\r\nTag : Microsoft.Azure.PowerShell.Cmdlets.Compute.Models.Api20210701.ResourceTags\r\nTimeoutInSecond : 0\r\nType : Microsoft.Compute\/virtualMachines\/runCommands<\/pre>\n<p>So, again we can see the error in the instanceView, but something else has changed. the instanceViewExitCode is now 1 and instanceViewExecutionState is failed! But in the previous case it was 0 and success, even though we still had an error. The issue is in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/understanding-non-terminating-errors-in-powershell\/\">terminating vs non-terminating errors in Powershell<\/a>.<\/p>\n<p>The following are other options to return an error.<\/p>\n<pre>Set-AzVmRunCommand -ResourceGroupName $rg -VMName $vm -Location $loc -RunCommandName \"Myruncommand\" -SourceScript \"write-error 'blah' -ErrorAction Stop\"<\/pre>\n<p>This results in the following.<\/p>\n<pre>InstanceViewError : C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandHandlerWindows\\2.0.5\\Downloads\\Script_MyRuncommand_2.ps1 : blah\r\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandHandlerWindows\\2.0.5\\Downloads\\Script_MyRuncommand_2.ps1:1 char:1\r\n+ write-error 'blah' -ErrorAction Stop\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException\r\n+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Script_MyRuncommand_2.ps1\r\n\r\nInstanceViewExecutionMessage :\r\nInstanceViewExecutionState : Failed\r\nInstanceViewExitCode : 1<\/pre>\n<p>You can also change the exit code with<\/p>\n<pre>Set-AzVmRunCommand -ResourceGroupName $rg -VMName $vm -Location $loc -RunCommandName \"Myruncommand\" -SourceScript \"exit 42\"<\/pre>\n<p>Note that in this case there is no value for Error.<\/p>\n<pre>InstanceViewError :\r\nInstanceViewExecutionMessage :\r\nInstanceViewExecutionState : Failed\r\nInstanceViewExitCode : 42<\/pre>\n<p>So, I hope this provides some guidance on checking for RunCommand failures in your automation.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we&#8217;re going to cover what can be a maddening issue in just determining whether your script failed. Let&#8217;s start with a very simple operation. Open a Powershell window and run the following: PS C:\\&gt; this is an error this: The term &#8216;this&#8217; is not recognized as a name of a cmdlet, function, script file, [&hellip;]<\/p>\n","protected":false},"author":103696,"featured_media":90,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6],"tags":[5,11,9,10],"class_list":["post-89","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-runcommand","tag-azure","tag-failures","tag-runcommand","tag-vmextensions"],"acf":[],"blog_post_summary":"<p>Today, we&#8217;re going to cover what can be a maddening issue in just determining whether your script failed. Let&#8217;s start with a very simple operation. Open a Powershell window and run the following: PS C:\\&gt; this is an error this: The term &#8216;this&#8217; is not recognized as a name of a cmdlet, function, script file, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/users\/103696"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media\/90"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}