Writing C# .net code in PowerShell

Hi

I am currently working through some webservice calls wrote in C# .net and converting these to PowerShell. This has been going ok until I got to the following:

test.systemFields = new systemFieldsType[2]

            int j = 0;
            metadata.systemFields[j].name = "name";
            metadata.systemFields[j++].value = "test name";
            metadata.systemFields[j].name = "type";
            metadata.systemFields[j++].value = "test";

In PowerShell I am trying:

$test.systemFields = New-Object systemFieldsType[2]

I can see systemFieldsType as an option but it doesn’t like me adding [2].

Any ideas how I deal with this in PowerShell?

Many thanks
Paul

You’re creating an array with two elements. That’s shorthand for calling the array constructor with an argument of two, which looks like this in PowerShell:

$test.systemFields = New-Object -TypeName systemFieldsType[] -ArgumentList 2

# Or as I usually write it, out of habit:

$test.systemFields = New-Object systemFieldsType[](2)

I know that second syntax is sort of me wishing that New-Object was just like the new statement in C#, but I can’t break the habit. That’s what New should look like. :stuck_out_tongue:

Hi Dave

Thanks for the reply.

When I run :
$test.systemFields = New-Object -TypeName systemFieldsType[]

and then:
$test.systemFields

I get the ‘name’ and ‘value’ headers. However, when I run:
$test.systemFields = New-Object -TypeName systemFieldsType[] -ArgumentList 2

and then:
$test.systemFields

I don’t get the headers and cannot set the following:

int j = 0;
metadata.systemFields[j].name = “name”;
metadata.systemFields[j++].value = “test name”;
metadata.systemFields[j].name = “type”;
metadata.systemFields[j++].value = “test”;

Any ideas?

Many thanks
Paul