Skip to content

Commit 6ed2339

Browse files
Added first cut of IniSettingsFile
1 parent 3ddf029 commit 6ed2339

13 files changed

Lines changed: 737 additions & 1 deletion

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
- Created new resource for replacing text in text files.
77
- MSFT_KeyValuePairFile:
88
- Created new resource for setting key value pairs in text files.
9+
- MSFT_IniSettingsFile:
10+
- Created new resource for setting Windows INI file settings.
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
# Suppressed as per PSSA Rule Severity guidelines for unit/integration tests:
2+
# https://github.com/PowerShell/DscResources/blob/master/PSSARuleSeverities.md
3+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
4+
param ()
5+
6+
Set-StrictMode -Version 'Latest'
7+
8+
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'
9+
10+
# Import the Storage Common Modules
11+
Import-Module -Name (Join-Path -Path $modulePath `
12+
-ChildPath (Join-Path -Path 'FileContentDsc.Common' `
13+
-ChildPath 'FileContentDsc.Common.psm1'))
14+
15+
# Import the Storage Resource Helper Module
16+
Import-Module -Name (Join-Path -Path $modulePath `
17+
-ChildPath (Join-Path -Path 'FileContentDsc.ResourceHelper' `
18+
-ChildPath 'FileContentDsc.ResourceHelper.psm1'))
19+
20+
# Import Localization Strings
21+
$localizedData = Get-LocalizedData `
22+
-ResourceName 'MSFT_IniSettingsFile' `
23+
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
24+
25+
<#
26+
.SYNOPSIS
27+
Retrieves the current state of the INI settings file entry.
28+
29+
.PARAMETER Path
30+
The path to the INI settings file to set the entry in.
31+
32+
.PARAMETER Section
33+
The section to add or set the entry in.
34+
35+
.PARAMETER Key
36+
The name of the key to add or set in the section.
37+
#>
38+
function Get-TargetResource
39+
{
40+
[OutputType([Hashtable])]
41+
[CmdletBinding()]
42+
param
43+
(
44+
[Parameter(Mandatory = $true)]
45+
[ValidateNotNullOrEmpty()]
46+
[String]
47+
$Path,
48+
49+
[Parameter(Mandatory = $true)]
50+
[ValidateNotNullOrEmpty()]
51+
[String]
52+
$Section,
53+
54+
[Parameter(Mandatory = $true)]
55+
[ValidateNotNullOrEmpty()]
56+
[String]
57+
$Key
58+
)
59+
60+
Assert-ParametersValid @PSBoundParameters
61+
62+
Write-Verbose -Message ($localizedData.GetIniSettingMessage -f `
63+
$Path, $Section, $Key)
64+
65+
$text = Get-IniSettingFileValue @PSBoundParameters
66+
67+
return @{
68+
Path = $Path
69+
Section = $Section
70+
Key = $Key
71+
Type = 'Text'
72+
Text = $text
73+
}
74+
}
75+
76+
<#
77+
.SYNOPSIS
78+
Sets the value of an entry in an INI settings file.
79+
80+
.PARAMETER Path
81+
The path to the INI settings file to set the entry in.
82+
83+
.PARAMETER Section
84+
The section to add or set the entry in.
85+
86+
.PARAMETER Key
87+
The name of the key to add or set in the section.
88+
89+
.PARAMETER Type
90+
Specifies the value type that contains the value to set the entry to. Defaults to 'Text'.
91+
92+
.PARAMETER Text
93+
The text to set the entry value to.
94+
Only used when Type is set to 'Text'.
95+
96+
.PARAMETER Secret
97+
The secret text to set the entry value to.
98+
Only used when Type is set to 'Secret'.
99+
#>
100+
function Set-TargetResource
101+
{
102+
# Should process is called in a helper functions but not directly in Set-TargetResource
103+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
104+
[CmdletBinding(SupportsShouldProcess = $true)]
105+
param
106+
(
107+
[Parameter(Mandatory = $true)]
108+
[ValidateNotNullOrEmpty()]
109+
[String]
110+
$Path,
111+
112+
[Parameter(Mandatory = $true)]
113+
[ValidateNotNullOrEmpty()]
114+
[String]
115+
$Section,
116+
117+
[Parameter(Mandatory = $true)]
118+
[ValidateNotNullOrEmpty()]
119+
[String]
120+
$Key,
121+
122+
[Parameter()]
123+
[ValidateSet('Text', 'Secret')]
124+
[String]
125+
$Type = 'Text',
126+
127+
[Parameter()]
128+
[String]
129+
$Text,
130+
131+
[Parameter()]
132+
[System.Management.Automation.PSCredential]
133+
[System.Management.Automation.Credential()]
134+
$Secret
135+
)
136+
137+
Assert-ParametersValid @PSBoundParameters
138+
139+
if ($Type -eq 'Secret')
140+
{
141+
Write-Verbose -Message ($localizedData.SetIniSettingSecretMessage -f `
142+
$Path, $Section, $Key)
143+
144+
$Text = $Secret.GetNetworkCredential().Password
145+
$null = $PSBoundParameters.Remove('Secret')
146+
}
147+
else
148+
{
149+
Write-Verbose -Message ($localizedData.SetIniSettingTextMessage -f `
150+
$Path, $Section, $Key, $Text)
151+
} # if
152+
153+
# Prepare the for the PSBoundParameters to be splatted
154+
$null = $PSBoundParameters.Remove('Type')
155+
$null = $PSBoundParameters.Add('Value',$Text)
156+
$null = $PSBoundParameters.Remove('Text')
157+
158+
Set-IniSettingFileValue @PSBoundParameters
159+
}
160+
161+
<#
162+
.SYNOPSIS
163+
Tests the value of an entry in an INI settings file.
164+
165+
.PARAMETER Path
166+
The path to the INI settings file to set the entry in.
167+
168+
.PARAMETER Section
169+
The section to add or set the entry in.
170+
171+
.PARAMETER Key
172+
The name of the key to add or set in the section.
173+
174+
.PARAMETER Type
175+
Specifies the value type that contains the value to set the entry to. Defaults to 'Text'.
176+
177+
.PARAMETER Text
178+
The text to set the entry value to.
179+
Only used when Type is set to 'Text'.
180+
181+
.PARAMETER Secret
182+
The secret text to set the entry value to.
183+
Only used when Type is set to 'Secret'.
184+
#>
185+
function Test-TargetResource
186+
{
187+
[OutputType([Boolean])]
188+
[CmdletBinding()]
189+
param
190+
(
191+
[Parameter(Mandatory = $true)]
192+
[ValidateNotNullOrEmpty()]
193+
[String]
194+
$Path,
195+
196+
[Parameter(Mandatory = $true)]
197+
[ValidateNotNullOrEmpty()]
198+
[String]
199+
$Section,
200+
201+
[Parameter(Mandatory = $true)]
202+
[ValidateNotNullOrEmpty()]
203+
[String]
204+
$Key,
205+
206+
[Parameter()]
207+
[ValidateSet('Text', 'Secret')]
208+
[String]
209+
$Type = 'Text',
210+
211+
[Parameter()]
212+
[String]
213+
$Text,
214+
215+
[Parameter()]
216+
[System.Management.Automation.PSCredential]
217+
[System.Management.Automation.Credential()]
218+
$Secret
219+
)
220+
221+
Assert-ParametersValid @PSBoundParameters
222+
223+
if ($Type -eq 'Secret')
224+
{
225+
$Text = $Secret.GetNetworkCredential().Password
226+
} # if
227+
228+
# Prepare the PSBoundParameters for splat
229+
$null = $PSBoundParameters.Remove('Type')
230+
$null = $PSBoundParameters.Remove('Text')
231+
$null = $PSBoundParameters.Remove('Secret')
232+
233+
if ((Get-IniSettingFileValue @PSBoundParameters) -eq $Text)
234+
{
235+
Write-Verbose -Message ($localizedData.IniSettingMatchesMessage -f `
236+
$Path, $Section, $Key)
237+
238+
return $true
239+
}
240+
else
241+
{
242+
Write-Verbose -Message ($localizedData.IniSettingMismatchMessage -f `
243+
$Path, $Section, $Key)
244+
245+
return $false
246+
} # if
247+
}
248+
249+
<#
250+
.SYNOPSIS
251+
Validates the parameters that have been passed are valid.
252+
If they are not valid then an exception will be thrown.
253+
254+
.PARAMETER Path
255+
The path to the INI settings file to set the entry in.
256+
257+
.PARAMETER Section
258+
The section to add or set the entry in.
259+
260+
.PARAMETER Key
261+
The name of the key to add or set in the section.
262+
263+
.PARAMETER Type
264+
Specifies the value type that contains the value to set the entry to. Defaults to 'Text'.
265+
266+
.PARAMETER Text
267+
The text to set the entry value to.
268+
Only used when Type is set to 'Text'.
269+
270+
.PARAMETER Secret
271+
The secret text to set the entry value to.
272+
Only used when Type is set to 'Secret'.
273+
#>
274+
function Assert-ParametersValid
275+
{
276+
[CmdletBinding()]
277+
param
278+
(
279+
[Parameter(Mandatory = $true)]
280+
[ValidateNotNullOrEmpty()]
281+
[String]
282+
$Path,
283+
284+
[Parameter(Mandatory = $true)]
285+
[ValidateNotNullOrEmpty()]
286+
[String]
287+
$Section,
288+
289+
[Parameter(Mandatory = $true)]
290+
[ValidateNotNullOrEmpty()]
291+
[String]
292+
$Key,
293+
294+
[Parameter()]
295+
[ValidateSet('Text', 'Secret')]
296+
[String]
297+
$Type = 'Text',
298+
299+
[Parameter()]
300+
[String]
301+
$Text,
302+
303+
[Parameter()]
304+
[System.Management.Automation.PSCredential]
305+
[System.Management.Automation.Credential()]
306+
$Secret
307+
)
308+
309+
# Does the file in path exist?
310+
if (-not (Test-Path -Path $Path))
311+
{
312+
New-InvalidArgumentException `
313+
-Message ($localizedData.FileNotFoundError -f $Path) `
314+
-ArgumentName 'Path'
315+
} # if
316+
}
317+
318+
Export-ModuleMember -Function *-TargetResource
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[ClassVersion("1.0.0.0"), FriendlyName("IniSettingsFile")]
2+
class MSFT_IniSettingsFile : OMI_BaseResource
3+
{
4+
[Key, Description("The path to the INI settings file to set the entry in.")] String Path;
5+
[Key, Description("The section to add or set the entry in.")] String Section;
6+
[Key, Description("The name of the key to add or set in the section.")] String Key;
7+
[Write, Description("Specifies the value type that contains the value to set the entry to. Defaults to 'Text'."),ValueMap{"Text", "Secret"},Values{"Text", "Secret"}] String Type;
8+
[Write, Description("The text to set the entry value to. Only used when Type is set to 'Text'.")] String Text;
9+
[write, Description("The secret text to set the entry value to. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
10+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Description
2+
3+
The resource is used to add, set or clear entries in Windows INI
4+
settings files.
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Localized resources for MSFT_ReplaceText
2+
3+
ConvertFrom-StringData @'
4+
GetIniSettingMessage = Reading the entry '{1}' key '{2}' from INI settings file '{0}'.
5+
SetIniSettingTextMessage = Setting the entry '{1}' key '{2}' to '{3}' in INI settings file '{0}'.
6+
SetIniSettingSecretMessage = Setting the entry '{1}' key '{2}' to secret text in INI settings file '{0}'.
7+
IniSettingMatchesMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is in the correct state. Change not required.
8+
IniSettingMismatchMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is not in the correct state. Change required.
9+
FileNotFoundError = File '{0}' not found.
10+
'@
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<#
2+
.EXAMPLE
3+
Set the `Level` entry in the [Logging] section to `Information`
4+
in the file `c:\myapp\myapp.ini`.
5+
#>
6+
Configuration Example
7+
{
8+
param
9+
(
10+
[Parameter()]
11+
[System.String[]]
12+
$NodeName = 'localhost'
13+
)
14+
15+
Import-DSCResource -ModuleName FileContentDsc
16+
17+
Node $NodeName
18+
{
19+
IniSettingsFile SetLogging
20+
{
21+
Path = 'c:\myapp\myapp.ini'
22+
Section = 'Logging'
23+
Key = 'Level'
24+
Text = 'Information'
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)