ansible-collections/ansible.windows

win_powershell output broken when using Powershell 7

UntiIted opened this issue · 3 comments

SUMMARY

Running the win_powershell task with executable: pwsh.exe specified results in broken output, with "Failed to compile C# code" among the error output.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_powershell

ANSIBLE VERSION
ansible [core 2.16.0]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.12/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.12.0 (main, Oct 21 2023, 17:44:38) [GCC 9.4.0] (/usr/bin/python3.12)
  jinja version = 3.1.2
  libyaml = False
COLLECTION VERSION
Collection      Version
--------------- -------
ansible.windows 2.2.0
CONFIGURATION
CONFIG_FILE() = None
OS / ENVIRONMENT

Host is WSL running Ubuntu 20.04.6 LTS
Target is Windows Server 2019 Datacenter
Issues only happen with Powershell 7, not 5

STEPS TO REPRODUCE

A simple powershell task is enough as long as it runs Powershell 7

- name: write output
  ansible.windows.win_powershell:
    script: |
      Write-Output $PSVersionTable
    executable: pwsh.exe
EXPECTED RESULTS

Task runs and returns successfully with no visible issues.

ACTUAL RESULTS

Task runs and returns successfully, however the output is broken and there's a clear indication of some issue in the backend. I can run with extra verbosity but I think the normal output gives all the necessary information.
You can see the C# errors and the broken output with an unknown list inserted in it.

{
    "changed": true,
    "debug": [],
    "error": [
        {
            "category_info": {
                "activity": "",
                "category": "OperationStopped",
                "category_id": 14,
                "reason": "InvalidOperationException",
                "target_name": "",
                "target_type": ""
            },
            "error_details": null,
            "exception": {
                "help_link": null,
                "hresult": -2146233079,
                "inner_exception": null,
                "message": "Failed to compile C# code:\r\n(1,1): hidden CS8019: Unnecessary using directive.\r\nusing Microsoft.Win32.SafeHandles;\r\n^\r\nerror CS0041: Unexpected error writing debug information -- 'The version of Windows PDB writer is older than required: 'diasymreader.dll''",
                "source": null,
                "type": "System.InvalidOperationException"
            },
            "fully_qualified_error_id": "Failed to compile C# code:\r\n(1,1): hidden CS8019: Unnecessary using directive.\r\nusing Microsoft.Win32.SafeHandles;\r\n^\r\nerror CS0041: Unexpected error writing debug information -- 'The version of Windows PDB writer is older than required: 'diasymreader.dll''",
            "output": "Failed to compile C# code:\r\n(1,1): hidden CS8019: Unnecessary using directive.\r\nusing Microsoft.Win32.SafeHandles;\r\n^\r\nerror CS0041: Unexpected error writing debug information -- 'The version of Windows PDB writer is older than required: \r\n'diasymreader.dll''\r\nAt line:259 char:17\r\n+ ...             throw [InvalidOperationException]\"Failed to compile C# co ...\r\n+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : OperationStopped: (:) [], InvalidOperationException\r\n    + FullyQualifiedErrorId : Failed to compile C# code:\r\n(1,1): hidden CS8019: Unnecessary using directive.\r\nusing Microsoft.Win32.SafeHandles;\r\n^\r\nerror CS0041: Unexpected error writing debug information -- 'The version of Windows PDB writer is older than required: \r\n'diasymreader.dll''\r\n \r\n",
            "pipeline_iteration_info": [],
            "script_stack_trace": "at <ScriptBlock>, <No file>: line 259\r\nat <ScriptBlock>, <No file>: line 30",
            "target_object": null
        }
    ],
    "host_err": "",
    "host_out": "",
    "information": [],
    "output": [
        {
            "Capacity": 122,
            "Length": 52,
            "MaxCapacity": 2147483647
        },
        {
            "Capacity": 122,
            "Length": 88,
            "MaxCapacity": 2147483647
        },
        {
            "Capacity": 122,
            "Length": 88,
            "MaxCapacity": 2147483647
        },
        {
            "Capacity": 122,
            "Length": 88,
            "MaxCapacity": 2147483647
        },
        {
            "Capacity": 122,
            "Length": 89,
            "MaxCapacity": 2147483647
        },
        {
            "GitCommitId": "7.4.0",
            "OS": "Microsoft Windows 10.0.17763",
            "PSCompatibleVersions": [
                "1.0",
                "2.0",
                "3.0",
                "4.0",
                "5.0",
                "5.1",
                "6.0",
                "7.0"
            ],
            "PSEdition": "Core",
            "PSRemotingProtocolVersion": {
                "Build": -1,
                "Major": 2,
                "MajorRevision": -1,
                "Minor": 3,
                "MinorRevision": -1,
                "Revision": -1
            },
            "PSVersion": {
                "BuildLabel": null,
                "Major": 7,
                "Minor": 4,
                "Patch": 0,
                "PreReleaseLabel": null
            },
            "Platform": "Win32NT",
            "SerializationVersion": {
                "Build": 0,
                "Major": 1,
                "MajorRevision": 0,
                "Minor": 1,
                "MinorRevision": 1,
                "Revision": 1
            },
            "WSManStackVersion": {
                "Build": -1,
                "Major": 3,
                "MajorRevision": -1,
                "Minor": 0,
                "MinorRevision": -1,
                "Revision": -1
            }
        }
    ],
    "result": {},
    "verbose": [],
    "warning": []
}

Thanks, I wonder if the issue is due to a change in pwsh 7.4 which uses dotnet 8. Will have to look into the Add-Type error and see how we can fix this.

I am weirdly not able to replicate the error but I believe #572 will fix this issue for you. I'm not sure if potentially there are some env vars that might be cause a problem here or whether a custom compiler is higher in the PATH causing these errors to be emitted.

In that case I'll wait until I can test with a fixed version and report afterwards