title: Retrieving the values of a nested Powershell object via properties in C#
body: <p>C# Newbie here. I am not sure if what I'm going for is even possible.
  Basically, I'm trying to read the properties of a system object array <strong>within</strong> a Powershell object in C#.</p>
  
  <p>For simplicity's sake, here is a basic Powershell code that has a similar output:</p>
  
  <pre><code>$object = New-Object -TypeName PSObject
  $object | Add-Member -MemberType NoteProperty -Name "Name" -Value "Yad"
  $object | Add-Member -MemberType NoteProperty -Name "Title" -Value "C# Noob"
  $object | Add-Member -MemberType NotePropety -Name "Machine Services" -Value (Get-Service)
  
  return $object
  </code></pre>
  
  <p>On the C# side of things, I am able to fetch the results by mapping the properties as below:</p>
  
  <pre><code>var shell = PowerShell.Create();
  shell.Commands.AddScript("C:\\Scripts\\Powershell\\TestScript.ps1 -user " + userName);
  
  Collection&lt;PSObject&gt; results = shell.Invoke();
  
  Profile userProfile = new Profile(); //Profile class is declared prior this line
  
  
  foreach (PSObject psObject in results)
          {
              userProfile.Name = Convert.ToString(psObject.Properties["Name"].Value);
              userProfile.Title= Convert.ToString(psObject.Properties["Title"].Value);
              userProfile.MachineServices= psObject.Properties["Machine Services"].Value;
           }
  </code></pre>
  
  <p>With this, the 'userProfile' object is equivalent to the Powershell output.</p>
  
  <p>Now, the 'MachineServices' property is an object that contains its own set of properties (status, name, and displayname in Powershell). Is it possible to call these properties and retrieve their values?</p>
  
  <p>I tried something similar to below but, of course, I get an error as the Powershell script is completely separate and the properties are unknown to the C# code prior runtime.</p>
  
  <pre><code>userProfile.MachineServices.Status
  </code></pre>
  
  <p>Any ideas?</p>