@@ -49,6 +49,37 @@ Add-Type -TypeDefinition $dummy_ansible_basic
49
49
$module_code = Get-Content - LiteralPath $module_path - Raw
50
50
51
51
$powershell = [PowerShell ]::Create()
52
+ $powershell.Runspace.SessionStateProxy.SetVariable (" ErrorActionPreference" , " Stop" )
53
+
54
+ # Load the PowerShell module utils as the module may be using them to refer to shared module options
55
+ # FUTURE: Lookup utils in the role or collection's module_utils dir based on #AnsibleRequires
56
+ $script_requirements = [ScriptBlock ]::Create($module_code ).Ast.ScriptRequirements
57
+ $required_modules = @ ()
58
+ if ($null -ne $script_requirements ) {
59
+ $required_modules = $script_requirements.RequiredModules
60
+ }
61
+ foreach ($required_module in $required_modules ) {
62
+ if (-not $required_module.Name.StartsWith (' Ansible.ModuleUtils.' )) {
63
+ continue
64
+ }
65
+
66
+ $module_util_path = [System.IO.Path ]::GetFullPath([System.IO.Path ]::Combine($module_path , ' ..' , ' ..' , ' ..' ,
67
+ ' module_utils' , ' powershell' , " $ ( $required_module.Name ) .psm1" ))
68
+ if (-not (Test-Path - LiteralPath $module_util_path - PathType Leaf)) {
69
+ # Failed to find path, just silently ignore for now and hope for the best
70
+ continue
71
+ }
72
+
73
+ $module_util_sb = [ScriptBlock ]::Create((Get-Content - LiteralPath $module_util_path - Raw))
74
+ $powershell.AddCommand (' New-Module' ).AddParameters(@ {
75
+ Name = $required_module.Name
76
+ ScriptBlock = $module_util_sb
77
+ }) > $null
78
+ $powershell.AddCommand (' Import-Module' ).AddParameter(' WarningAction' , ' SilentlyContinue' ) > $null
79
+ $powershell.AddCommand (' Out-Null' ).AddStatement() > $null
80
+
81
+ }
82
+
52
83
$powershell.AddScript ($module_code ) > $null
53
84
$powershell.Invoke () > $null
54
85
0 commit comments