Using computed PowerShell properties from C #

So, I want to execute the calculated PowerShell properties (hope the correct name) from C #.

My CmdLet in the PS (Exchange) console looks like this:

Get-Something -ResultSize unlimited |Select-Object DisplayName,@{name="RenamedColumn;expression={$_.Name}},ToBeExpanded -Expand ToBeExpanded |Select-Object DisplayNameRenamedColumn,ToBeExpandedGuid

This works fine, the problem occurs when I try to execute it with C #.

My code is as follows:

List<string> parameters = new List<string>()
            {
                "DisplayName","@{{name=\"RenamedColumn\";expression={{$_.Name }} }}","ToBeExpanded"
            };
List<string> parameters2 = new List<string>()
            {
                "DisplayName","RenamedColumn","ToBeExpanded","ToBeExpandedGuid"
            };
    powershell.AddCommand("Get-Something");
    powershell.AddParameter("ResultSize","unlimited");
    powershell.AddCommand("Select-Object");
    powershell.AddParameter("Property",parameters);
    powershell.AddParameter("ExpandProperty","ToBeExpanded");
    powershell.AddCommand("Select-Object");
    powershell.AddParameters("Property",parameters2);

    result = powershell.Invoke();

Then my result contains ToBeExpandedGuidnull. So I tried the command without a second choice, and it shows me that it has a column:

@{{name=\"RenamedColumn\";expression={{$_.Name }} }}

So, I thought powershell would not recognize this renaming ... Now my question is, how can I use something like this with C #? Is there any solution?

+4
source share
2 answers

PowerShell @{Name="RenamedColumn";Expression={$_.Name}} , Hashtable, # Hashtable ( , IDictionary), Select-Object:

new Hashtable{
    {"Name","RenamedColumn"},
    {"Expression",ScriptBlock.Create("$_.Name")}
}

P.S.
, , , ScriptBlock :

@{Name="RenamedColumn";Expression="Name"}

new Hashtable{
    {"Name","RenamedColumn"},
    {"Expression","Name"}
}
+2

@PetSerAl

:

         List<object> parameters = new List<object>()
                {
                    "Name",
                    new Hashtable()
                    {
                        {"Name","someName"},
                        {"Expression",ScriptBlock.Create("$_.SomeColumn.Child -join ','")}
                    }
                };
            powershell.AddCommand("Get-Something");
            powershell.AddCommand("Select-Object");
            powershell.AddParameter("Property", parameters);

            powershell.Runspace.Open();
            Collection<PSObject> powershellResult = powershell.Invoke();
            powershell.Runspace.Close();

, , ( ...)

0