Access PSObject property by name in C# - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: PowerShell & .ps1 (https://zeroday.vip/Forum-PowerShell-ps1) +--- Thread: Access PSObject property by name in C# (/Thread-Access-PSObject-property-by-name-in-C) |
Access PSObject property by name in C# - antispeculation435043 - 07-21-2023 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#? RE: Access PSObject property by name in C# - tremayne5 - 07-21-2023 Try something like this: psobjectvariable.Properties["transactionName"].Value RE: Access PSObject property by name in C# - lavernlaverna990 - 07-21-2023 Here's something that I didn't expect to work, but it did. ``` dynamic x = psobjectvariable; Console.Write(x.transactionName); ``` |