# Calling powershell function via Ansible

**URL:** <https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [March 5, 2020, 7:45pm UTC](https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396 "2020-03-05T19:45:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [March 5, 2020, 7:45pm UTC](https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396/1 "2020-03-05T19:45:40Z")

</div>

I have a function, it works perfectly if I run it directly from my target server.

`  
powershell.exe -ExecutionPolicy ByPass -file “C:\Scripts\USERNAME\dfsadd\_func.ps1”; dfsadd -junction Apps -obj\_name toast -prd\_vserver PROD -dr\_vserver DR -prd\_state online -dr\_state offline

With success seen below.

` `

Path TargetPath State ReferralPriorityClass ReferralPriorityRank

* * *

\DOMAIN.net\Apps\toast \PROD\Apps\toast Online sitecost-normal 0  
\DOMAIN.net\Apps\toast \DR\Apps\toast Offline sitecost-normal 0

`

Through ansible  
I’ve tried both win\_shell and win\_command like so  
`

- name: Create DFS links

# win\_command: powershell.exe -ExecutionPolicy ByPass -file C:\Scripts\USERNAME\dfsadd\_func.ps1; dfsadd “{{ vol\_junction }} {{ obj\_name }} {{ prd\_vserver }} {{ dr\_vserver }} {{ prd\_state }} {{ dr\_state }}”

win\_shell: powershell -file C:\Scripts\USERNAME\dfsadd\_func.ps1’;’ dfsadd " -junction {{ vol\_junction }} -obj\_name {{ obj\_name }} -prd\_vserver {{ prd\_vserver }} -dr\_vserver {{ dr\_vserver }} -prd\_state {{ prd\_state }} -dr\_state {{ dr\_state }}"

`

With varying combinations of how to include that semi-colon, how to _not_ include the semi-colon, whatever.

Mostly, to this result.

`  
fatal: [[HOSTNAME.DOMAIN.net](http://HOSTNAME.DOMAIN.net)]: FAILED! =\> {  
“changed”: true,  
“cmd”: “powershell -file C:\Scripts\USERNAME\dfsadd\_func.ps1’;’ dfsadd " -junction Apps -obj\_name toast -prd\_vserver PROD -dr\_vserver DR -prd\_state online -dr\_state offline"”,  
“delta”: “0:00:00.968771”,  
“end”: “2020-03-05 07:36:02.440099”,  
“msg”: “non-zero return code”,  
“rc”: 1,  
“start”: “2020-03-05 07:36:01.471327”,  
“stderr”: “Processing -File ‘C:\Scripts\USERNAME\dfsadd\_func.ps1;’ failed because the file does not have a ‘.ps1’ extension. Specify a valid Windows PowerShell script file name, and then try again.”,  
“stderr\_lines”: [  
“Processing -File ‘C:\Scripts\USERNAME\dfsadd\_func.ps1;’ failed because the file does not have a ‘.ps1’ extension. Specify a valid Windows PowerShell script file name, and then try again.”  
],  
“stdout”: “Windows PowerShell \r\nCopyright (C) 2016 Microsoft Corporation. All rights reserved.\r\n\r\n”,  
“stdout\_lines”: [  
"Windows PowerShell ",  
“Copyright (C) 2016 Microsoft Corporation. All rights reserved.”,  
“”  
]  
}

`

I don’t have much hair left to remove, so I can’t pull much more out, anyone have any good advice?

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [March 5, 2020, 9:35pm UTC](https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396/2 "2020-03-05T21:35:53Z")

</div>

When you call powershell.exe with -File you cannot run multiple commands after that, it’s designed to run a script with potential arguments. You can even see in the error from powershell it thinks the file it needs to run is `C:\Scripts\USERNAME\dfsadd\_func.ps1;’ (with the semicolon) so that’s why it is saying the extension is wrong.

If you are trying to run a function that is sourced from a file do the following

`

- name: Create DFS links  
win\_shell: |  
. C:\Scripts\USERNAME\dfsadd\_func.ps1 # dot source the file to load the functions  
dfsadd -junction Apps -obj\_name toast -prd\_vserver PROD -dr\_vserver DR -prd\_state online -dr\_state offline

`

Please bear in mind the double hop problem with WinRM where you will be unable to talk to downstream servers in your task unless you use something like become or your auth is CredSSP or Kerberos with credential delegation enabled.

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [March 5, 2020, 10:05pm UTC](https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396/3 "2020-03-05T22:05:13Z")

</div>

Jordan,

The methodology I was following was "if it worked through ise, why's this not pass it properly?", I knew I was doing something wrong, I just didn't know what yet.

I'll give this a shot in the morning and report back.

After 12 punches on the card of "Jordan saves the day" do you earn a prize?

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [March 6, 2020, 3:51pm UTC](https://forum.ansible.com/t/calling-powershell-function-via-ansible/32396/4 "2020-03-06T15:51:00Z")

</div>

Thanks for the note on the “Become” double hop snafu’s, found another post from a year or so ago where you helped someone else with resolving their become issues and used that to muddle through it myself.

solution

`

- name: Create DFS links  
win\_shell: |  
. C:\scripts\USERNAME\dfsadd\_func.ps1

dfsadd -junction “{{ vol\_junction }}” -obj\_name “{{ obj\_name }}” -prd\_vserver “{{ prd\_vserver }}” -dr\_vserver “{{ dr\_vserver }}” -prd\_state “{{ prd\_state }}” -dr\_state “{{ dr\_state }}”  
become: yes  
become\_method: runas  
become\_user: “[USERNAME2@DOMAIN.NET](mailto:USERNAME2@DOMAIN.NET)”  
`

I really appreciate the assist.
