1
+ # Copyright (c) Microsoft Corporation.
2
+ # Licensed under the MIT License.
3
+
4
+ [CmdletBinding ()]
5
+ param (
6
+ [ValidateSet (' List' , ' Get' , ' Set' , ' Test' )]
7
+ $Operation = ' List' ,
8
+ [Parameter (ValueFromPipeline )]
9
+ $stdinput
10
+ )
11
+
12
+ $ProgressPreference = ' Ignore'
13
+ $WarningPreference = ' Ignore'
14
+ $VerbosePreference = ' Ignore'
15
+
16
+ if ($Operation -eq ' List' )
17
+ {
18
+ $clases = Get-CimClass
19
+
20
+ foreach ($r in $clases )
21
+ {
22
+ $version_string = " " ;
23
+ $author_string = " " ;
24
+ $moduleName = " " ;
25
+
26
+ $propertyList = @ ()
27
+ foreach ($p in $r.CimClassProperties )
28
+ {
29
+ if ($p.Name )
30
+ {
31
+ $propertyList += $p.Name
32
+ }
33
+ }
34
+
35
+ $namespace = $r.CimSystemProperties.Namespace.ToLower ().Replace(' /' , ' .' )
36
+ $classname = $r.CimSystemProperties.ClassName
37
+ $fullResourceTypeName = " $namespace /$classname "
38
+ $requiresString = " DSC/WMIGroup"
39
+
40
+ $z = [pscustomobject ]@ {
41
+ type = $fullResourceTypeName ;
42
+ version = $version_string ;
43
+ path = " " ;
44
+ directory = " " ;
45
+ implementedAs = " " ;
46
+ author = $author_string ;
47
+ properties = $propertyList ;
48
+ requires = $requiresString
49
+ }
50
+
51
+ $z | ConvertTo-Json - Compress
52
+ }
53
+ }
54
+ elseif ($Operation -eq ' Get' )
55
+ {
56
+ $inputobj_pscustomobj = $null
57
+ if ($stdinput )
58
+ {
59
+ $inputobj_pscustomobj = $stdinput | ConvertFrom-Json
60
+ }
61
+
62
+ $result = @ ()
63
+
64
+ if ($inputobj_pscustomobj.resources ) # we are processing a config batch
65
+ {
66
+ foreach ($r in $inputobj_pscustomobj.resources )
67
+ {
68
+ $type_fields = $r.type -split " /"
69
+ $wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
70
+ $wmi_classname = $type_fields [1 ]
71
+
72
+ # TODO: add filtering based on supplied properties of $r
73
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname
74
+
75
+ if ($wmi_instances )
76
+ {
77
+ $instance_result = @ {}
78
+ $wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
79
+ $wmi_instance.psobject.properties | % {
80
+ if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
81
+ {
82
+ $instance_result [$_.Name ] = $_.Value
83
+ }
84
+ }
85
+
86
+ $result += @ ($instance_result )
87
+ }
88
+ else
89
+ {
90
+ $errmsg = " Can not find type " + $r.type + " ; please ensure that Get-CimInstance returns this resource type"
91
+ Write-Error $errmsg
92
+ exit 1
93
+ }
94
+ }
95
+ }
96
+ else # we are processing an individual resource call
97
+ {
98
+ $type_fields = $inputobj_pscustomobj.type -split " /"
99
+ $wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
100
+ $wmi_classname = $type_fields [1 ]
101
+
102
+ # TODO: add filtering based on supplied properties of $inputobj_pscustomobj
103
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname
104
+
105
+ if ($wmi_instances )
106
+ {
107
+ $wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
108
+ $result = @ {}
109
+ $wmi_instance.psobject.properties | % {
110
+ if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
111
+ {
112
+ $result [$_.Name ] = $_.Value
113
+ }
114
+ }
115
+ }
116
+ else
117
+ {
118
+ $errmsg = " Can not find type " + $inputobj_pscustomobj.type + " ; please ensure that Get-CimInstance returns this resource type"
119
+ Write-Error $errmsg
120
+ exit 1
121
+ }
122
+ }
123
+
124
+ $result | ConvertTo-Json - Compress
125
+ }
126
+ else
127
+ {
128
+ Write-Error " ERROR: Unsupported operation requested from wmigroup.resource.ps1"
129
+ }
0 commit comments