Powershell Calling .NET Assembly that uses App.config

耗尽温柔 提交于 2020-01-09 04:22:05

问题


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.

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.

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).

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

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


回答1:


Try:

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



回答2:


After researching further I found out the cause. At an earlier point in the script I was loading SMO:

$null = [reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smo") 

I believe this some how mangles my configuration settings. The fix was to do as Chris mentions above to this call first:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null)
$null = [reflection.assembly]::loadwithpartialname("microsoft.sqlserver.smo") 

And then on my second call to another assembly do this:

$config_path = $assembly_exe + ".config"
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)
[Reflection.Assembly]::LoadFrom($assembly_exe)

Problem appears to be solved...



来源:https://stackoverflow.com/questions/835862/powershell-calling-net-assembly-that-uses-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!