Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access PSObject property by name in C#

For example I have a PSObject transaction with two properties: id and transactionName , so that it looks like: transaction { id: 123 transactionName : tranName1 }

and I want to return the id of the transaction if its name is tranName1.

It looks to me that in powershell scripts, we can simply do:

if $transaction.transactionName -eq tranName return $transaction.id

however in c# it will give error since it cannot recognize the property by name... any ideas how to do it in c#?

like image 241
jamesdeath123 Avatar asked Mar 07 '13 17:03

jamesdeath123


2 Answers

Try something like this:

psobjectvariable.Properties["transactionName"].Value
like image 192
Mike Shepard Avatar answered Sep 16 '22 21:09

Mike Shepard


Here's something that I didn't expect to work, but it did.

dynamic x = psobjectvariable;
Console.Write(x.transactionName);
like image 31
Ambrose Leung Avatar answered Sep 18 '22 21:09

Ambrose Leung