# Run multipul powershell commands with ansible vm\_shell

**URL:** <https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 12, 2020, 4:57pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950 "2020-01-12T16:57:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![batchenr](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/batchenr/32/7123_2.png) [@batchenr](https://forum.ansible.com/u/batchenr)\
**Post date:** [January 12, 2020, 4:57pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/1 "2020-01-12T16:57:18Z")

</div>

Im using Ansible 2.9.2, I need to run 3 powershell command one after the other. My playbook :

```auto
  tasks:
    - name: "User Create"
      local_action:
        module: vmware_vm_shell
        cluster: "{{ cluster }}"
        datacenter: "{{ datacenter }}"
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        vm_id: "{{ name }}"
        vm_username: "{{ vm_username }}"
        vm_password: "{{ vm_password }}"
        vm_shell: 'c:\Windows\System32\WindowsPowershell\v1.0\powershell.exe'
        vm_shell_args: '$Password= ConvertTo-SecureString {{ Password }} -AsPlainText -Force ; New-LocalUser {{ Username}} -Password {{ Password }} ; Add-LocalGroupMember -Group {{ Group }} -Member {{ Username }}'
        wait_for_process: yes
        validate_certs: no

```

If i run the first command like :

```auto
vm_shell_args: '$Password= ConvertTo-SecureString {{ Password }} -AsPlainText -Force 

```

It works. How can i run multiple vm\_shell commands ?

I tried separate then with ‘;’ or ‘|’ but none of it works. Thanks

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 12, 2020, 7:21pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/2 "2020-01-12T19:21:20Z")

</div>

‪On Sun, Jan 12, 2020 at 11:57 AM ‫רגב בתחן‬‎ \<batchenr@gmail.com\> wrote:‬

> Im using Ansible 2.9.2, I need to run 3 powershell command one after the other. My playbook :
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "User Create"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module: vmware\_vm\_shell  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cluster: "{{ cluster }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: "{{ datacenter }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ vcenter\_server }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ vcenter\_user }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ vcenter\_pass }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_id: "{{ name }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_username: "{{ vm\_username }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_password: "{{ vm\_password }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_shell: 'c:\Windows\System32\WindowsPowershell\v1.0\powershell.exe'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_shell\_args: '$Password= ConvertTo-SecureString {{ Password }} -AsPlainText -Force ; New-LocalUser {{ Username}} -Password {{ Password }} ; Add-LocalGroupMember -Group {{ Group }} -Member {{ Username }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_for\_process: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: no
> 
> If i run the first command like :
> 
> vm\_shell\_args: '$Password= ConvertTo-SecureString {{ Password }} -AsPlainText -Force
> 
> It works. How can i run multiple vm\_shell commands ?
> 
> I tried separate then with ';' or '|' but none of it works. Thanks

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I think you can create a loop (lookup "with\_items" or "loop" or  
something like that) and pass the {{ item }}. Thing is I do not know  
what you are looping around, but I have weaseled out by having the  
loop task calling/including another task (file) that does the deed.

Now if one task depends on the output of the previous one, you could  
use set a variable and use when to tell the next task the previous one  
was successful (in case ansible does not catch the error message).

---

<div class="post-metadata">

**Author:** ![batchenr](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/batchenr/32/7123_2.png) [@batchenr](https://forum.ansible.com/u/batchenr)\
**Post date:** [January 12, 2020, 7:26pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/3 "2020-01-12T19:26:24Z")

</div>

I need all commands to run one by one, i tried loop with items it didnt work, all of this commands creating windows user in win vm, the first one do the Password var, the second creates rhe user with the Password var and the last is adding it to a group, with loop it loops all playbook, i need it to open powershell only once.

If i run this command manually on powershell it works but i cannot find a way to do 3 commands sequence, thanks

בתאריך יום א׳, 12 בינו׳ 2020 ב-21:21 מאת Mauricio Tavares \<[raubvogel@gmail.com](mailto:raubvogel@gmail.com)\>:

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 12, 2020, 7:33pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/4 "2020-01-12T19:33:14Z")

</div>

‪On Sun, Jan 12, 2020 at 2:26 PM ‫רגב בתחן‬‎ \<batchenr@gmail.com\> wrote:‬

> I need all commands to run one by one, i tried loop with items it didnt work, all of this commands creating windows user in win vm, the first one do the Password var, the second creates rhe user with the Password var and the last is adding it to a group, with loop it loops all playbook, i need it to open powershell only once.
> 
> If i run this command manually on powershell it works but i cannot find a way to do 3 commands sequence, thanks

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What if you write a powershell script to run the 3 commands,  
taking as arguments whatever info it needs (paths, usenrames,  
whatever) and then run the 3 commands inside it? Then copy file to  
target box, run it there as the local user you need it to run as, get  
results (so you know if it worked or went boink), and delete file.

---

<div class="post-metadata">

**Author:** ![batchenr](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/batchenr/32/7123_2.png) [@batchenr](https://forum.ansible.com/u/batchenr)\
**Post date:** [January 12, 2020, 8:08pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/5 "2020-01-12T20:08:11Z")

</div>

I can do it in a script, i was wondering if ansible had another way…

בתאריך יום א׳, 12 בינו׳ 2020 ב-21:33 מאת Mauricio Tavares \<[raubvogel@gmail.com](mailto:raubvogel@gmail.com)\>:

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 12, 2020, 9:25pm UTC](https://forum.ansible.com/t/run-multipul-powershell-commands-with-ansible-vm-shell/31950/6 "2020-01-12T21:25:20Z")

</div>

‪On Sun, Jan 12, 2020 at 3:08 PM ‫רגב בתחן‬‎ \<batchenr@gmail.com\> wrote:‬

> I can do it in a script, i was wondering if ansible had another way..

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I think the only way to run all commands on the same task is to  
it at the powershell level, which is what you were trying to do. I  
know using the command module, if I want to do 3 commands in the same  
task such that it will stop if one command fails is "commandA &&  
commandB && commandC" which is bash, not ansible.

There is nothing stopping you from running a script as a one-liner in  
powershell, i.e.

{CommandA -ea Stop} Catch {CommandB}

the third command I do not know how to add. I think ansible is running  
your command as

powershell.exe -Command "commandA $arg1 $arg $argN"

Maybe something like this would work

powershell.exe -Command "&(commandA $arg1 $arg $argN" ; commandB  
$arg3 $arg1; etc}"

AFAIK (I have not touched powershell since 2018), the ";" means each  
command does not care what the previous did. Maybe "&" would work?
