Skip to content

Commit 3867299

Browse files
First almost working version
1 parent 9df5570 commit 3867299

6 files changed

Lines changed: 158 additions & 123 deletions

File tree

Modules/FIleContentDsc/DSCResources/MSFT_ReplaceText/MSFT_ReplaceText.psm1

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ param ()
55

66
Set-StrictMode -Version 'Latest'
77

8-
Import-Module `
9-
-Name (Join-Path `
10-
-Path (Split-Path -Path $PSScriptRoot -Parent) `
11-
-ChildPath 'CommonResourceHelper.psm1')
12-
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_ReplaceText'
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_ReplaceText' `
23+
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
1324

1425
<#
1526
.SYNOPSIS
@@ -38,26 +49,29 @@ function Get-TargetResource
3849
$Search
3950
)
4051

41-
Assert-ParametersValid
52+
Assert-ParametersValid @PSBoundParameters
4253

43-
$fileContent = Get-Content -Path $Path
54+
$fileContent = Get-Content -Path $Path -Raw
4455

45-
Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
56+
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
4657
$Path,$Search)
4758

4859
$text = ''
4960

50-
if ($fileContent -match $Search)
51-
{
52-
$text = $Matches.Values
61+
# Search the file content for any matches
62+
$results = [regex]::Matches($fileContent,$Search)
5363

54-
Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
55-
$Path,$Search,$text)
64+
if ($results.Count -eq 0) {
65+
# No matches found - already in state
66+
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
67+
$Path,$Search)
5668
}
5769
else
5870
{
59-
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
60-
$Path,$Search)
71+
$text = ($results.Value -join ',')
72+
73+
Write-Verbose -Message ($localizedData.StringMatchFoundMessage -f `
74+
$Path,$Search,$text)
6175
} # if
6276

6377
return @{
@@ -106,41 +120,39 @@ function Set-TargetResource
106120
[String]
107121
$Search,
108122

123+
[Parameter()]
109124
[ValidateSet('Text', 'Password')]
110125
[String]
111126
$Type = 'Text',
112127

128+
[Parameter()]
113129
[String]
114130
$Text,
115131

132+
[Parameter()]
116133
[System.Management.Automation.PSCredential]
117134
[System.Management.Automation.Credential()]
118135
$Password
119136
)
120137

121-
Assert-ParametersValid
138+
Assert-ParametersValid @PSBoundParameters
122139

123-
$fileContent = Get-Content -Path $Path
140+
$fileContent = Get-Content -Path $Path -Raw
124141

125-
switch ($Type)
142+
if ($Type -eq 'Password')
126143
{
127-
'Text'
128-
{
129-
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
130-
$Path,$Text)
144+
Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f `
145+
$Path,$Text)
131146

132-
$fileContent = $fileContent -Replace $Search,$Text
133-
break
134-
} # 'Text'
135-
'Password'
136-
{
137-
Write-Verbose -Message ($script:localizedData.StringReplacePasswordMessage -f `
138-
$Path)
147+
$Text = $Password.Password
148+
}
149+
else
150+
{
151+
Write-Verbose -Message ($localizedData.StringReplacePasswordMessage -f `
152+
$Path)
153+
} # if
139154

140-
$fileContent = $fileContent -Replace $Search,$Password.Password
141-
break
142-
} # 'Password'
143-
} # switch
155+
$fileContent = $fileContent -Replace $Search,$Text
144156

145157
Set-Content `
146158
-Path $Path `
@@ -185,62 +197,67 @@ function Test-TargetResource
185197
[String]
186198
$Search,
187199

200+
[Parameter()]
188201
[ValidateSet('Text', 'Password')]
189202
[String]
190203
$Type = 'Text',
191204

205+
[Parameter()]
192206
[String]
193207
$Text,
194208

209+
[Parameter()]
195210
[System.Management.Automation.PSCredential]
196211
[System.Management.Automation.Credential()]
197212
$Password
198213
)
199214

200-
Assert-ParametersValid
215+
Assert-ParametersValid @PSBoundParameters
201216

202-
$fileContent = Get-Content -Path $Path
217+
$fileContent = Get-Content -Path $Path -Raw
203218

204-
Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
219+
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
205220
$Path,$Search)
206221

207-
if (-not ($fileContent -match $Search)) {
208-
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
222+
# Search the file content for any matches
223+
$results = [regex]::Matches($fileContent,$Search)
224+
225+
if ($results.Count -eq 0) {
226+
# No matches found - already in state
227+
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
209228
$Path,$Search)
210229

211-
return $false
230+
return $true
231+
}
232+
233+
# Flag to signal whether settings are correct
234+
[Boolean] $desiredConfigurationMatch = $true
235+
236+
if ($Type -eq 'Password')
237+
{
238+
$Text = $Password.Password
212239
} # if
213240

214-
switch ($Type)
241+
foreach ($result in $results)
215242
{
216-
'Text'
217-
{
218-
if ($Matches.Values -eq $Text)
219-
{
220-
Write-Verbose -Message ($script:localizedData.StringAlreadyMatchesTextMessage -f `
221-
$Path,$Search,$Text)
222-
223-
return $false
224-
} # if
225-
break
226-
} # 'Text'
227-
'Password'
243+
if ($result.Value -ne $Text)
228244
{
229-
if ($Matches.Values -eq $Password.Password)
230-
{
231-
Write-Verbose -Message ($script:localizedData.StringAlreadyMatchesPasswordMessage -f `
232-
$Path,$Search)
233-
234-
return $false
235-
} # if
236-
break
237-
} # 'Password'
238-
} # switch
245+
$desiredConfigurationMatch = $false
246+
} # if
247+
} # foreach
239248

240-
Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
241-
$Path,$Search,$Matches.Values)
249+
if ($desiredConfigurationMatch)
250+
{
251+
Write-Verbose -Message ($localizedData.StringNoReplacementMessage -f `
252+
$Path,$Search)
253+
}
254+
else
255+
{
256+
Write-Verbose -Message ($localizedData.StringReplacementRequiredMessage -f `
257+
$Path,$Search)
258+
} # if
242259

243-
return $true
260+
return $desiredConfigurationMatch
244261
}
245262

246263
<#
@@ -281,13 +298,16 @@ function Assert-ParametersValid
281298
[String]
282299
$Search,
283300

301+
[Parameter()]
284302
[ValidateSet('Text', 'Password')]
285303
[String]
286304
$Type = 'Text',
287305

306+
[Parameter()]
288307
[String]
289308
$Text,
290309

310+
[Parameter()]
291311
[System.Management.Automation.PSCredential]
292312
[System.Management.Automation.Credential()]
293313
$Password
@@ -297,9 +317,9 @@ function Assert-ParametersValid
297317
if (-not (Test-Path -Path $Path))
298318
{
299319
New-InvalidArgumentException `
300-
-Message ($script:localizedData.FileNotFoundError -f $Path) `
320+
-Message ($localizedData.FileNotFoundError -f $Path) `
301321
-ArgumentName 'Path'
302322
} # if
303323
}
304324

305-
Export-ModuleMember -Function '*-TargetResource'
325+
Export-ModuleMember -Function *-TargetResource

Modules/FIleContentDsc/DSCResources/MSFT_ReplaceText/MSFT_ReplaceText.mof renamed to Modules/FIleContentDsc/DSCResources/MSFT_ReplaceText/MSFT_ReplaceText.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[ClassVersion("1.0.0.0"),FriendlyName("ReplaceText")]
1+
[ClassVersion("1.0.0.0"), FriendlyName("ReplaceText")]
22
class MSFT_ReplaceText : OMI_BaseResource
33
{
44
[Key, Description("The path to the text file to replace the string in.")] String Path;

Modules/FIleContentDsc/DSCResources/MSFT_ReplaceText/en-US/MSFT_ReplaceText.strings.psd1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
ConvertFrom-StringData @'
44
SearchForTextMessage = Searching using RegEx '{1}' in file '{0}'.
5-
StringNotFoundMessage = String not found using RegEx '{1}' in file '{0}'.
6-
StringAlreadyMatchesTextMessage = String found using RegEx '{1}' found in file '{0}', but already matches text '{2}'.
7-
StringAlreadyMatchesPasswordMessage = String found using RegEx '{1}' found in file '{0}', but already matches password.
8-
StringMatchFoundMessage = String '{2}' found using RegEx '{1}' found in file '{0}'.
5+
StringNotFoundMessage = String not found using RegEx '{1}' in file '{0}', change not required.
6+
StringMatchFoundMessage = String(s) '{2}' found using RegEx '{1}' in file '{0}'.
7+
StringReplacementRequiredMessage = String found using RegEx '{1}' in file '{0}', replacement required.
8+
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.
99
StringReplaceTextMessage = String replaced by '{1}' in file '{0}'.
1010
StringReplacePasswordMessage = String replaced by password in file '{0}'.
1111
FileNotFoundError = File '{0}' not found.

Modules/FIleContentDsc/FileContentDsc.psd1

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ModuleVersion = '1.0.0.0'
1010
# CompatiblePSEditions = @()
1111

1212
# ID used to uniquely identify this module
13-
GUID = '4ac4593d-3f4d-44f0-95eb-4ea01fa7b2a5'
13+
GUID = '6c9fe2f4-8af9-4bad-bd95-5909188c0f0a'
1414

1515
# Author of this module
1616
Author = 'Microsoft Corporation'
@@ -37,7 +37,7 @@ PowerShellVersion = '4.0'
3737
# DotNetFrameworkVersion = ''
3838

3939
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
40-
# CLRVersion = ''
40+
CLRVersion = '4.0'
4141

4242
# Processor architecture (None, X86, Amd64) required by this module
4343
# ProcessorArchitecture = ''
@@ -61,19 +61,19 @@ PowerShellVersion = '4.0'
6161
# NestedModules = @()
6262

6363
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
64-
FunctionsToExport = '*'
64+
FunctionsToExport = @()
6565

6666
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
67-
CmdletsToExport = '*'
67+
CmdletsToExport = @()
6868

6969
# Variables to export from this module
70-
VariablesToExport = '*'
70+
VariablesToExport = @()
7171

7272
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
73-
AliasesToExport = '*'
73+
AliasesToExport = @()
7474

7575
# DSC resources to export from this module
76-
# DscResourcesToExport = @()
76+
DscResourcesToExport = 'ReplaceText'
7777

7878
# List of all modules packaged with this module
7979
# ModuleList = @()
@@ -112,4 +112,3 @@ PrivateData = @{
112112
# DefaultCommandPrefix = ''
113113

114114
}
115-

0 commit comments

Comments
 (0)