Skip to content

Windows: shell_command fails during PowerShell initialization with Win32Exception (126), while manual pwsh / powershell.exe commands work #17326

Open

Description

@hy-lmh

What version of the Codex App are you using (From “About Codex” dialog)?

Codex version: [fill in]

What subscription do you have?

plus

What platform is your computer?

OS: Windows 11

What issue are you seeing?

Title:
Windows: shell_command fails during PowerShell initialization with Win32Exception (126), while manual pwsh / powershell.exe commands work

Body:

Summary

On Windows, Codex Desktop can attach to the thread terminal successfully, but every shell_command call fails before the command actually runs.

This happens both inside the sandbox and outside the sandbox.

The failure occurs during PowerShell initialization, even for a minimal command like:

cmd /c echo TERMINAL_OK


### What steps can reproduce the bug?

Process terminated.
The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception.
   at System.Environment.FailFast(System.Runtime.CompilerServices.StackCrawlMarkHandle, System.String, System.Runtime.CompilerServices.ObjectHandleOnStack, System.String)
   at System.Environment.FailFast(System.Threading.StackCrawlMark ByRef, System.String, System.Exception, System.String)
   at System.Environment.FailFast(System.String, System.Exception)
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(System.String[], Int32)
   at Microsoft.PowerShell.ManagedPSEntry.Main(System.String[])
System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception.
 ---> System.ComponentModel.Win32Exception (126): 找不到指定的模块。
   at System.Management.Automation.Internal.SecuritySupport.GetSaferPolicy(String path, SafeHandle handle)
   at System.Management.Automation.Security.SystemPolicy.TestSaferPolicy(String testPathScript, String testPathModule)
   at System.Management.Automation.Security.SystemPolicy.GetAppLockerPolicy(String path, SafeHandle handle)
   at System.Management.Automation.Security.SystemPolicy.GetLockdownPolicy(String path, SafeHandle handle, Nullable`1 canExecuteResult)
   at System.Management.Automation.Security.SystemPolicy.GetLockdownPolicy(String path, SafeHandle handle)
   at System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy()
   at System.Management.Automation.Runspaces.InitialSessionState..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2()
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(String[] args, Int32 argc)


### What is the expected behavior?

_No response_

### Additional information

_No response_

Activity

  1. added
    app
    on Apr 10, 2026
  2. added
    bug
    windows-os
    tool-calls
    on Apr 10, 2026
  3. removed
    app
    on Apr 10, 2026
  4. rza255 commented on Sep 28, 2026

    @rza255

    I reproduced the same InitialSessionState -> GetSaferPolicy -> Win32Exception (126) failure and isolated a minimal cause/fix on my machine.

    Controls:

    • bundled pwsh outside Codex sandbox: PASS
    • native cmd.exe inside Codex elevated read-only sandbox: PASS
    • native where.exe inside the same sandbox: PASS
    • minimal pwsh startup inside that sandbox: FAIL before the requested command, with the same error 126 path

    The sanitized sandbox environment was missing SystemRoot.

    No-model discriminator:

    read-only sandbox
    windows.sandbox="elevated"
    shell_environment_policy.inherit="none"
    + SystemRoot=C:\Windows
    

    made minimal pwsh startup pass immediately.

    I then confirmed the same through a hermetic model-backed codex exec path while retaining:

    • --ignore-user-config
    • --ignore-rules
    • --ephemeral
    • --strict-config
    • --sandbox read-only
    • windows.sandbox="elevated"
    • shell_environment_policy.inherit="none"

    and adding only:

    SystemRoot=C:\Windows
    

    That probe successfully started PowerShell and read one scratch file. No Full Access or workspace-write was used.

    So for this specific GetSaferPolicy/126 failure, SystemRoot alone was the minimum environment repair on my machine. One-machine evidence, but the result was controlled: the passing exec differed from the failing one only by explicit SystemRoot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtool-callswindows-os

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions