Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import-Module from GAC for PowerShell Usage

Tags:

powershell

gac

I have created a powershell module that works just fine if I load it like this

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

I did register the module in the global assembly cache as well but can't load it from there. I have verified that the module in fact is loaded in the gac. I figured that it would be sufficient to load the module like this

Import-Module My.PowerShell.DocumentConversion.dll

Obviously I was wrong, how do one do to run powershell modules from gac?

like image 593
Eric Herlitz Avatar asked Feb 12 '13 15:02

Eric Herlitz


People also ask

How do I import a PowerShell module?

To import the module into all sessions, add an Import-Module command to your PowerShell profile. To manage remote Windows computers that have PowerShell and PowerShell remoting enabled, create a PSSession on the remote computer and then use Get-Module -PSSession to get the PowerShell modules in the PSSession.

What is GAC in PowerShell?

The Global Assembly Cache (GAC) is a machine wide repository for . Net Assemblies. PowerShell GAC provides several PowerShell commands to view and modify the GAC. PowerShell GAC works standalone and does not depend on tools like gacutils.exe.

How do I use a PowerShell module?

How to Import a Module into Every Session. The Import-Module command imports modules into your current PowerShell session. To import a module into every PowerShell session that you start, add the Import-Module command to your PowerShell profile. For more information about profiles, see about_Profiles.


2 Answers

Try the Add-Type cmdlet:

Add-Type -Assembly My.PowerShell.DocumentConversion

If it's not working try the LoadWithPartialName method:

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

Or using the full path:

[System.Reflection.Assembly]::LoadFile(...)
like image 77
Shay Levy Avatar answered Sep 30 '22 12:09

Shay Levy


As long as the assembly is in the GAC, just use the strong name to reference the assembly. To get the path to the GAC be aware it has changed in .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)
like image 28
peterwiens Avatar answered Sep 30 '22 14:09

peterwiens