Declare a general list of third-party assembly type in Powershell 2

I am trying to create a new Collections.Generic.Listtype object Amazon.Cloudwatch.Model.Dimensionsin Powershell 2.0.

I can create an object Amazon.Cloudwatch.Model.Dimensionssuccessfully using

> $dimension = New-Object Amazon.Cloudwatch.Model.Dimensions

And I can create an object of Collections.Generic.Listtype stringwith:

> $list = New-Object Collections.Generic.List[string]

However, when I try to create an object of Collections.Generic.Listtype Amazon.Cloudwatch.Model.Dimensions, I get the following error:

> $list = New-Object Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension]

New-Object : Cannot find type 
[Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension]]: make sure the assembly containing this type is loaded.
At line:1 char:19
+ $list = New-Object <<<<  
Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension]
    + CategoryInfo          : InvalidType: (:) [New-Object], 
PSArgumentException
    + FullyQualifiedErrorId : 
TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

The same statement works on Powershell 3.0, so it seems to be caused by how Powershell 2.0 generators look for assemblies. According to another SO answer , there is an error in Powershell 2.0:

However, there is a bad catch. PowerShell 2.0 has an error when mixing and matching BCL and third-party types as type parameters. The latter must have assembly qualifications:

# broken over two lines for clarity with backtick escape
$o = new-object ('collections.generic.dictionary[[{0}],[{1}]]' -f `
    [type1].fullname, [type2].fullname)

, . PowerShell 3.0 .

() .

SO :

, . , :

$bar = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"

:

> $list = New-Object "System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]"

.

> $list = New-Object "System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]"
New-Object : Cannot find type 
[System.Collections.Generic.List`1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]]: make sure the assembly containing this type is loaded.
At line:1 char:22
+ $dimlist = New-Object <<<<  
"System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]"
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

.

: Amazon.Cloudwatch.Model.Dimensions AWS SDK .NET

+4
3

, powershell 2.0, , :

Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.CloudWatch.dll"
$o = "System.Collections.Generic.List``1" -as "Type"
$o = $o.MakeGenericType("Amazon.Cloudwatch.Model.Dimension" -as "Type")
$list = [Activator]::CreateInstance($o)

$list.gettype() | select UnderlyingSystemType

UnderlyingSystemType
--------------------
System.Collections.Generic.List`1[Amazon.CloudWatch.Model.Dimension]
+2

. , .

, , , , .

#Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.CloudWatch.dll"
$list = New-Object Collections.Generic.List[System.Object]
$d = $dims = New-Object Amazon.Cloudwatch.Model.Dimension
$d.name = "AutoScalingGroup"
$d.value = "xy"
$list.Add($dim)
$list | Get-Member

. ?

enter image description here

+1
$list = [Activator]::CreateInstance([System.Collections.Generic.List`1].MakeGenericType([Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]))

Of course, you need to load assemblies earlier or add them to the GAC. In my test, I downloaded all the assemblies from C: \ Program Files (x86) \ AWS SDK for .NET \ bin \ Net45.

foreach ($file in Get-ChildItem -Path 'C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\' -Filter '*.dll')
{
    [Reflection.Assembly]::LoadFile($file.FullName) | Out-Null
}
0
source

Source: https://habr.com/ru/post/1684999/


All Articles