Add-Type in a function contained in a module

蓝咒 提交于 2019-12-10 18:15:52

问题


I'm not sure why this is failing, but if I Add-Type from a function then for whatever reason my code fails when I need to use the *.dll api.

function Add-References
{
    Add-Type -Path "C:\myDllPath.dll"
    Add-Type -Path "C:\myDllPath2.dll"
}

I have tried making the function global, the dot operator... Keep in mind this function is contained in a module and is being called by a script. My goal is to just use the function to declare my references rather than copy paste these paths in all of my scripts.


回答1:


It would help if you included the error you're receving.

But I suggest an alternative: use the RequiredAssemblies key in your module manifest:

Specifies the assembly (.dll) files that the module requires. Enter the assembly file names. Windows PowerShell loads the specified assemblies before updating types or formats, importing nested modules, or importing the module file that is specified in the value of the RootModule key.

Use this parameter to list all the assemblies that the module requires. This includes assemblies that must be loaded to update any formatting or type files that are listed in the FormatsToProcess or TypesToProcess keys, even if those assemblies are also listed as binary modules in the NestedModules key.



来源:https://stackoverflow.com/questions/38246413/add-type-in-a-function-contained-in-a-module

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