2013-02-12 34 views
7

tôi đã tạo ra một mô-đun PowerShell mà làm việc tốt nếu tôi tải nó như thế nàyImport-Module từ GAC cho PowerShell Cách sử dụng

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

tôi đã đăng ký mô-đun trong Global Assembly Cache cũng nhưng không thể tải nó từ đó. Tôi đã xác minh rằng mô-đun trong thực tế được tải trong gac. Tôi thấy rằng sẽ đủ để tải mô-đun như thế này

Import-Module My.PowerShell.DocumentConversion.dll 

Rõ ràng là tôi đã sai, làm cách nào để chạy mô-đun PowerShell từ gac?

Trả lời

15

Thử Add-Type cmdlet:

Add-Type -Assembly My.PowerShell.DocumentConversion 

Nếu nó không làm việc thử các phương pháp LoadWithPartialName:

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

Hoặc sử dụng đường dẫn đầy đủ:

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

Hoặc sử dụng đường dẫn đầy đủ: [System.Reflection.Assembly] :: LoadFrom (...) – RinoTom

2

Chừng nào lắp ráp nằm trong GAC, chỉ cần sử dụng tên mạnh để tham chiếu đến assembly. Để có được đường dẫn đến GAC phải nhận thức nó đã thay đổi trong 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) 
Các vấn đề liên quan