From a0b3c7c0d649f0d5f3ceefaf51fbbfe1c39ab726 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 21 Jan 2026 03:29:16 +0100 Subject: [PATCH] Fix LIB env var incorrectly set to "System.Collections.DictionaryEntry" (#86436) This is a fix for a common issue where Ansible is setting the LIB env var to the data type (System.Collections.DictionaryEntry) and not the actual value. It unfortunately causes downstream issues if you run PowerShell scripts that are affected by the LIB variable not being a search path, but an incorrect string. It causes Add-Type to fail on even the simples call (like `Add-Type "using System;"` --- changelogs/fragments/fix-windows-lib-env-corruption.yml | 2 ++ .../module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 | 2 +- .../library/add_type_test.ps1 | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/fix-windows-lib-env-corruption.yml diff --git a/changelogs/fragments/fix-windows-lib-env-corruption.yml b/changelogs/fragments/fix-windows-lib-env-corruption.yml new file mode 100644 index 00000000000..4ad3c478ede --- /dev/null +++ b/changelogs/fragments/fix-windows-lib-env-corruption.yml @@ -0,0 +1,2 @@ +bugfixes: +- "Fix Windows LIB env var corruption (https://github.com/ansible-collections/ansible.windows/issues/297)." diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 index 3a14608e1f4..e4b447224a3 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 @@ -378,7 +378,7 @@ Function Add-CSharpType { $originalEnv = @{} try { 'LIB' | ForEach-Object -Process { - $value = Get-Item -LiteralPath "Env:\$_" -ErrorAction SilentlyContinue + $value = (Get-Item -LiteralPath "Env:\$_" -ErrorAction SilentlyContinue).Value if ($value) { $originalEnv[$_] = $value Remove-Item -LiteralPath "Env:\$_" diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 index 5cb1a72da70..59d09dc3567 100644 --- a/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 @@ -322,6 +322,7 @@ namespace Namespace12 $env:LIB = "C:\fake\folder\path" try { Add-CSharpType -Reference $lib_set + Assert-Equal -actual $env:LIB -expected "C:\fake\folder\path" } finally { Remove-Item -LiteralPath env:\LIB