Repository navigation
Expand file tree
/
Copy pathPSObjectExtension.cs
More file actions
64 lines (60 loc) · 2.53 KB
/
Copy pathPSObjectExtension.cs
File metadata and controls
64 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
{
/// <summary>
/// Extends the PSObject with C# convenience
/// </summary>
public static class PSObjectExtension
{
/// <summary>
/// Get the value!
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="PSObject">The object to extend</param>
/// <param name="Name">The name of the property that has the value</param>
/// <returns>The value</returns>
public static T GetValue<T>(this PSObject PSObject, string Name)
{
PSObject value = PSObject.AsPSObject(PSObject.Properties[Name].Value);
return (T)value.BaseObject;
}
/// <summary>
/// Get the Values!
/// Always as an arraylist, empty if null
/// </summary>
/// <param name="PSObject">The object to extend</param>
/// <param name="Name">>The name of the property that has the values</param>
/// <returns>The values</returns>
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;
}
/// <summary>
/// Get a hashtable value!
/// </summary>
/// <typeparam name="T">The type of the values in hashtable</typeparam>
/// <param name="PSObject">The object to extend</param>
/// <param name="Name">The name of the property that has the hashtable</param>
/// <returns>The hashtable!</returns>
public static Dictionary<string, ParameterScript> GetParameterScriptDictionary(this PSObject PSObject, string Name)
{
Hashtable temp = PSObject.GetValue<Hashtable>(Name);
Dictionary<string, ParameterScript> result = new Dictionary<string, ParameterScript>();
foreach (DictionaryEntry pair in temp)
result[(string)pair.Key] = new ParameterScript((PSObject)pair.Value);
return result;
}
}
}