Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell 5 AppSettings Bug?

I upgraded to PowerShell v5.0.10586.117 and now I am unable to access the AppSettings from my app.config file. The code works with PowerShell v4 (I've tried it on other computers that have v4 installed and it returns the data from AppSettings).

I set the App Config in PowerShell

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $AppConfigPath)

I can verify that the Current Domain has the App Settings with

[System.AppDomain]::CurrentDomain.GetData("APP_CONFIG_FILE")

returns PATH\App.config but when I run

[System.Configuration.ConfigurationManager]::AppSettings.count

PowerShell v5 returns: 0

but on PowerShell v4 (Windows 7, Windows Server 2008 R2 Enterprise, etc) returns: 5

Why would the behavior be different on PowerShell 5 vs PowerShell 4? Is this a bug? Any idea how to resolve the App Settings issue?

[More Info]

I have tried doing a work-around with

      $configFile = System.Configuration.ConfigurationManager]::OpenExeConfiguration([System.Configuration.ConfigurationUserLevel]::None)
      $settings = $configFile.AppSettings.Settings
      $retVal = $settings[$keyName].Value
      $configFile.Save([System.Configuration.ConfigurationSaveMode]::Modified)
      [System.Configuration.ConfigurationManager]::RefreshSection($configFile.AppSettings.SectionInformation.Name)

With this work -round using the $retVal, I can get the data out of the AppSettings that I expect, but it fails later on a .dll that I'm importing that expects the App Config data to exist.

[Update 1]: I upgraded to PowerShell 5 on Windows 8, Windows 8.1, and Server 2012 and got the same issue. Updated the Title and issue description to reflect that it is on all PowerShell 5 instances that I've tested.

[Update 2]:

Per @PetSerAl, I discovered that I will have the same issue in PowerShell v4 if I try to get the AppSetting before I set the config file

[System.Configuration.ConfigurationManager]::AppSettings
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $AppConfigPath)
[System.Configuration.ConfigurationManager]::AppSettings.Count

returns 0 in PowerShell v4.

If I move my app config file to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config then I do not have an issue returning the data, nor do I have an issue with the external dlls that I'm loading that expect information in the app config

like image 802
rogergarrison Avatar asked Nov 08 '22 16:11

rogergarrison


1 Answers

This seems to be a bug in the earlier versions of PowerShell 5. I have since upgraded to PowerShell 5.1.14409.1005 and the issue has been resolved.

like image 141
rogergarrison Avatar answered Dec 01 '22 21:12

rogergarrison