Powershell 调用使用 App.config 的 .NET 程序集 [英] Powershell Calling .NET Assembly that uses App.config

查看:20
本文介绍了Powershell 调用使用 App.config 的 .NET 程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Powershell 脚本,它正在加载一个 .NET 程序集(在我的例子中是 .EXE)并调用一个使用 app.config 来提取加密连接字符串的公共方法.

I have a Powershell script that is loading a .NET assembly (.EXE in my case) and calling a public method that uses the app.config to pull an encrypted connection string.

脚本将程序集的 exe.config 作为 powershell.exe.config 动态复制到 Powershell ($pshome) 文件夹中,并且能够从我的开发箱中正常运行.问题是它不能从标准的 Windows Server 2003 安装中运行.

The script dynamically copies the assembly's exe.config over to the Powershell ($pshome) folder as powershell.exe.config and is able to run from my development box just fine. The problem is that it doesn't run from a standard Windows Server 2003 installation.

我确认 exe.config 被正确复制到 powershell 目录.我已经运行了 SysInternals Process Explorer 并验证了该进程正在访问配置文件(没有文件未找到消息).我远程调试了 powershell.exe 实例,可以看到程序集加载正常,但无法访问 ConfigurationManager.AppSettings[...] 值(返回 null).

I verified the exe.config is being copied over to the powershell directory correctly. I've run SysInternals Process Explorer and verified that the process was accessing the config files (no file not found messages). I remotely debugged the powershell.exe instance and can see that the assembly is loading fine but cannot access ConfigurationManager.AppSettings[...] values (returns null).

我没有想法了.我读过我可以使用单独的应用程序域,但我没有看到使用 Powershell 执行此操作的示例.

I'm out of ideas. I've read that I may be able to use a separate app domain but I see no examples of doing this with Powershell.

我的代码做了一些事情:

My code does something to the effect of:

$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()  

在 Vista 中代码有效,在 Windows Server 2003 中代码无效.

In Vista the code works, in Windows Server 2003 the code does not work.

推荐答案

尝试:

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

这篇关于Powershell 调用使用 App.config 的 .NET 程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆