Skip to content
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

win_powershell: Cannot run script containing Powershell 7 operators #452

Closed
iamperson347 opened this issue Nov 29, 2022 · 2 comments · Fixed by #459
Closed

win_powershell: Cannot run script containing Powershell 7 operators #452

iamperson347 opened this issue Nov 29, 2022 · 2 comments · Fixed by #459

Comments

@iamperson347
Copy link

SUMMARY

When attempting to utilize powershell 7 operators with win_powershell module, it appears the module fails due to a parsing error (since the "parent" process runs in PS 5).

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_powershell

ANSIBLE VERSION
ansible [core 2.12.8]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible-venv/5.10.0-core-2.12.8/lib/python3.8/site-packages/ansible
  ansible collection location = /opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/collections
  executable location = /opt/ansible-venv/5.10.0-core-2.12.8/bin/ansible
  python version = 3.8.0 (default, Dec  9 2021, 17:53:27) [GCC 8.4.0]
  jinja version = 3.1.2
  libyaml = True

COLLECTION VERSION
# /opt/ansible-venv/5.10.0-core-2.12.8/lib/python3.8/site-packages/ansible_collections
Collection      Version
--------------- -------
ansible.windows 1.10.0

# /opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/collections/ansible_collections
Collection      Version
--------------- -------
ansible.windows 1.11.1

CONFIGURATION
COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/collections']
DEFAULT_CALLBACK_PLUGIN_PATH(/etc/ansible/ansible.cfg) = ['/opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/plugins/callback']
DEFAULT_FILTER_PLUGIN_PATH(/etc/ansible/ansible.cfg) = ['/opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/plugins/filter']
DEFAULT_FORKS(/etc/ansible/ansible.cfg) = 20
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = ['/etc/ansible/inventory/running']
DEFAULT_LOG_PATH(/etc/ansible/ansible.cfg) = /var/log/ansible/ansible.log
DEFAULT_MODULE_PATH(/etc/ansible/ansible.cfg) = ['/opt/ansible-venv/5.10.0-core-2.12.8/share/ansible/plugins/modules']
HOST_KEY_CHECKING(env: ANSIBLE_HOST_KEY_CHECKING) = False

OS / ENVIRONMENT

Ubuntu 18 (Controller)
Windows Server 2019 (with Powershell 7.2.7 installed)

STEPS TO REPRODUCE
    - name: Run PowerShell script in PowerShell 7
      ansible.windows.win_powershell:
        script: |
          $($var.Prop ?? "<none>")
        executable: pwsh.exe
      register: pwsh_output
EXPECTED RESULTS

win_powershell can run powershell 7 features when paramater executable is set to pwsh.exe

In the example above, I would assume "output" would have ""

ACTUAL RESULTS
"exception": "Exception calling \"Create\" with \"1\" argument(s): \"At line:1 char:13\r\n+ $($var.Prop ?? \"<none>\")\r\n+             ~~\nUnexpected token '??' in expression or statement.\"\nAt line:517 char:1\r\n+ $scriptAst = [ScriptBlock]::Create($module.Params.script).Ast\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException\n    + FullyQualifiedErrorId : ParseException\r\n\r\nScriptStackTrace:\r\nat <ScriptBlock>, <No file>: line 517\r\n",
@jborean93
Copy link
Collaborator

The problem here is

$scriptAst = [ScriptBlock]::Create($module.Params.script).Ast
$supportsShouldProcess = $false
if ($scriptAst -is [Management.Automation.Language.ScriptBlockAst] -and $scriptAst.ParamBlock.Attributes) {
$supportsShouldProcess = [bool]($scriptAst.ParamBlock.Attributes |
Where-Object { $_.TypeName.Name -eq 'CmdletBinding' } |
Select-Object -First 1 |
ForEach-Object -Process {
$_.NamedArguments | Where-Object {
$_.ArgumentName -eq 'SupportsShouldProcess' -and ($_.ExpressionOmitted -or $_.Argument.ToString() -eq '$true')
}
})
}
. The module is parsing the code to check if [CmdletBinding(SupportsShouldProcess)] is set as this is used to check whether to run this in check mode with -WhatIf. What we can probably do is catch the parse error and do a more rudimentary check for this attribute and add a documentation caveat around it.

@iamperson347
Copy link
Author

Awesome - thank you for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants