Title: Calling action scriptblock for registered Powershel.Exiting EngineEvent throws NullReferenceException if script block contains 'finally' block · Issue #11869 · PowerShell/PowerShell · GitHub
Open Graph Title: Calling action scriptblock for registered Powershel.Exiting EngineEvent throws NullReferenceException if script block contains 'finally' block · Issue #11869 · PowerShell/PowerShell
X Title: Calling action scriptblock for registered Powershel.Exiting EngineEvent throws NullReferenceException if script block contains 'finally' block · Issue #11869 · PowerShell/PowerShell
Description: Steps to reproduce create file code.ps1 with content: Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Write-Host "Start ExitHandler" try{ try{} catch{} finally{ } } catch{ # print all exceptions (with all inner) # cod...
Open Graph Description: Steps to reproduce create file code.ps1 with content: Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Write-Host "Start ExitHandler" try{ try{} catch{} finally{ } } catch{ # pri...
X Description: Steps to reproduce create file code.ps1 with content: Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Write-Host "Start ExitHandler" try{ try{} catch{} finally{ } } ca...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/11869
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Calling action scriptblock for registered Powershel.Exiting EngineEvent throws NullReferenceException if script block contains 'finally' block","articleBody":"\r\n\r\n# Steps to reproduce\r\n1. create file `code.ps1` with content:\r\n\r\n```\r\nRegister-EngineEvent -SourceIdentifier Powershell.Exiting -Action { \r\n Write-Host \"Start ExitHandler\"\r\n try{ \r\n try{}\r\n catch{}\r\n finally{ } \r\n }\r\n catch{\r\n # print all exceptions (with all inner)\r\n # code from the book \"Windows Powershell in action 3rd edition, page 534\"\r\n $ErrorRecord = $Error[0]\r\n $out=\"\"\r\n $out+= $($ErrorRecord | Format-List -Property * -Force | out-string)\r\n $out+= $($ErrorRecord.InvocationInfo | Format-List -Property * | out-string)\r\n $Exception = $ErrorRecord.Exception\r\n for ($depth = 0; $Exception -ne $null; $depth++)\r\n {\r\n $out+= $(\"$depth\" * 80)\r\n $out+= $($Exception | Format-List -Property * -Force | out-string)\r\n $Exception = $Exception.InnerException\r\n }\r\n Write-Host \"$out\"\r\n }\r\n Write-Host \"End ExitHandler\"\r\n}\r\nexit \r\n```\r\n2. Create second file `run_code.ps1` with content (in the same directory):\r\n```\r\npwsh \"${PsScriptRoot}/code.ps1\"\r\n```\r\n\r\n3. run file `run_code.ps1`\r\n\r\n# Expected behavior\r\nUser should see two printed lines\r\n```\r\nStart ExitHandler\r\nEnd ExitHandler\r\n```\r\n\r\n# Actual behavior\r\nUser sees exception details similar to this:\r\n```\r\nPSMessageDetails :\r\nException : System.NullReferenceException: Object reference not set to an instance of an object.\r\n at System.Management.Automation.ExceptionHandlingOps.SuspendStoppingPipeline(ExecutionContext context)\r\n at System.Management.Automation.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)\r\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\nTargetObject :\r\nCategoryInfo : OperationStopped: (:) [], NullReferenceException\r\nFullyQualifiedErrorId : System.NullReferenceException\r\nErrorDetails :\r\nInvocationInfo : System.Management.Automation.InvocationInfo\r\nScriptStackTrace : at \u003cScriptBlock>, ...\\code.ps1: line 7\r\nPipelineIterationInfo : {}\r\n\r\n\r\n\r\nMyCommand :\r\nBoundParameters : {}\r\nUnboundArguments : {}\r\nScriptLineNumber : 7\r\nOffsetInLine : 5\r\nHistoryId : -1\r\nScriptName : ...\\code.ps1\r\nLine : Write-Host \"Start ExitHandler\"\r\n\r\nPositionMessage : At ...\\code.ps1:7 char:5\r\n + Write-Host \"Start ExitHandler\"\r\n + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nPSScriptRoot : ...\r\nPSCommandPath : ...\\code.ps1\r\nInvocationName :\r\nPipelineLength : 0\r\nPipelinePosition : 0\r\nExpectingInput : False\r\nCommandOrigin : Internal\r\nDisplayScriptPosition :\r\n\r\n\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000\r\nMessage : Object reference not set to an instance of an object.\r\nData : {System.Management.Automation.Interpreter.InterpretedFrameInfo}\r\nInnerException :\r\nTargetSite : Boolean SuspendStoppingPipeline(System.Management.Automation.ExecutionContext)\r\nStackTrace : at System.Management.Automation.ExceptionHandlingOps.SuspendStoppingPipeline(ExecutionContext context)\r\n at System.Management.Automation.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)\r\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\nHelpLink :\r\nSource : System.Management.Automation\r\nHResult : -2147467261\r\n\r\n```\r\n\r\n# Environment data\r\n\r\n\u003c!-- provide the output of $PSVersionTable -->\r\n\r\n```\r\nName Value\r\n---- -----\r\nPSVersion 6.2.4\r\nPSEdition Core\r\nGitCommitId 6.2.4\r\nOS Microsoft Windows 10.0.18362\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\nThe funny thing is that if you comment out line 6 `finally{ }` the code will work.\r\nI was trying to find some more information about what kind of code can/cannot appear in Powershell.Exiting action scriptblock but did not find any restrictions etc.","author":{"url":"https://github.com/grzegorz-wolszczak","@type":"Person","name":"grzegorz-wolszczak"},"datePublished":"2020-02-17T22:17:32Z","url":"https://github.com/PowerShell/PowerShell/issues/11869"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1de98974-63b5-5341-04a4-009050b83253 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B260:298C67:127639:1A14C8:6AFD4DA0 |
| html-safe-nonce | 082163ac22594590f040b204a362cdda62e8b2c548b0b9d8a8704529285fc663 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjYwOjI5OEM2NzoxMjc2Mzk6MUExNEM4OjZBRkQ0REEwIiwidmlzaXRvcl9pZCI6IjgxMzYwNTI2MjkwMjYxOTY4OTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 27361126309bb868cb35c9e5afe42820d520ffdc5621394bd93e03366885beca |
| hovercard-subject-tag | issue:566540207 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/11869/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b6cab148818c60d8c89cc0278cd21553da8e01f8055aee7ac7dca31a55ba573/PowerShell/PowerShell/issues/11869 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b6cab148818c60d8c89cc0278cd21553da8e01f8055aee7ac7dca31a55ba573/PowerShell/PowerShell/issues/11869 |
| og:image:alt | Steps to reproduce create file code.ps1 with content: Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Write-Host "Start ExitHandler" try{ try{} catch{} finally{ } } catch{ # pri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | grzegorz-wolszczak |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width