# Windows module development question: msiexec acts up when ran through WinRM

**URL:** <https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [April 20, 2016, 8:38pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224 "2016-04-20T20:38:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 20, 2016, 8:38pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/1 "2016-04-20T20:38:56Z")

</div>

Hi, Ansible devel folks,

This week I put together a module that sets the Splunk Universal Forwarder up on Windows machines. One of the nice things Splunk’s done with the forwarder is make some useful flags that you can pass to msiexec.exe that do things like configure the forwarder as a service, but leave it stopped for now and configured to start on next boot - this is perfect for what I want, which is to set the forwarder set up on a base machine image that I can spin up lots of new instances from and see them automagically start feeding their logs to the indexer.

The whole module in its current state is here - [https://gist.github.com/kwerey/e20649d24d8dff63bf39db5ecb569fe1](https://gist.github.com/kwerey/e20649d24d8dff63bf39db5ecb569fe1). The goal is to let users do their decision-making in the inventory, in ways that are pretty easy to compare & contrast. So you give it a dictionary of key/value pairs to pass as flags, and the module collapses them into the string you see here called $installParameters. Other than that, it’s not doing anything too complex, just downloading and installing:

`

| 

$cmd = “msiexec.exe /i ${installer\_path} RECEIVING\_INDEXER=‘${indexer\_uri}:${indexer\_port}’ ${installParameters} AGREETOLICENSE=yes /quiet”  
Invoke-Expression $cmd

 |
| | |

`

But… Windows seems to be thwarting my plans. If I stick that variable containing the complete command - $cmd - into the module’s return values, I get this:

`  
“cmd”: “msiexec.exe /i C:\Users\vagrant\AppData\Local\Temp\SplunkForwarder.msi RECEIVING\_INDEXER=‘192.168.2.10:9997’ LAUNCH\_SPLUNK=0 AGREETOLICENSE=yes /quiet”

`

If run that command verbatim in an RDP session, I get exactly the intended behaviour: the forwarder installs, shows up in the results of `get-service` as stopped:

`

```auto
PS C:\Users\Administrator> msiexec.exe /i C:\Users\vagrant\AppData\Local\Temp\SplunkForwarder.msi RECEIVING_INDEXER='192.168.2.10:9997' LAUNCH_SPLUNK=0 AGREETOLICENSE=yes /quiet
PS C:\Users\Administrator>
PS C:\Users\Administrator> get-service -name Spl*

Status Name DisplayName
------ ---- -----------
Stopped SplunkForwarder SplunkForwarder Service

PS C:\Users\Administrator>

```

`

But when I run through Ansible, splunkd starts up running. Very mysterious. It’s like it’s not reading the flags, even though it pays attention to /quiet and installs successfully.

I’ve had a google around for how the rules might change when logged in to an RDP session versus running remotely over WinRM, but nothing jumped out at me as suspicious. I thought I was on to something when I realised I’d been running my playbook as the Vagrant user, but RDP’d in as Administrator, but running the Ansible as Administrator doesn’t change anything.

Anyone wise in the ways of Powershell got any tips about where I’m going wrong here?

Thanks,  
Nikki

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 20, 2016, 10:23pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/2 "2016-04-20T22:23:07Z")

</div>

Hi Nikki,

Building up command line arguments for powershell to execute can be a trial. On occasion I have had to resort to building an array of the args I want to pass and then running the exe using Start-Process. There’s a page here which describes the many ways you can and some times have to code powershell in order to start external programs: [http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx](http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx)

I would probably set /qn as one of the options, just to make sure msiexec.exe doesn’t try and start a gui.

Sounds like you are a way into getting your module working, but I’d consider making a role to do this. Well worth trying the win\_package module to fetch and install the forwarder. I am pretty sure I have used it to install the splunk forwarder successfully in the past, although perhaps without as many extra args. You can also use win\_template to modify the splunk config, depending on your needs and win\_service to take control of when the forwarder comes up etc.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 21, 2016, 4:40pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/3 "2016-04-21T16:40:11Z")

</div>

Thanks, Jon, that’s some really good info.

And, yeah, agreed that a role would be a better format for this. I’d started out trying to make one, hit weird bugs, and gone ‘eh, if I try to do it as a module it’ll probably make it easier to find what’s going wrong’.

And it… kind of has. Getting msiexec to log everything shows me it’s actually getting the right flag, applying it, and then running a bunch of extra stuff later that overrides to switches the ‘now start the service’ option back on. Mysterious, and maybe more a problem with Splunk than with my Ansible skills or the skills of whoever wrote the win\_msi module I’d tried and rejected.

Oh well. I learned a lot of extra Powershell skills from trying!

Thanks,  
Nikki

---

<div class="post-metadata">

**Author:** ![marc.petrivelli](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marc.petrivelli/32/3773_2.png) [@marc.petrivelli](https://forum.ansible.com/u/marc.petrivelli)\
**Post date:** [April 21, 2016, 4:54pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/4 "2016-04-21T16:54:09Z")

</div>

I had similar issues when automating the install of the splunk forwarder. I used the win\_msi module to install splunk however I couldn’t get around the forwarder starting automatically (behavior was inconsistent). I am a very amateur powershell coder and personally despise the language (if you can even call it that). I ended up pulling the win\_msi modules directrly from the devel branch of the extras repo and saw more consistent behavior but still had issues with the server starting. There was a large change in this module WRT to the RunAs parameter and wait logic. Maybe give it a try.

To get around the service starting before i could deploy my config file I didL

`

- name: splunk | install forwarder  
win\_msi:  
path: “{{splunk\_installer\_loc}}”  
state: present  
wait: yes  
extra\_args: "{{ splunk\_arg\_list | join(’ ')}} "  
creates: “{{splunk\_install\_dir}}”  
register: splunk\_install\_status

- name: splunk | set reboot required flag  
set\_fact: win\_reboot\_required=true  
when: splunk\_install\_status.reboot\_required | default(False)

- name: splunk | deploy conf files  
win\_template: src=“{{ item.file }}”  
dest=“{{ splunk\_install\_dir}}\{{item.loc}}”  
with\_items:

- { file: ‘server\_outputs.conf.j2’, loc: ‘etc\system\local\outputs.conf’ }

- { file: ‘windows\_inputs.conf.j2’, loc: ‘etc\apps\Splunk\_TA\_windows\local\inputs.conf’ }  
register: splunk\_conf\_files

- name: splunk | restart service  
win\_service: name=“{{splunk\_service\_name}}” state=“{{item}}”  
with\_items:

- stopped

- started  
ignore\_errors: yes ##stopping an already stopped service causes errors  
when: splunk\_conf\_files | changed

`

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 21, 2016, 5:05pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/5 "2016-04-21T17:05:34Z")

</div>

I’m glad it’s not just me hitting this!

Thanks for the info, Marc. Can I check - in that example, you’re seeing it start automatically, then you’re restarting it so it reads the conf files?

(My original plan to stick it on a base machine image, so it’d be ideal to get it installed without ever starting the service)

---

<div class="post-metadata">

**Author:** ![marc.petrivelli](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marc.petrivelli/32/3773_2.png) [@marc.petrivelli](https://forum.ansible.com/u/marc.petrivelli)\
**Post date:** [April 21, 2016, 5:09pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/6 "2016-04-21T17:09:58Z")

</div>

It starts automatically…most of the time. I can’t figure exactly what the conditions are when it doesn’t start. if you notice I added a “ignore\_errors: yes” to the restart service task. This was to work around the fact that it didn’t always start. As an FYI in ansible 1.9.x, when you call win\_service: name=svc state=stopped when svc is already running, the module throws an error

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 21, 2016, 6:05pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/7 "2016-04-21T18:05:43Z")

</div>

Ah, okay. I wonder if things have changed - using the newest build of the universal forwarder, it seemed to be misbehaving, but misbehaving in pretty consistent ways.

Thanks! & glad you’ve got to some working automation. If I figure out a fix for my extra-specific use case, I’ll report back here with it.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 22, 2016, 9:35am UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/8 "2016-04-22T09:35:09Z")

</div>

Thanks for this.

One other thing that might be worth mentioning. My understanding is that msiexec communicates with a windows installer service, which actually does the job of doing whatever the msi is supposed to do.  
You can wait for msiexec to complete but its perfectly possible for the installer service to be doing things after msiexec has returned.

Might be worth using Resource Monitor or similar to observe what processes are running and see if that might explain any inconsistencies.

Jon

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 22, 2016, 8:22pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/9 "2016-04-22T20:22:57Z")

</div>

I didn’t know that - nice.

BTW, the other thing I wrote a Powershell module for since starting to work with Windows was one to manage the certificate store. I couldn’t see an existing Ansible way to do this - but I suspect that’s because there’s nice native ways to do this with group policy for machines on a domain, and that most people don’t needs to. If a module for adding certificates to a store would be useful, though, I’d be happy to put together a PR for that.

Cheers,  
Nikki

---

<div class="post-metadata">

**Author:** ![marc.petrivelli](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marc.petrivelli/32/3773_2.png) [@marc.petrivelli](https://forum.ansible.com/u/marc.petrivelli)\
**Post date:** [April 23, 2016, 7:04pm UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/10 "2016-04-23T19:04:54Z")

</div>

I would say put it out there. My team has written quite a few ad hoc powershell scripts to deal with certs on Windows and it can be a bit daunting. I was able to get a good start on some of these scripts by looking at how certs are handled in [http://docs.ansible.com/ansible/intro\_windows.html#windows-system-prep](http://docs.ansible.com/ansible/intro_windows.html#windows-system-prep)

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 25, 2016, 7:55am UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/11 "2016-04-25T07:55:14Z")

</div>

Sorry for the delay following up on this one.

There is actually an as-yet-unmerged windows module for managing certificates here: [https://github.com/ansible/ansible-modules-extras/pull/1374](https://github.com/ansible/ansible-modules-extras/pull/1374)

It would be great to get your input on the module and perhaps enhancements (if yours does other things the above doesn’t). If you could test it out and comment on it to say you’ve tested it then that would get it a step closer to getting it merged into Ansible, or if you can enhance it that would be fab too. I agree there’s a place for a module to manage certs since not everyone is on a domain or wants to use group policy.

I know it can be a bit disappointing when someone else has got there first, but if you could take a look and share the wisdom of your experience, we will all get the best code we can.

By the way if you are thinking of creating a PR, its a big help if you can create integration tests. Since this is ansible we talking about integration tests are dead easy to create, since they are just playbooks. Happy to help anyone get started with integration testing, especially for windows stuff.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [April 25, 2016, 11:28am UTC](https://forum.ansible.com/t/windows-module-development-question-msiexec-acts-up-when-ran-through-winrm/21224/12 "2016-04-25T11:28:15Z")

</div>

Nah, it being the weekend is an great reason not to reply to emails! And - no, that’s, cool, I’d much rather there be one good certificates module that’s passed several pairs of eyes than a whole bunch of different ones with different features. I’ve left some thoughts on that PR.

Happy to have a look through integration tests some time - is there anything particularly needed for Windows at the moment?

Best,  
Nikki
