New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a null reference exception when 'Runspace.DefaultRunspace' is null #21344
Conversation
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow great catch, LGTM!
|
The test |
|
I am receiving this error with 7..4.2 version, are there any relevance here. There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: |
|
@msekkappan Nah no relevance, the exception you're getting is normal and expected. For more info I'd recommend sharing your code and the exception in the PowerShell community discord. |


PR Summary
Fix #21253
Fix a null reference exception when
Runspace.DefaultRunspace == null.This issue happens when the
WDAC Auditfeature is enabled on the client machine.Enter-PSSessionwill push aRemoteRunspaceto theRunspaceRefinConsoleHost, then the next input loop will use theRemoteRunspaceto create a pipeline. The call toLogWDACAuditMessagehappens when creating that pipeline, and at that point, we are still in the starting thread ofpwsh, not aPipeline Execution Thread, so the local-thread-storage propertyRunspace.DefaultRunspaceisnull.The fix is to check on
Runspace.DefaultRunspace?.Id == 1. This PR also does some cleanup of the code.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.