Run Windows Powershell script on condition

884 views
Skip to first unread message

James O'Neill

unread,
May 1, 2014, 7:31:50 AM5/1/14
to salt-...@googlegroups.com
Hi,

I'm running SaltStack 2014.1.3 with a Windows minion, and I'm trying to run a Powershell script on the minion only if a directory or file does not exist. Here is my current test state:

cmd-test:
  cmd.script:
    - source: salt://scripts/install.ps1
    - shell: powershell
    - creates: c:\test.txt


The script is executed every time. I have also tried onlyif and unless, but don't know what I can put as the test. I have tried tests like

- unless: cmd /c 'IF EXIST c:\test.txt EXIT 0'

but the tests don't seem to care if the directory/file exists.

Is this supported on Windows? What should my state look like if I want to run a script if a directory does not exist?

Regards,
James

Colton Myers

unread,
May 6, 2014, 3:18:54 PM5/6/14
to salt-...@googlegroups.com
I don't think cmd.script supports the `creates` argument.  You'll have to do it with unless or onlyif.  Those arguments work off the return code of the command you give them.  So you need to run that command you're using for `unless` and verify that it's giving a zero return code when you don't want the script to run.

--
Colton Myers


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hemebond

unread,
May 10, 2014, 10:52:07 PM5/10/14
to salt-...@googlegroups.com
Thanks for the reply Colton. New cmd.script does appear to support "creates" but I didn't end up using it. Your comment helped me find the logic flaw in my check and it's working
 correctly now with:

- onlyif: cmd /c 'IF EXIST c:\test.txt EXIT 1'

Which isn't nice to read but works. Thanks again.


--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/0VuheFs4ZGQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.

Colton Myers

unread,
May 15, 2014, 3:16:15 PM5/15/14
to salt-...@googlegroups.com
Awesome!  Glad you got it working.

--
Colton Myers
Reply all
Reply to author
Forward
0 new messages