小编rog*_*son的帖子

PowerShell 5 AppSettings Bug?

我升级到PowerShell v5.0.10586.117,现在我无法从app.config文件访问AppSettings.该代码适用于PowerShell v4(我已经在安装了v4的其他计算机上尝试过,它从AppSettings返回数据).

我在PowerShell中设置了App Config

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $AppConfigPath)
Run Code Online (Sandbox Code Playgroud)

我可以验证当前域是否具有应用程序设置

[System.AppDomain]::CurrentDomain.GetData("APP_CONFIG_FILE")
Run Code Online (Sandbox Code Playgroud)

回来PATH\App.config 但是我跑的时候

[System.Configuration.ConfigurationManager]::AppSettings.count
Run Code Online (Sandbox Code Playgroud)

PowerShell v5返回: 0

但在PowerShell v4(Windows 7,Windows Server 2008 R2 Enterprise等)上返回: 5

为什么PowerShell 5与PowerShell 4的行为会有所不同?这是一个错误吗?知道如何解决App Settings问题吗?

[更多信息]

我试过做一个解决方案

      $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)
Run Code Online (Sandbox Code Playgroud)

使用$ retVal这个work -round,我可以从我期望的AppSettings中获取数据,但是稍后在我导入的.dll上失败,期望App Config数据存在.

[更新1]:我在Windows 8,Windows 8.1和Server 2012上升级到PowerShell 5并遇到了同样的问题.更新了标题和问题说明,以反映它在我测试的所有PowerShell 5实例上.

[更新2]:

根据@PetSerAl,我发现如果我在设置配置文件之前尝试获取AppSetting,我将在PowerShell v4中遇到同样的问题

[System.Configuration.ConfigurationManager]::AppSettings
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $AppConfigPath)
[System.Configuration.ConfigurationManager]::AppSettings.Count
Run Code Online (Sandbox Code Playgroud)

0在PowerShell v4中返回.

如果我将我的应用程序配置文件移动到C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config那时我没有返回数据的问题,我也没有遇到我正在加载的外部dll的问题,期望在应用程序配置中的信息

windows powershell app-config powershell-5.0

5
推荐指数
1
解决办法
243
查看次数

标签 统计

app-config ×1

powershell ×1

powershell-5.0 ×1

windows ×1