来自GAC的Import-Module用于PowerShell用法

问题描述 投票:8回答:2

我创建了一个PowerShell模块,如果我像这样加载它就可以正常工作

Import-Module "C:\temp\My.PowerShell.DocumentConversion.dll"

我确实在全局程序集缓存中注册了该模块,但无法从那里加载它。我已经验证了模块实际上已加载到gac中。我认为像这样加载模块就足够了

Import-Module My.PowerShell.DocumentConversion.dll

显然我错了,怎么做从gac运行powershell模块?

powershell gac
2个回答
18
投票

试试Add-Type cmdlet:

Add-Type -Assembly My.PowerShell.DocumentConversion

如果它不起作用,请尝试使用LoadWithPartialName方法:

[System.Reflection.Assembly]::LoadWithPartialName('My.PowerShell.DocumentConversion')

或者使用完整路径:

[System.Reflection.Assembly]::LoadFile(...)

2
投票

只要程序集在GAC中,只需使用强名称来引用程序集即可。要获得GAC的路径,请注意它已在.Net 4.0 http://en.wikipedia.org/wiki/Global_Assembly_Cache中发生了变化

$assemblyPath = 'path to the assembly file in the gac'
$fullName = [System.Reflection.AssemblyName]::GetAssemblyName($assemblyPath).FullName
[System.Reflection.Assembly]::Load($fullName)
© www.soinside.com 2019 - 2024. All rights reserved.