有没有人研究过如何让PowerShell使用app.config
文件?我有一些.NET DLL我想在我的一个脚本中使用,但是他们希望自己的配置部分存在于app.config
/中web.config
.
我有一个Powershell脚本,它正在加载.NET程序集(在我的例子中是.EXE)并调用一个公共方法,该方法使用app.config来提取加密的连接字符串.
该脚本将程序集的exe.config动态复制到Powershell($ pshome)文件夹作为powershell.exe.config,并且能够从我的开发框运行就好了.问题是它不能从标准的Windows Server 2003安装运行.
我验证了exe.config正在被正确复制到powershell目录.我运行了SysInternals Process Explorer并验证了该进程正在访问配置文件(没有找到文件的消息).我远程调试的powershell.exe实例,可以看到该组件加载罚款,但不能访问ConfigurationManager.AppSettings [...]值(返回null).
我没有想法.我已经读过,我可能可以使用单独的应用程序域,但我看不到使用Powershell执行此操作的示例.
我的代码对以下内容产生了影响:
$absolute_path = "c:\foo.exe"
$config_path = $absolute_path + ".config"
copy "$config_path" "$pshome\powershell.exe.config" -Force
[Reflection.Assembly]::LoadFrom($absolute_path)
$foo = new-object MyFooAssembly.FooClass
$foo.DoSomething()
Run Code Online (Sandbox Code Playgroud)
在Vista中代码可以工作,在Windows Server 2003中,代码不起作用.
我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig);
Run Code Online (Sandbox Code Playgroud)
[Configuration.ConfigurationManager] :: ConnectionStrings.Count总是返回"1",
"[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name"总是返回"LocalSqlServer",而ConnectionString名称不在我的".config"中文件.
请注意,从PowerShell命令提示符执行PowerShell脚本将按预期运行.就在我从PowerShell ISE中执行它时,它无法按预期工作.