PowerShell 5 AppSettings Error?

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

I set the application configuration in PowerShell

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

I can verify that the current domain has application 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 is the behavior of PowerShell 5 different from PowerShell 4? This is mistake? Any idea to solve the problem with the application settings?

[Additional Information]

I tried to work 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) 

Using this working environment, using $ retVal, I can get data from pending AppSettings, but this fails later .dll, which I import, which expects 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 problem. The description of Title and issue has been updated to reflect that it is in all PowerShell 5 instances that I tested.

[Update 2]:

Per @PetSerAl, I found that I will have the same problem in PowerShell v4 if I try to get AppSetting before installing the configuration 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 translate my application configuration file to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config , then I have no problem returning data and I have no problem with external DLLs that I load while waiting application configuration information

+5
source share
1 answer

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

+1
source

Source: https://habr.com/ru/post/1247891/


All Articles