简体   繁体   中英

How do I iterate over PSObject in C#?

From my C# code, I'm calling a PowerShell file. This PowerShell file calls an API like this:

$tickets = Invoke-RestMethod -uri 'x'

return $tickets.value

From my C# code, I'm storing the returned result in a variable like this:

var tickets = pipeline.Invoke();

foreach (var ticket in tickets)
{
    System.Diagnostics.Debug.WriteLine(ticket.Status); //ERROR
}

I'm getting an error here saying that foreach cannot operate on variables of type System.Management.Automation.PSObject.

Printing 'tickets' returns this:

@{Id=581; CID=3; Status=Active}

@{Id=545; CID=6; Status=Active}

I need to be able to iterate through each ticket in tickets.

这是您的操作方式:

System.Diagnostics.Debug.WriteLine(ticket.Properties["Status"].Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM