Calling powershell function via Ansible

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]: 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?

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.

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?

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
`

I really appreciate the assist.