0

I have a C# function (MyFunction(List<SafeInfo> securitySchema)). I'm trying to write a powershell script that will create the List<SafeInfo> object and pass it into MyFunction(List<SafeInfo>) via reflection. Reflection's all good in PowerShell, but I'm trying to figure out the correct way to transpose the creation of the argument object into PowerShell.

Here's asample C# object that can be used:

List<SafeInfo> schema = new List<SafeInfo>() {
    new SafeInfo("mySafe1", new SecurityInfo("key1", SecurityType.Key, "some description")),
    new SafeInfo("mySafe2", new SecurityInfo("key2", SecurityType.Combination, "another description")),
    new SafeInfo("mySafe3", SecurityInfo.Unsecured)
};

My PowerShell so far:

$schema = New-Object 'Collections.Generic.List[MyNamespace.SafeInfo]'
$schema.Add(New-Object('MyNamespace.SafeInfo', 'mySafe1', New-Object('MyNamespace.SecurityInfo', 'key1', SecurityType.Key, 'some description')))
$schema.Add(New-Object('MyNamespace.SafeInfo', 'mySafe2', New-Object('MyNamespace.SecurityInfo', 'key2', SecurityType.Combination, 'another description')))
$schema.Add(New-Object('MyNamespace.SafeInfo', 'mySafe3', [MyNamespace.SecurityInfo]::Unsecured))

Am I on the right track? What's the best way to create this nested object in PowerShell? Thanks!

1 Answer 1

1

Am I on the right track?

Yes and no.

New-Object is a cmdlet, and cmdlet parameters are space-separated (and named) rather than comma-separated arguments like in C#.

Furthermore, with New-Object the type name and the argument list is two separate parameters

New-Object -TypeName 'MyNamespace.SafeInfo' -ArgumentList 'mySafe1',([MyNamespace.SecurityInfo]::Unsecured)

(notice how you can not resolve the static member Unsecured directly in the list, thus the () around)

You can't call a cmdlet directly inside a .NET method, but you can wrap the call in a subexpression ($()):

$schema.Add($(New-Object -TypeName 'MyNameSpace.SafeInfo' -ArgumentList 'mySafe1',$(New-Object -TypeName 'MyNameSpace.SafeInfo' -ArgumentList "key1", SecurityType.Key, "some description")))

If a parameter supports positional binding (as does -TypeName and -ArgumentList), you can omit the parameter name:

$schema.Add($(New-Object 'MyNameSpace.SafeInfo' 'mySafe1',$(New-Object 'MyNameSpace.SafeInfo' "key1",SecurityType.Key,"some description")))

Again, notice how the first parameter value ('MyNameSpace') and the second (the argument list) is separated by a space rather than a comma


With the above in mind, your examples become:

$schema = New-Object 'Collections.Generic.List[MyNamespace.SafeInfo]'
$schema.Add($(New-Object 'MyNamespace.SafeInfo' 'mySafe1',$(New-Object 'MyNamespace.SecurityInfo' 'key1',([MyNamespace.SecurityType]::Key),'some description')))
$schema.Add($(New-Object 'MyNamespace.SafeInfo' 'mySafe2',$(New-Object 'MyNamespace.SecurityInfo' 'key2',([MyNamespace.SecurityType]::Combination),'another description')))
$schema.Add($(New-Object 'MyNamespace.SafeInfo' 'mySafe3',([MyNamespace.SecurityInfo]::Unsecured)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.