using PSModuleDevelopment.Template; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Management.Automation; using System.Text; using System.Threading.Tasks; namespace PSModuleDevelopment.Utility { /// /// Extends the PSObject with C# convenience /// public static class PSObjectExtension { /// /// Get the value! /// /// The type of the value /// The object to extend /// The name of the property that has the value /// The value public static T GetValue(this PSObject PSObject, string Name) { PSObject value = PSObject.AsPSObject(PSObject.Properties[Name].Value); return (T)value.BaseObject; } /// /// Get the Values! /// Always as an arraylist, empty if null /// /// The object to extend /// >The name of the property that has the values /// The values public static ArrayList GetValues(this PSObject PSObject, string Name) { if (null == PSObject || null == PSObject.Properties || null == PSObject.Properties[Name] || null == PSObject.Properties[Name]?.Value) return new ArrayList(); PSObject value = PSObject.AsPSObject(PSObject.Properties[Name].Value); if (null == value.BaseObject) return new ArrayList(); return (ArrayList)value.BaseObject; } /// /// Get a hashtable value! /// /// The type of the values in hashtable /// The object to extend /// The name of the property that has the hashtable /// The hashtable! public static Dictionary GetParameterScriptDictionary(this PSObject PSObject, string Name) { Hashtable temp = PSObject.GetValue(Name); Dictionary result = new Dictionary(); foreach (DictionaryEntry pair in temp) result[(string)pair.Key] = new ParameterScript((PSObject)pair.Value); return result; } } }