Example #1
0
        // part 2c
        private static InfoParts HandleResults(string server, string dataset, string nameid,
                                               string filename, FileSetup.LineTrimming trimOptions, string filenotfound, string comment)
        {
            var      expandedFileName = Environment.ExpandEnvironmentVariables(nameid);
            FileInfo file             = new FileInfo(expandedFileName);
            var      filePath         = file.DirectoryName;
            var      simplefilename   = file.Name;
            var      parts            = new InfoParts(server, "file", DateTime.UtcNow);
            int      index            = 0;

            if (file.Exists)
            {
                using (FileStream fs = file.OpenRead()) // this shares with open files, no crash
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        while (!sr.EndOfStream)
                        {
                            //var line = Utilities.RemoveAfter(sr.ReadLine().re, comment);
                            var    line = sr.ReadLine().RemoveAfter(comment);
                            string val  = TrimAsNeeded(line, trimOptions);
                            if (val.Length > 0)
                            {
                                ++index;
                                parts.Add(string.Empty, filePath, simplefilename, index, dataset, "String", val);
                            }
                        }
                    }
                if (parts.Parts.Count < 1)
                {
                    parts.Add(simplefilename, index, dataset, "String", comment);  // add comment line if there was no uncommented data in the file
                }
            }
            else
            {
                parts.Add(simplefilename, index, dataset, "String", filenotfound);
            }

            return(parts);
        }
Example #2
0
        private void PSPropertyInfoOfTypeToList <T>(InfoParts parts, string nameid, string name, int index, PSObject obj)
            where T : PSPropertyInfo
        {
            //bool indexNeedsUpdate = false;
            foreach (T prop in obj.Properties.OfType <T>())
            {
                if (prop.IsGettable)
                {
                    //indexNeedsUpdate = true;
                    var nam = prop.Name;        // __GENUS ; __CLASS; __SUPERCLASS _RELPATH
                                                //var pstyp = prop.MemberType; // pstype.property
                    if (nam == nameid)
                    {
                        continue;                                                    // skip this value, it is being handled differently
                    }
                    var    propValTyp = prop.TypeNameOfValue.Replace("System.", ""); // system.int32; system.string
                    string val;

                    if (null == prop.Value)
                    {
                        continue;
                    }
                    else if (propValTyp.Equals("IntPtr"))
                    {
                        continue;                                 // IntPtr!- remove
                    }
                    else if (propValTyp.Equals("DateTime"))       // round trip format
                    {
                        val = ((DateTime)prop.Value).ToString("o");
                    }
                    else
                    {
                        val = prop.Value?.ToString();
                    }
                    if (!(propValTyp == val.Replace("System.", "")))
                    {
                        //sb.AppendLine($"'name':'{nam}';'type':'{propValTyp}';'value':'{val.Trim()}'");
                        parts.Add(name, index, nam, propValTyp, val.Trim());
                    }
                }
                //Console.WriteLine(prop.ToString());
                //int ix = 0;
            }
            //if (indexNeedsUpdate) index++;
            //return index;
        }
Example #3
0
        private InfoParts HandleResults(string server, string dataset, string nameid, Collection <PSObject> results)
        {
            var parts = new InfoParts(server, dataset, DateTime.UtcNow);


            // convert the script result into a single string
            int index = 0;

            //StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                //stringBuilder.AppendLine(obj.ToString());
                var name = obj.Properties[nameid].Value.ToString();
                index++;
                //PSPropOfTypeToString<PSAdaptedProperty>(stringBuilder, obj); //PSProperty
                PSPropertyInfoOfTypeToList <PSAliasProperty>(parts, nameid, name, index, obj); //PSPropertyInfo
                PSPropertyInfoOfTypeToList <PSNoteProperty>(parts, nameid, name, index, obj);  //PSPropertyInfo
                //PSPropertyInfoOfTypeToString<PSScriptProperty>(stringBuilder, obj); // PSPropertyInfo


                //var psProps = from Properties in obj where TypeName(Property) == "" select obj.Properties;
                //var psProps = obj.Properties.OfType<PSProperty>();
                foreach (PSProperty prop in obj.Properties.OfType <PSProperty>())
                {
                    if (prop.IsGettable)
                    {
                        var nam = prop.Name;        // __GENUS ; __CLASS; __SUPERCLASS _RELPATH
                        if (nam == name)
                        {
                            continue;              // this detail can be skipped, it is handled specially
                        }
                        if (nam.StartsWith("__"))
                        {
                            continue;                       // bail out on __ items.
                        }
                        //var pstyp = prop.MemberType; // pstype.property
                        // Property system.management.automation.psmembertypes.property
                        var    propValTyp = prop.TypeNameOfValue.Replace("System.", ""); // system.int32; system.string
                        var    valtyp     = prop.Value?.GetType();                       // .name=int32; .fullname = system.int32 string[]
                        string val;
                        if (null == prop.Value)
                        {
                            continue;
                        }
                        else if (nam.ToLower().Equals("RawData"))
                        {
                            continue;
                        }
                        else if (propValTyp.StartsWith("Management."))
                        {
                            continue;
                        }
                        else if (valtyp.IsArray)
                        {
                            // https://www.codeproject.com/Articles/93260/Retrieving-Information-From-Windows-Management-Ins
                            // fails for value type types
                            if (valtyp.Name.ToLower().StartsWith("int16"))
                            {
                                val = ConvertArrayToString <Int16>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("int32"))
                            {
                                val = ConvertArrayToString <Int32>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("int64"))
                            {
                                val = ConvertArrayToString <Int64>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("uint16"))
                            {
                                val = ConvertArrayToString <UInt16>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("uint32"))
                            {
                                val = ConvertArrayToString <UInt32>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("uint64"))
                            {
                                val = ConvertArrayToString <UInt64>(prop);
                            }
                            else if (valtyp.Name.ToLower().StartsWith("byte"))
                            {
                                val = "Byte[]"; //ConvertArrayToString<UInt64>(prop);
                            }
                            else if (valtyp.Name.StartsWith("DateTime"))
                            {
                                object[] ary    = (object[])prop.Value;
                                string[] result = ary.Where(x => x != null)
                                                  .Select(x => ((DateTime)x).ToString("o")).ToArray();
                                val = $"{{\"{string.Join("\",\"", result)}\"}}";
                            }
                            else
                            {
                                object[] ary    = (object[])prop.Value;
                                string[] result = ary.Where(x => x != null)
                                                  .Select(x => x.ToString()).ToArray();
                                val = $"{{\"{string.Join("\",\"", result)}\"}}";
                            }
                        }
                        else if (propValTyp.Equals("DateTime"))       // round trip format
                        {
                            val = ((DateTime)prop.Value).ToString("o");
                        }
                        else
                        {
                            val = prop.Value.ToString();
                        }
                        if (!(propValTyp == val.Replace("System.", "")))
                        {
                            //stringBuilder.AppendLine($"'name':'{nam}';'type':'{propValTyp}';'value':'{val.Trim()}'");
                            parts.Add(name, index, nam, propValTyp, val.Trim());
                        }
                    }
                }
            }
            return(parts);
        }