Using assemblies

If an assembly is listed in the using statement for a script, it will be loaded. For example, System.Windows.Forms may be loaded in Windows PowerShell; the assembly is not available in PowerShell Core:

using assembly System.Windows.Forms

Add-Type is able to do much the same thing:

Add-Type -AssemblyName System.Windows.Forms

Assemblies loaded by name are stored in the Global Assembly Cache (GAC). The GAC is stored in $env:WINDIRAssembly. gacutil may be used to find assemblies within the cache:

gacutil /l System.Windows.Forms

The Gac module, on the PowerShell Gallery, provides a more consistent experience:

PS> Install-Module Gac -Scope CurrentUser
PS> Get-GacAssembly System.Windows.Forms

Name Version Culture PublicKeyToken PrArch
---- ------- ------- -------------- ------
System.Windows.Forms 2.0.0.0 b77a5c561934e089 MSIL
System.Windows.Forms 1.0.5000.0 b77a5c561934e089 None
System.Windows.Forms 4.0.0.0 b77a5c561934e089 MSIL

As shown in the preceding code block, more than one version of the same assembly can exist on a system. If a specific version is required, the full name of the assembly may be used:

using assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 

This full name is exposed by both gacutil and the Gac module, as shown here:

PS> Get-GacAssembly System.Windows.Forms | Select-Object FullName

FullName
--------
System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

The using assembly statement will load assemblies from a specific path, if one is supplied, as follows:

using assembly 'C:SomeDirsomeAssembly.dll' 

PowerShell allows the using assembly statement any number of times in a script, and more than one assembly can be loaded in a single script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.123