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

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

问题描述

我有一个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的($ PSHome的)文件夹powershell.exe.config,并能够从我的发展框就好运行。的问题是,它不会从一个标准的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.

我的code做某事的效果:

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中的code工作,在Windows Server 2003中的code不起作用。

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

推荐答案

尝试:

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

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

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