Getting values ​​of a nested Powershell object through properties in C #

C # Newbie here. I’m not sure that what I’m going to do is even possible. Basically, I'm trying to read the properties of an array of system objects inside a Powershell object in C #.

For simplicity, here is the basic Powershell code that has similar output:

$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

On the C # side of things, I can get results by matching properties, as shown below:

var shell = PowerShell.Create();
shell.Commands.AddScript("C:\\Scripts\\Powershell\\TestScript.ps1 -user " + userName);

Collection<PSObject> 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;
         }

The userProfile object is equivalent to Powershell output.

Now the "MachineServices" property is an object that contains its own set of properties (status, name and display name in Powershell). Is it possible to call these properties and get their values?

- , , , , Powershell script #.

userProfile.MachineServices.Status

?

+4
1

Get-Service PowerShell .GetType(), , Object, ServiceController.

#, Profile.MachineServices System.ServiceProcess.ServiceController[],

userProfile.MachineServices= (System.ServiceProcess.ServiceController[])(psObject.Properties["Machine Services"].Value);

# , , IntelliSense ..

+1

All Articles