Skip to content

Instantly share code, notes, and snippets.

@rterbush
Last active December 16, 2015 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rterbush/9719d2280d19eb95d583 to your computer and use it in GitHub Desktop.
Save rterbush/9719d2280d19eb95d583 to your computer and use it in GitHub Desktop.
apply_script_01:
cmd.run:
- name: '
invoke-sqlcmd -inputfile "c:\\temp\\01_script.sql"'
- shell: powershell
- creates: 'c:\\var\\salt\\state\\_01_script.txt'
script_01_complete:
file.touch:
- name: 'c:\\var\\salt\\state\\_01_script.txt'
- require:
- cmd: apply_script_01
apply_script_02:
cmd.run:
- name: '
invoke-sqlcmd -inputfile "c:\\temp\\02_script.sql"'
- shell: powershell
- creates: 'c:\\var\\salt\\state\\_02_script.txt'
- require:
- cmd: apply_script_01
script_02_complete:
file.touch:
- name: 'c:\\var\\salt\\state\\_02_script.txt'
- require:
- cmd: apply_script_02
@rterbush
Copy link
Author

Went this direction for the following reasons:

  1. using cmd.script looks interesting, but not clear if shell: will allow me to do something like shell: "powershell invoke-sqlcmd -Inputfile"
  2. cannot really count on return values from powershell for different commands it is invoking like 'invoke-sqlcmd' which makes it hard to use stateful: True

@lorengordon
Copy link

@rterbush, here is an example using a powershell try block with -ErrorAction Stop with cmd.run and stateful: True:

{%- set join_domain = salt['pillar.get']('join-domain:windows', {}) %}

{%- if join_domain %}

join standalone system to domain:
  cmd.run:
    - name: '
      try
      {
        $domain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()).Name;
        "changed=no comment=`"System is joined already to a domain [$domain].`" domain=$domain";
      }
      catch
      {
        $cred = New-Object -TypeName System.Management.Automation.PSCredential
          -ArgumentList {{ join_domain.username }}, (ConvertTo-SecureString
          -String "{{ join_domain.encrypted_password }}"
          -Key ([Byte[]] "{{ join_domain.key }}".split(",")));
    {%- if join_domain.oupath -%}
        Add-Computer -DomainName {{ join_domain.domain_name }} -Credential $cred
          -Force -OUPath "{{ join_domain.oupath }}" -ErrorAction Stop;
    {%- else -%}
        Add-Computer -DomainName {{ join_domain.domain_name }} -Credential $cred
          -Force -ErrorAction Stop;
    {%- endif -%}
        "changed=yes comment=`"Joined system to the domain.`" domain={{ join_domain.domain_name }}"
      }'
    - shell: powershell
    - stateful: true
{%- endif %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment