Пример #1
0
 public static void SendProperties(WMIClassInfo wmi, CommandBody cmd)
 {
     Task.Run(() =>
     {
         Telnet.Instance.Send(new CommandBody(Common.ApiCommand.WMI_Props, cmd.AId, Global.CurrentClient.ID, GetProperties(wmi)));
     });
 }
Пример #2
0
        public static WMIObjectCollection GetProperties(WMIClassInfo wmi)
        {
            WMIObjectCollection collection = new WMIObjectCollection()
            {
                Namespace = wmi.Namespace,
                Class     = wmi.Class
            };
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmi.Namespace,
                                                                             "select * from " + wmi.Class);
            int i = 0;
            ManagementObjectCollection result = null;

            try
            {
                result = searcher.Get();
                foreach (ManagementObject obj in result)
                {
                    WMIPropertyCollection props = new WMIPropertyCollection()
                    {
                        Index = ++i,
                        Name  = obj.GetPropertyValue("Name").ToString(),
                    };
                    collection.Add(props);
                    foreach (PropertyData prop in obj.Properties)
                    {
                        string value = null;
                        if (prop.Name == "Capabilities")
                        {
                        }
                        if (prop.Value != null)
                        {
                            if (prop.IsArray)
                            {
                                switch (prop.Type)
                                {
                                case CimType.None:
                                    value = null;
                                    break;

                                case CimType.SInt8:
                                    value = string.Join(" | ", prop.Value as sbyte[]);
                                    break;

                                case CimType.UInt8:
                                    value = string.Join(" | ", prop.Value as byte[]);
                                    break;

                                case CimType.SInt16:
                                    value = string.Join(" | ", prop.Value as short[]);
                                    break;

                                case CimType.UInt16:
                                    value = string.Join(" | ", prop.Value as ushort[]);
                                    break;

                                case CimType.SInt32:
                                    value = string.Join(" | ", prop.Value as int[]);
                                    break;

                                case CimType.UInt32:
                                    value = string.Join(" | ", prop.Value as uint[]);
                                    break;

                                case CimType.SInt64:
                                    value = string.Join(" | ", prop.Value as long[]);
                                    break;

                                case CimType.UInt64:
                                    value = string.Join(" | ", prop.Value as ulong[]);
                                    break;

                                case CimType.Real32:
                                    value = string.Join(" | ", prop.Value as float[]);
                                    break;

                                case CimType.Real64:
                                    value = string.Join(" | ", prop.Value as double[]);
                                    break;

                                case CimType.Boolean:
                                    value = string.Join(" | ", prop.Value as bool[]);
                                    break;

                                case CimType.String:
                                    value = string.Join(" | ", prop.Value as string[]);
                                    break;

                                case CimType.DateTime:
                                    value = string.Join(" | ", prop.Value as DateTime[]);
                                    break;

                                case CimType.Reference:
                                    value = string.Join(" | ", prop.Value as short[]);
                                    break;

                                case CimType.Char16:
                                    value = string.Join(" | ", prop.Value as char[]);
                                    break;

                                case CimType.Object:
                                    value = string.Join(" | ", prop.Value as object[]);
                                    break;
                                }
                            }
                            else
                            {
                                value = prop?.Value?.ToString();
                            }
                        }
                        props.Add(new WMIPropertyInfo()
                        {
                            Name = prop.Name, Value = value
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                collection.Add(new WMIPropertyCollection()
                {
                    Name = "发生异常:" + ex.Message
                });
            }

            return(collection);
        }