AnsiballZ/PowerShell ParseError when using win_scheduled_task

Hello,

I’m attempting to create a scheduled task using win_scheduled_task but getting a ParseError. It looks like in order to execute the ansible task, AnsiballZ generates a Powershell script, but when trying to execute the script, Powershell doesn’t see it as valid.

This happens even with the example task from win_scheduled_task’s docs:

---
- name: Test win_scheduled_task
  hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Create a task to open 2 command prompts as SYSTEM
      community.windows.win_scheduled_task:
        name: TaskName
        description: open command prompt
        actions:
          - path: cmd.exe
            arguments: /c hostname
          - path: cmd.exe
            arguments: /c whoami
        triggers:
          - type: daily
            start_boundary: '2017-10-09T09:00:00'
        username: SYSTEM
        state: present
        enabled: true

Here’s the output:

___@___:/mnt/c/Users/___/cuas/cuas-ansible$ ansible-playbook playbooks/install-archiver.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Test win_scheduled_task] ******************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [127.0.0.1]

TASK [Create a task to open 2 command prompts as SYSTEM] ****************************************************************************
fatal: [127.0.0.1]: FAILED! => {
    "changed": false,
    "rc": 1
}

MSG:

MODULE FAILURE
See stdout/stderr for the exact error


MODULE_STDERR:

ParserError: /home/___/.ansible/tmp/ansible-tmp-1756236420.9482832-2444818-11025389486272/AnsiballZ_win_scheduled_task.ps1:159
Line |
 159 |  {"module_entry": "IyFwb3dlcnNoZWxsCgojIENvcHlyaWdodDogKGMpIDIwMTU …
     |  ~
     | unexpected token '', expected 'begin', 'process', 'end', 'clean', or
     | 'dynamicparam'.


PLAY RECAP **************************************************************************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Relevant software versions:

  • OS Name: Microsoft Windows 11 Enterprise
  • OS Version 10.0.22631 Build 22631
  • WSL: 6.6.87.2-microsoft-standard-WSL2
  • Ubuntu: 24.04.2 LTS
  • Powershell: PowerShell 7.5.2
  • Ansible: [core 2.16.3]
  • Python: 3.12.3
  • Jinja: 3.1.2
  • community.windows: 2.1.0

Any help is appreciated!

2.16 is quite old and unsupported, and even within that series there were 11 releases after 2.16.3. While this won’t necessarily be resolved by moving to a newer version, I know there was a pretty major Powershell-related change in 2.18, and it was touched again in 2.19. Testing the newest version would at least rule out it being a known bug that was already addressed.

Thanks, I’ll try upgrading when able.