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

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'
)

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?

You can also specify just the assembly without the version info:
Add-Type -AssemblyName "System.Xml.Linq"

Related

PowerShell using netcoreapp3.0 assembly: Add-Type fails with "Could not load type 'System.Object' from assembly 'System.Private.CoreLib"

I need to use one of my dotnet core 3 assemblies within a powershell script:
try {
Add-Type -Path "app.dll"
}
catch {
$_.Exception.LoaderExceptions
}
This fails:
Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does
not exist.
Why I try to Add-Type System.Private.CoreLib.lib, the same error occurs.
It seems to work well with a netstandard2.0 assembly, but not with my netcoreapp3.0 assembly.
What can I do in order to use the methods of my assembly in the powershell script?
If you need to load in PowerShell .NET assemblies that are targeting above netstandard2.0 version, then you should try to execute your script in PowerShell Core 7.1.3 or above.
Windows PowerShell might be able to load only .NET assemblies that are targeting .NET Framework or netstandard2.0.

PowerShell how to add-type from GAC

I try to call Add-Type -AssemblyName myassembly, and myassembly has been registered into GAC, but I always got following error:
Cannot add type. The assembly 'myassembly' could not be found.
I know how to Add-Type from a file location, is there a way to Add-Type from GAC conveniently?
To load from the GAC you need the fully qualified assembly name:
Add-Type -AssemblyName "Microsoft.Web.Deployment, Version=9.0.0.0", Colture=netural, PublicKeyToken=89956cdc9090cc76"
And yea, it's really annoying!
You can also use [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Deployment') Yes I know its obsolete and you should use Load().
Often you may not have the full name and don't care to enumerate the assemblies in the GAC. This will allow you to load the assembly. If you care, you can then pull the fullname to use with Load() for your scripts going forward.
[System.Reflection.Assembly]::LoadWithPartialName('system.web.security')
PS c:\> [System.Web.Security.Membership].Assembly.Fullname
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
If you copy it to another computer, you have to unblock it.

Using Accord.Imaging in UWP

(apologies for beginner question) I am trying to use Accord.Net Imaging function in an UWP windows 10 App. Any function I try gives me an error:
The type 'Bitmap' is defined in an assembly that is not referenced. You must add a reference to assembly 'CoreCompat.System.Drawing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.The type 'Bitmap' is defined in an assembly that is not referenced. You must add a reference to assembly 'CoreCompat.System.Drawing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
although I included Using System.Drawing (which includes Bitmap)
UnmanagedImage unmanagedImage = UnmanagedImage.FromByteArray(bytes);
If you're using NuGet, you can install the following package to get System.Drawing, and thus access to the Bitmap class: https://www.nuget.org/packages/CoreCompat.System.Drawing.v2/5.2.0-preview1-r131
Note that this is a prerelease version, so you'll need to check the box that says "Include prerelease" next to the search bar if using the Visual Studio NuGet GUI.

Module import loads wrong assembly version

I have a module manifest that specifies an explicit path to some Visual Studio 2015 assemblies (version 14.0.0.0) in the "RequiredAssemblies" array. After running Import-Module I am getting the 12.0.0.0 version assemblies added to the session instead.
RequiredAssemblies = #(
"${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\TestAgent\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
)
After running the following command:
[appdomain]::currentdomain.getassemblies() | sort -property fullname | format-table fullname
I can see that only the 12.0.0.0 version of the assembly was added to my current session:
Microsoft.TeamFoundation.WorkItemTracking.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
I have ensured that the *.dll at the path is the version I am looking for... Why is powershell forcing the previous version? Is there any way to prevent it from doing so?

How to install ajaxControlToolKit.dll in gac

I have two different version dll of ajaxcontroltoolkit in a project, i can not put both in bin folder so i need to install them in gac. How to do it.
In order to add an assembly into the GAC, the assembly needs to be strongly named.
If you have the code, you can do it and then add it to the GAC.
If you don't have the code but the assembly itself, you still can strong name the assembly using the Assembly Linker :
http://msdn.microsoft.com/en-us/library/xc31ft41.aspx
al /out:MyAssembly.dll MyModule.netmodule /keyfile:sgKey.snk
Regards