1 #Requires -Version 5.0 -Modules CimCmdlets
2
ConvertFrom-CimInstance()3 Function ConvertFrom-CimInstance {
4 param(
5 [Parameter(Mandatory=$true)][CimInstance]$Instance
6 )
7 $hashtable = @{
8 _cim_instance = $Instance.CimSystemProperties.ClassName
9 }
10 foreach ($prop in $Instance.CimInstanceProperties) {
11 $hashtable."$($prop.Name)" = ConvertTo-OutputValue -Value $prop.Value
12 }
13 return $hashtable
14 }
15
ConvertTo-OutputValuenull16 Function ConvertTo-OutputValue {
17 param($Value)
18
19 if ($Value -is [DateTime[]]) {
20 $Value = $Value | ForEach-Object { $_.ToString("o") }
21 } elseif ($Value -is [DateTime]) {
22 $Value = $Value.ToString("o")
23 } elseif ($Value -is [Double]) {
24 $Value = $Value.ToString() # To avoid Python 2 double parsing issues on test validation
25 } elseif ($Value -is [Double[]]) {
26 $Value = $Value | ForEach-Object { $_.ToString() }
27 } elseif ($Value -is [PSCredential]) {
28 $password = $null
29 $password_ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($Value.Password)
30 try {
31 $password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($password_ptr)
32 } finally {
33 [System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($password_ptr)
34 }
35 $Value = @{
36 username = $Value.Username
37 password = $password
38 }
39 } elseif ($Value -is [CimInstance[]]) {
40 $value_list = [System.Collections.Generic.List`1[Hashtable]]@()
41 foreach ($cim_instance in $Value) {
42 $value_list.Add((ConvertFrom-CimInstance -Instance $cim_instance))
43 }
44 $Value = $value_list.ToArray()
45 } elseif ($Value -is [CimInstance]) {
46 $Value = ConvertFrom-CimInstance -Instance $Value
47 }
48
49 return ,$Value
50 }
51
Get-TargetResource()52 Function Get-TargetResource
53 {
54 [CmdletBinding()]
55 [OutputType([Hashtable])]
56 param(
57 [Parameter(Mandatory = $true)]
58 [ValidateSet("Present", "Absent")]
59 [String] $Ensure = "Present",
60
61 [Parameter(Mandatory = $true)]
62 [ValidateNotNullOrEmpty()]
63 [String] $Path
64 )
65 return @{
66 Ensure = $Ensure
67 Path = $Path
68 }
69 }
70
Set-TargetResourcenull71 Function Set-TargetResource
72 {
73 [CmdletBinding()]
74 param
75 (
76 [Parameter(Mandatory = $true)]
77 [ValidateSet("Present", "Absent")]
78 [String] $Ensure = "Present",
79
80 [Parameter(Mandatory = $true)]
81 [ValidateNotNullOrEmpty()]
82 [String] $Path,
83
84 [String] $DefaultParam = "Default",
85 [String] $StringParam,
86 [String[]] $StringArrayParam,
87 [SByte] $Int8Param,
88 [SByte[]] $Int8ArrayParam,
89 [Byte] $UInt8Param,
90 [Byte[]] $UInt8ArrayParam,
91 [Int16] $Int16Param,
92 [Int16[]] $Int16ArrayParam,
93 [UInt16] $UInt16Param,
94 [UInt16[]] $UInt16ArrayParam,
95 [Int32] $Int32Param,
96 [Int32[]] $Int32ArrayParam,
97 [UInt32] $UInt32Param,
98 [UInt32[]] $UInt32ArrayParam,
99 [Int64] $Int64Param,
100 [Int64[]] $Int64ArrayParam,
101 [UInt64] $UInt64Param,
102 [UInt64[]] $UInt64ArrayParam,
103 [Bool] $BooleanParam,
104 [Bool[]] $BooleanArrayParam,
105 [Char] $CharParam,
106 [Char[]] $CharArrayParam,
107 [Single] $SingleParam,
108 [Single[]] $SingleArrayParam,
109 [Double] $DoubleParam,
110 [Double[]] $DoubleArrayParam,
111 [DateTime] $DateTimeParam,
112 [DateTime[]] $DateTimeArrayParam,
113 [PSCredential] $PSCredentialParam,
114 [CimInstance[]] $HashtableParam,
115 [CimInstance] $CimInstanceParam,
116 [CimInstance[]] $CimInstanceArrayParam,
117 [CimInstance] $NestedCimInstanceParam,
118 [CimInstance[]] $NestedCimInstanceArrayParam
119 )
120
121 $info = @{
122 Version = "1.0.1"
123 Ensure = @{
124 Type = $Ensure.GetType().FullName
125 Value = $Ensure
126 }
127 Path = @{
128 Type = $Path.GetType().FullName
129 Value = $Path
130 }
131 DefaultParam = @{
132 Type = $DefaultParam.GetType().FullName
133 Value = $DefaultParam
134 }
135 }
136
137 foreach ($kvp in $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator()) {
138 $info."$($kvp.Key)" = @{
139 Type = $kvp.Value.GetType().FullName
140 Value = (ConvertTo-OutputValue -Value $kvp.Value)
141 }
142 }
143
144 if (Test-Path -Path $Path) {
145 Remove-Item -Path $Path -Force > $null
146 }
147 New-Item -Path $Path -ItemType File > $null
148 Set-Content -Path $Path -Value (ConvertTo-Json -InputObject $info -Depth 10) > $null
149 Write-Verbose -Message "set verbose"
150 Write-Warning -Message "set warning"
151 }
152
Test-TargetResourcenull153 Function Test-TargetResource
154 {
155 [CmdletBinding()]
156 [OutputType([Boolean])]
157 param
158 (
159 [Parameter(Mandatory = $true)]
160 [ValidateSet("Present", "Absent")]
161 [String] $Ensure = "Present",
162
163 [Parameter(Mandatory = $true)]
164 [ValidateNotNullOrEmpty()]
165 [String] $Path,
166
167 [String] $DefaultParam = "Default",
168 [String] $StringParam,
169 [String[]] $StringArrayParam,
170 [SByte] $Int8Param,
171 [SByte[]] $Int8ArrayParam,
172 [Byte] $UInt8Param,
173 [Byte[]] $UInt8ArrayParam,
174 [Int16] $Int16Param,
175 [Int16[]] $Int16ArrayParam,
176 [UInt16] $UInt16Param,
177 [UInt16[]] $UInt16ArrayParam,
178 [Int32] $Int32Param,
179 [Int32[]] $Int32ArrayParam,
180 [UInt32] $UInt32Param,
181 [UInt32[]] $UInt32ArrayParam,
182 [Int64] $Int64Param,
183 [Int64[]] $Int64ArrayParam,
184 [UInt64] $UInt64Param,
185 [UInt64[]] $UInt64ArrayParam,
186 [Bool] $BooleanParam,
187 [Bool[]] $BooleanArrayParam,
188 [Char] $CharParam,
189 [Char[]] $CharArrayParam,
190 [Single] $SingleParam,
191 [Single[]] $SingleArrayParam,
192 [Double] $DoubleParam,
193 [Double[]] $DoubleArrayParam,
194 [DateTime] $DateTimeParam,
195 [DateTime[]] $DateTimeArrayParam,
196 [PSCredential] $PSCredentialParam,
197 [CimInstance[]] $HashtableParam,
198 [CimInstance] $CimInstanceParam,
199 [CimInstance[]] $CimInstanceArrayParam,
200 [CimInstance] $NestedCimInstanceParam,
201 [CimInstance[]] $NestedCimInstanceArrayParam
202 )
203 Write-Verbose -Message "test verbose"
204 Write-Warning -Message "test warning"
205 $exists = Test-Path -LiteralPath $Path -PathType Leaf
206 if ($Ensure -eq "Present") {
207 $exists
208 } else {
209 -not $exists
210 }
211 }
212
213 Export-ModuleMember -Function *-TargetResource
214
215