diff --git a/windows/arbitrary_powershell.yml b/windows/arbitrary_powershell.yml new file mode 100644 index 0000000..90d608d --- /dev/null +++ b/windows/arbitrary_powershell.yml @@ -0,0 +1,16 @@ +--- +- name: Arbitrary PowerShell + hosts: "{{ HOSTS | default('windows') }}" + gather_facts: false + vars: + ps_script: undef + + tasks: + - name: Run PowerShell + ansible.windows.win_powershell: + script: | + {{ ps_script }} + register: ps_output + + - debug: + msg: "{{ ps_output.output }}" diff --git a/windows/powershell_script.yml b/windows/powershell_script.yml new file mode 100644 index 0000000..122e5ff --- /dev/null +++ b/windows/powershell_script.yml @@ -0,0 +1,20 @@ +--- +- name: PowerShell Script + hosts: "{{ HOSTS | default('windows') }}" + gather_facts: false + vars: + remote_dest: "C:\\sample_script.ps1" + tasks: + - name: Copy script to remote + ansible.windows.win_copy: + src: "{{playbook_dir}}/sample_script.ps1" + dest: "{{ remote_dest }}" + + - name: Run Script + ansible.windows.win_powershell: + script: | + {{ remote_dest }} -ServiceState {{ service_state }} + register: ps_output + + - debug: + var: ps_output diff --git a/windows/sample_script.ps1 b/windows/sample_script.ps1 new file mode 100644 index 0000000..159170a --- /dev/null +++ b/windows/sample_script.ps1 @@ -0,0 +1,7 @@ +Param +( + [Parameter(Mandatory=$true)] + [string]$ServiceState +) +# Sample parameterized script for use with powershell_script.yml +Get-Service | Where-Object -FilterScript {$_.Status -eq $ServiceState} | Select-Object -Property 'Name' diff --git a/windows/setup.yml b/windows/setup.yml index 2013429..2b0045b 100644 --- a/windows/setup.yml +++ b/windows/setup.yml @@ -114,3 +114,52 @@ controller_templates: type: text variable: package_name required: true + + - name: "WINDOWS / Arbitrary PowerShell" + job_type: run + inventory: "Workshop Inventory" + project: "Ansible official demo project" + playbook: "windows/arbitrary_powershell.yml" + execution_environment: Default execution environment + credentials: + - "Workshop Credential" + survey_enabled: true + survey: + name: '' + description: '' + spec: + - question_name: Server Name or Pattern + type: text + variable: HOSTS + required: false + - question_name: PowerShell Script (Default returns random cat fact) + type: textarea + variable: ps_script + default: "(Invoke-RestMethod -Method 'GET' -Uri 'https://catfact.ninja/fact').fact" + required: true + + - name: "WINDOWS / PowerShell Script" + job_type: run + inventory: "Workshop Inventory" + project: "Ansible official demo project" + playbook: "windows/powershell_script.yml" + execution_environment: Default execution environment + credentials: + - "Workshop Credential" + survey_enabled: true + survey: + name: '' + description: '' + spec: + - question_name: Server Name or Pattern + type: text + variable: HOSTS + required: false + - question_name: Service state to query? + type: multiplechoice + variable: service_state + required: false + default: 'Running' + choices: + - 'Running' + - 'Stopped'