I was trying to script the usage of the windows DISM utilities via ansible – and ran into an issue with being unable to execute dism from within the winrm context. Apparently some varieties of rpc (which DISM uses internally) are prevented from running from within the winrm execution context … What this amounts to with regard to dism is that you can execute the dism commands and they appear to work - but due to the obscure way that some component within the process fail, mounting an image always produces a corrupt/unusable mount point (Get-WinImages -Mounted always displays that the mounted image “requires remount”)
I actually couldn’t find any good details on the topic – other than this blog post from someone who discusses the fun unnecessarily annoying details associated with automating windows via winrm within the context of puppet:
And the workaround for the issue he came up with was to create a scheduled task from within winrm, then manually trigger the running of the newly created scheduled task stashing away the stdout/stderr of the scheduled task execution, then poll for the ending of the process created by the triggered scheduled task, then lift the stashed output data back into winrm context for continued use in the winrm context.
I adapted his script (https://github.com/mwrock/boxstarter/blob/master/BoxStarter.Common/Invoke-FromTask.ps1) into an ansible module – which works something like this:
(from within winrm context)
(1) generate a wrapper powershell script to invoke the module’s command argument and redirect stdout/stderr to magic file paths (module’s command argument is assumed to invoke commands which can’t execute properly from within winrm environment)
(2) create a scheduled task as a particular user configured to invoke the powershell wrapper script created in step 1
(3) manually trigger the running of the scheduled task
→ causes a scheduled task to launch a process which doesn’t have the winrm execution restrictions
(4) poll for the end of the process launched by the scheduled task manager
(5) when the scheduled task process ends, ingest the stdout/stderr results that were stashed away
(6) cleanup the scheduled task and generated files
Usage looks like this:
win_invoke_from_task:
command: ‘dism.exe /Mount-Wim /WimFile:{{sts_windows_image_build_dir}}{{image}}.{{variant}}.wim /MountDir:{{sts_windows_image_mount_dir}} /index:1’
user: “{{ ansible_ssh_user }}”
credential: “{{ credential }}”
register: image_contents
tags: test
I wondered if anyone encountered this and solved the problem in a different way or would be interested in this module …?