Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference an assembly from the GAC in a PowerShell module?

Tags:

powershell

gac

I'm writing a PowerShell module which depending on the SMO assemblies in SQL Server. (The only one I need to reference is Microsoft.SqlServer.Smo.dll)

When I've been developing module. I've just taken a copy of the assembly I need and referenced it in my manifest file similar to this:

RequiredAssemblies = @(         
    "$env:userprofile\Documents\WindowsPowerShell\Modules\Dependencies\Microsoft.SqlServer.Smo.dll"
)

I would assume that in a production environment you'd want to reference assemblies from a standard location. I'd assume a standard location would be the GAC.

If I reference the assembly from the GAC, I get this, but this looks like it could break if a new version of the assembly is installed:

RequiredAssemblies = @(
    'C:\WINDOWS\assembly\gac_msil\Microsoft.SqlServer.Smo\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll'
)

I could also reference the assembly directly from the SDK:

RequiredAssemblies = @(
    'C:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll'
)

Another option I've considered (but possibly might be breaking a EULA somewhere) is to copy the assemblies to a server share and reference that like so:

RequiredAssemblies = @(
    '\\MyServer\PowerShellDependencies\Microsoft.SqlServer.Smo.dll'
)

But how should I be doing this?

If it's relevant, all computers that this module will be installed on are 64-bit and will have the SMO libraries installed. Also, this isn't a publicly available piece of software, it's being deployed on a company network.

Update: I've tried only specifying the name of the assembly in the manifest and this appears to work.

RequiredAssemblies = @(
    'Microsoft.SqlServer.Smo.dll'
)
like image 873
Jake Avatar asked Jan 26 '26 08:01

Jake


1 Answers

Unless you're going to deploy the SMO assemblies as private assemblies, then I wouldn't recommend loading them from direct references.

To load from the GAC, use the Add-Type cmdlet with the fully qualified assembly name...

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

Related: How do I use Add-Type to load Microsoft.Web.Deployment?

like image 148
Rich Rousseau Avatar answered Jan 29 '26 02:01

Rich Rousseau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!