0

I have a PowerShell script that runs another script from local server fine when executed locally and produces results in a standard text file.

However, when I run the same script through Ansible Runbook, no result is produced

The PowerShell script is-

# RunCis.ps1
# Import DBA functions 
# Construct local instance name and execute Getter script, format output and redirect to 
# a text file on the local server
  .\ImportAllLmeSqlDbaFunctions.ps1

$inst = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
$instance = "$env:COMPUTERNAME\$inst"

Get-CISAdHocDistQueries -Instances $instance| format-table -AutoSize > c:\cis\output.txt 

The Ansible Runbook is-

---


# Below will create directory if not existing

- name: Copy Powershell scripts to remote server 
  copy:
    src: cis
    dest: c:\
- name: Run CIS powershell script on the remote server
  win_shell: C:\cis\RunCis.ps1 

Your help is much appreciated.

rp123409
  • 145
  • 3
  • 11
  • 1
    Have you tried to use `register` ? – error404 Jul 21 '21 at 08:11
  • Thanks. I am fairly new to Ansible so some example regarding registering will help – rp123409 Jul 21 '21 at 08:43
  • 1) Maybe you want to use `win_powershell`? By default, ansible runs `win_shell` entries using cmd, though you can change this by specifying `executable: pwsh.exe` for example. 2) You may want to specify full path to `.\ImportAllLmeSqlDbaFunctions.ps1` in case ansible starts the shell somewhere else. 3) try using something like `register: output` and `msg: "pwsh output is {{ output }}"` to view if any errors are getting generated. – Cpt.Whale Jul 21 '21 at 18:45

0 Answers0