Closed sjanssen15 closed 4 weeks ago
The parameters
option just sets the parameters when calling the script
, you still need to specify the param
block to map those parameters or access them through $args
. In your example you just want to add param($Path)
to the top of the script being run and it will be able to access the parameter you specified.
- name: Test script
ansible.windows.win_powershell:
script: |
param($Path)
Write-Host $Path
parameters:
Path: C:\temp
register: script
The
parameters
option just sets the parameters when calling thescript
, you still need to specify theparam
block to map those parameters or access them through$args
. In your example you just want to addparam($Path)
to the top of the script being run and it will be able to access the parameter you specified.- name: Test script ansible.windows.win_powershell: script: | param($Path) Write-Host $Path parameters: Path: C:\temp register: script
Thanks for your time to reply. Sorry I didn't understood this. The documentation doesn't say this at parameters. Perhaps good to explain?
The script
is treated as a ScriptBlock to run, the parameter
don't set variables but pass it through as parameters when invoking it. Consider this example
Function My-Function {
Write-Host "Path is '$Path'"
}
My-Function -Path 'foo'
The $Path
variable is not set in the function because you don't actually specify the parameters for the function. To get it working you need to specify the parameter in the function/scriptblock itself:
Function My-Function {
param ($Path)
Write-Host "Path is '$Path'"
}
My-Function -Path 'foo'
You could also access it through $args[1]
($args[0]
is the parameter name -Path
) but that's less obvious and I wouldn't recommend it
Function My-Function {
Write-Host "Path is '$($args[1])'"
}
My-Function -Path 'foo'
The documentation for the module is at https://github.com/ansible-collections/ansible.windows/blob/5f859d110c8fc3eabd2bc435ecdd2ab01f3ccd46/plugins/modules/win_powershell.py#L62, if you can figure out a better way of explaining it then we are happy to see a PR with the change.
SUMMARY
When running a Powershell script with the module ansible.windows.win_powershell + Parameters (see second example on the docs website) I get no passthrough of the parameter.
ISSUE TYPE
COMPONENT NAME
ansible.windows.win_powershell
ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
OS: Red Hat Enterprise Linux 8.10 Kernel: 4.18.0-553.22.1.el8_10.x86_64
STEPS TO REPRODUCE
Running the following Playbook to a Windows Server 2022 standard host via WinRM with Kerberos auth.
EXPECTED RESULTS
The parameter to be passed on to the script and being usable in the script.
ACTUAL RESULTS
Output shows the empty parameter output. This is the same task as the second example in the examples on the Ansible docs: https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html