0
public class MyInfo        
{
 public string Status { get; set; }
 public string Class { get; set; }
 public string FriendlyName { get; set; }
 public string InstanceId { get; set; }
}

string strScript = "Get-PnpDevice";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(strScript);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();

foreach (PSObject pSObject in results)
{

}

After JsonConvert.SerializeObject(pSObject);

{"CliXml":"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">\r\n  <S>_x000D__x000A_Status     Class           FriendlyName                                                                     InstanceId  _x000D__x000A_------     -----           ------------                                                                     ----------  _x000D__x000A_Unknown    HIDClass        HID-compliant consumer control device                                            HID\\VID_0..._x000D__x000A_OK         System          System board                                                                     ACPI\\PNP0..._x000D__x000A_OK         System          Motherboard resources                                                            ACPI\\PNP0..._x000D__x000A_OK         System          Motherboard resources                                                                                                                        SWD\\MSRRA..._x000D__x000A_Unknown    HIDClass        HID-compliant vendor-defined device                                              HID\\VID_0..._x000D__x000A__x000D__x000A__x000D__x000A_</S>\r\n</Objs>"}

But I need is MyClasss.Class so i can diferentiate or make some condition on it .

saudblaze
  • 150
  • 10
  • Here is an example that converts WMI data to a custom class - the same technique should work in your case: [Dealing with CimObjects with PowerShell inside C#](https://stackoverflow.com/questions/51099688/dealing-with-cimobjects-with-powershell-inside-c-sharp/51103104#51103104) – boxdog Feb 17 '22 at 07:50

1 Answers1

0

1 => Create this classes

public class HardwareDevice
{
    public string Name { get; set; }        
    public List<HardwareProperty> Properties { get; set; }
    public HardwareDevice()
    {
        Properties = new List<HardwareProperty>();
    }
}

public class HardwareProperty
{
    public string Name { get; set; }
    public string Value { get; set; }        
}

2 = > Call this method which return List

var hardware = GetHardwareInfo(string "select * from Get-PnpDevice", string scope = "ROOT\\CIMV2")

3 = > here is the method

List<HardwareDevice> GetHardwareInfos(string query, string scope = "ROOT\\CIMV2")
    {
        List<HardwareDevice> hardwares = new List<HardwareDevice>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            foreach (ManagementObject share in searcher.Get())
            {
                HardwareDevice hrd = new HardwareDevice();
                try
                {
                    if (share["Name"] != null)
                        hrd.Name = share["Name"].ToString().Trim();
                    else
                        hrd.Name = share.ToString().Trim();
                }
                catch
                {
                    hrd.Name = share.ToString().Trim();
                }
                foreach (PropertyData PC in share.Properties)
                {
                    HardwareProperty property = new HardwareProperty();
                    property.Name = PC.Name;

                    if (PC.Value != null && PC.Value.ToString() != "")
                    {
                        switch (PC.Value.GetType().ToString())
                        {
                            case "System.String[]":
                                string[] str = (string[])PC.Value;
                                string str2 = "";
                                foreach (string st in str)
                                    str2 += st + "&";

                                property.Value = str2.Trim('&');
                                break;
                            case "System.UInt16[]":
                                ushort[] shortData = (ushort[])PC.Value;
                                string tstr2 = "";
                                for (int i = 0; i < shortData.Length; i++)
                                {
                                    tstr2 += shortData[i].ToString() + "&";
                                }
                                property.Value = tstr2.Trim('&');
                                break;

                            default:
                                property.Value = PC.Value.ToString().Trim();
                                break;
                        }
                    }
                    hrd.Properties.Add(property);
                }
                hardwares.Add(hrd);
            }
        }
        catch (Exception exp)
        {

        }
        return hardwares;
    }
saudblaze
  • 150
  • 10