If you still want to write a script, most likely thing that might be messing things up is the fact that you need to be aware that PowerShell has the concept of an output pipe which can contain objects of varying types, and by default many PowerShell built in functions (also called CmdLets) default to sending objects to the output pipe. That's why sometimes you will see
| Out-Null
on the ends of some lines of the module code. Thus throws away unwanted objects and lets you control what is in the output pipe, so that only the things you need are converted into json to be returned to the ansible controller.
Hope this helps,
Jon
#!powershell# WANT_JSON# POWERSHELL_COMMON
$path = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"$items = Get-ChildItem -Recurse -Path $path -Include *.lnk$Shell = New-Object -ComObject WScript.Shell$result = @{ changed = $false installed_progs = @{}}$intalled_programs = New-Object System.Collections.Arraylist
foreach ($item in $items) {
$object = new-object psobject @{ name = $item.BaseName.tolower() path = $Shell.CreateShortcut($item).targetpath.tolower() }
if ($object.path.endswith('exe')) { $result.installed_progs.add($item.BaseName.toLower(), $Shell.CreateShortcut($item).targetpath.tolower()) }}
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-NullExit-Json $result
#!powershell# WANT_JSON$path = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"$items = Get-ChildItem -Recurse -Path $path -Include *.lnk$Shell = New-Object -ComObject WScript.Shell$result = @{ changed = $false installed_progs = @()}
foreach ($item in $items) {
$object = new-object psobject @{ name = $item.BaseName.tolower() path = $Shell.CreateShortcut($item).targetpath.tolower() }
if ($object.path.endswith('exe') -and $object.path -like "*Program*") { $result.installed_progs+=$object }}
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
echo $result | convertto-json -compress -depth 99---- name: Get Installed Programs with Versions hosts: testing# no_log: true gather_facts: FALSE tasks: - name: Get Installed Applications action: getInstalledPrograms register: applications - debug: var=applications
- name: Check Application Version win_file_version: path: "{{ item.path }}" no_log: false register: exe_file_version with_items: - "{{ applications.installed_progs }}" - debug: var="{{ item }}" no_log: false with_items: "{{ exe_file_version.results }}"ok: [REMOVED] => { "applications": { "changed": false, "installed_progs": [ { "name": "7-zip file manager", "path": "c:\\program files\\7-zip\\7zfm.exe" }, }
}ok: [REMOVED] => (item={u'_ansible_parsed': True, u'changed': False, u'_ansible_no_log': False, u'_ansible_item_result': True, u'win_file_version': {u'file_private_part': u'0', u'file_build_part': u'0', u'file_version': u'2, 3, 0, 0', u'product_version': u'2.3 beta 2', u'path': u'c:\\program files\\teracopy\\teracopy.exe', u'file_major_part': u'2', u'file_minor_part': u'3'}, u'item': {u'path': u'c:\\program files\\teracopy\\teracopy.exe', u'name': u'teracopy'}, u'invocation': {u'module_name': u'win_file_version'}}) => { "<type 'dict'>": "VARIABLE IS NOT DEFINED!", "item": { "changed": false, "invocation": { "module_name": "win_file_version" }, "item": { "name": "teracopy", "path": "c:\\program files\\teracopy\\teracopy.exe" }, "win_file_version": { "file_build_part": "0", "file_major_part": "2", "file_minor_part": "3", "file_private_part": "0", "file_version": "2, 3, 0, 0", "path": "c:\\program files\\teracopy\\teracopy.exe", "product_version": "2.3 beta 2" } }}
{
"changed": false,
"ansible_facts": {
"applications": {
"installed_progs": [
{
"name": "7-zip file manager",
"path": "c:\\program files\\7-zip\\7zfm.exe"
},
}
}
}Hope this helps,
Jon