Skip to content

Commit bb32904

Browse files
First working version of ReplaceText
1 parent 7ceb60d commit bb32904

16 files changed

Lines changed: 331 additions & 161 deletions

.vscode/RunAllTests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[string] $repoRoot = Split-Path -Path (Split-Path -Path $Script:MyInvocation.MyCommand.Path)
22
if ( (-not (Test-Path -Path (Join-Path -Path $repoRoot -ChildPath 'DSCResource.Tests'))) -or `
3-
(-not (Test-Path -Path (Join-Path -Path $repoRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
3+
(-not (Test-Path -Path (Join-Path -Path $repoRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
44
{
5-
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $repoRoot -ChildPath '\DSCResource.Tests\'))
5+
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $repoRoot -ChildPath '\DSCResource.Tests\'))
66
}
77

88
Import-Module (Join-Path $PSScriptRoot "..\Tests\TestHarness.psm1" -Resolve)
99
$dscTestsPath = Join-Path -Path $PSScriptRoot `
10-
-ChildPath "..\Modules\FileContentDsc\DscResource.Tests\Meta.Tests.ps1"
10+
-ChildPath "..\Modules\FileContentDsc\DscResource.Tests\Meta.Tests.ps1"
1111
Invoke-TestHarness -DscTestsPath $dscTestsPath

.vscode/settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"powershell.codeFormatting.openBraceOnSameLine": false,
4+
"powershell.codeFormatting.newLineAfterOpenBrace": false,
5+
"powershell.codeFormatting.newLineAfterCloseBrace": true,
6+
"powershell.codeFormatting.whitespaceBeforeOpenBrace": true,
7+
"powershell.codeFormatting.whitespaceBeforeOpenParen": true,
8+
"powershell.codeFormatting.whitespaceAroundOperator": true,
9+
"powershell.codeFormatting.whitespaceAfterSeparator": true,
10+
"powershell.codeFormatting.ignoreOneLineBlock": false,
11+
"powershell.codeFormatting.alignPropertyValuePairs": true
12+
}

Modules/FIleContentDsc/DSCResources/MSFT_ReplaceText/MSFT_ReplaceText.psm1

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ $modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot
99

1010
# Import the Storage Common Modules
1111
Import-Module -Name (Join-Path -Path $modulePath `
12-
-ChildPath (Join-Path -Path 'FileContentDsc.Common' `
13-
-ChildPath 'FileContentDsc.Common.psm1'))
12+
-ChildPath (Join-Path -Path 'FileContentDsc.Common' `
13+
-ChildPath 'FileContentDsc.Common.psm1'))
1414

1515
# Import the Storage Resource Helper Module
1616
Import-Module -Name (Join-Path -Path $modulePath `
17-
-ChildPath (Join-Path -Path 'FileContentDsc.ResourceHelper' `
18-
-ChildPath 'FileContentDsc.ResourceHelper.psm1'))
17+
-ChildPath (Join-Path -Path 'FileContentDsc.ResourceHelper' `
18+
-ChildPath 'FileContentDsc.ResourceHelper.psm1'))
1919

2020
# Import Localization Strings
2121
$localizedData = Get-LocalizedData `
@@ -54,31 +54,32 @@ function Get-TargetResource
5454
$fileContent = Get-Content -Path $Path -Raw
5555

5656
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
57-
$Path,$Search)
57+
$Path, $Search)
5858

5959
$text = ''
6060

6161
# Search the file content for any matches
62-
$results = [regex]::Matches($fileContent,$Search)
63-
64-
if ($results.Count -eq 0) {
62+
$results = [regex]::Matches($fileContent, $Search)
63+
64+
if ($results.Count -eq 0)
65+
{
6566
# No matches found - already in state
6667
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
67-
$Path,$Search)
68+
$Path, $Search)
6869
}
6970
else
7071
{
7172
$text = ($results.Value -join ',')
7273

7374
Write-Verbose -Message ($localizedData.StringMatchFoundMessage -f `
74-
$Path,$Search,$text)
75+
$Path, $Search, $text)
7576
} # if
7677

7778
return @{
78-
Path = $Path
79-
Search = $Search
80-
Type = 'Text'
81-
Text = $text
79+
Path = $Path
80+
Search = $Search
81+
Type = 'Text'
82+
Text = $text
8283
}
8384
}
8485

@@ -93,15 +94,15 @@ function Get-TargetResource
9394
The RegEx string to use to search the text file.
9495
9596
.PARAMETER Type
96-
Specifies the value type to use as the replacement string.
97+
Specifies the value type to use as the replacement string. Defaults to 'Text'.
9798
9899
.PARAMETER Text
99100
The text to replace the text identifed by the RegEx.
100101
Only used when Type is set to 'Text'.
101102
102-
.PARAMETER Password
103-
The password to replace the text identified by the RegEx.
104-
Only used when Type is set to 'Password'.
103+
.PARAMETER Secret
104+
The secret text to replace the text identified by the RegEx.
105+
Only used when Type is set to 'Secret'.
105106
#>
106107
function Set-TargetResource
107108
{
@@ -121,7 +122,7 @@ function Set-TargetResource
121122
$Search,
122123

123124
[Parameter()]
124-
[ValidateSet('Text', 'Password')]
125+
[ValidateSet('Text', 'Secret')]
125126
[String]
126127
$Type = 'Text',
127128

@@ -132,31 +133,32 @@ function Set-TargetResource
132133
[Parameter()]
133134
[System.Management.Automation.PSCredential]
134135
[System.Management.Automation.Credential()]
135-
$Password
136+
$Secret
136137
)
137138

138139
Assert-ParametersValid @PSBoundParameters
139140

140141
$fileContent = Get-Content -Path $Path -Raw
141142

142-
if ($Type -eq 'Password')
143+
if ($Type -eq 'Secret')
143144
{
144-
Write-Verbose -Message ($localizedData.StringReplacePasswordMessage -f `
145-
$Path)
145+
Write-Verbose -Message ($localizedData.StringReplaceSecretMessage -f `
146+
$Path)
146147

147-
$Text = $Password.Password
148+
$Text = $Secret.GetNetworkCredential().Password
148149
}
149150
else
150151
{
151152
Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f `
152-
$Path,$Text)
153+
$Path, $Text)
153154
} # if
154155

155-
$fileContent = $fileContent -Replace $Search,$Text
156+
$fileContent = $fileContent -Replace $Search, $Text
156157

157158
Set-Content `
158159
-Path $Path `
159160
-Value $fileContent `
161+
-NoNewline `
160162
-Force
161163
}
162164

@@ -171,15 +173,15 @@ function Set-TargetResource
171173
The RegEx string to use to search the text file.
172174
173175
.PARAMETER Type
174-
Specifies the value type to use as the replacement string.
176+
Specifies the value type to use as the replacement string. Defaults to 'Text'.
175177
176178
.PARAMETER Text
177179
The text to replace the text identifed by the RegEx.
178180
Only used when Type is set to 'Text'.
179181
180-
.PARAMETER Password
181-
The password to replace the text identified by the RegEx.
182-
Only used when Type is set to 'Password'.
182+
.PARAMETER Secret
183+
The secret text to replace the text identified by the RegEx.
184+
Only used when Type is set to 'Secret'.
183185
#>
184186
function Test-TargetResource
185187
{
@@ -198,7 +200,7 @@ function Test-TargetResource
198200
$Search,
199201

200202
[Parameter()]
201-
[ValidateSet('Text', 'Password')]
203+
[ValidateSet('Text', 'Secret')]
202204
[String]
203205
$Type = 'Text',
204206

@@ -209,33 +211,34 @@ function Test-TargetResource
209211
[Parameter()]
210212
[System.Management.Automation.PSCredential]
211213
[System.Management.Automation.Credential()]
212-
$Password
214+
$Secret
213215
)
214216

215217
Assert-ParametersValid @PSBoundParameters
216218

217219
$fileContent = Get-Content -Path $Path -Raw
218220

219221
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
220-
$Path,$Search)
222+
$Path, $Search)
221223

222224
# Search the file content for any matches
223-
$results = [regex]::Matches($fileContent,$Search)
225+
$results = [regex]::Matches($fileContent, $Search)
224226

225-
if ($results.Count -eq 0) {
227+
if ($results.Count -eq 0)
228+
{
226229
# No matches found - already in state
227230
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
228-
$Path,$Search)
231+
$Path, $Search)
229232

230233
return $true
231234
}
232235

233236
# Flag to signal whether settings are correct
234237
[Boolean] $desiredConfigurationMatch = $true
235238

236-
if ($Type -eq 'Password')
239+
if ($Type -eq 'Secret')
237240
{
238-
$Text = $Password.Password
241+
$Text = $Secret.Password
239242
} # if
240243

241244
foreach ($result in $results)
@@ -249,12 +252,12 @@ function Test-TargetResource
249252
if ($desiredConfigurationMatch)
250253
{
251254
Write-Verbose -Message ($localizedData.StringNoReplacementMessage -f `
252-
$Path,$Search)
255+
$Path, $Search)
253256
}
254257
else
255258
{
256259
Write-Verbose -Message ($localizedData.StringReplacementRequiredMessage -f `
257-
$Path,$Search)
260+
$Path, $Search)
258261
} # if
259262

260263
return $desiredConfigurationMatch
@@ -272,15 +275,15 @@ function Test-TargetResource
272275
The RegEx string to use to search the text file.
273276
274277
.PARAMETER Type
275-
Specifies the value type to use as the replacement string.
278+
Specifies the value type to use as the replacement string. Defaults to 'Text'.
276279
277280
.PARAMETER Text
278281
The text to replace the text identifed by the RegEx.
279282
Only used when Type is set to 'Text'.
280283
281-
.PARAMETER Password
282-
The password to replace the text identified by the RegEx.
283-
Only used when Type is set to 'Password'.
284+
.PARAMETER Secret
285+
The secret text to replace the text identified by the RegEx.
286+
Only used when Type is set to 'Secret'.
284287
#>
285288
function Assert-ParametersValid
286289
{
@@ -299,7 +302,7 @@ function Assert-ParametersValid
299302
$Search,
300303

301304
[Parameter()]
302-
[ValidateSet('Text', 'Password')]
305+
[ValidateSet('Text', 'Secret')]
303306
[String]
304307
$Type = 'Text',
305308

@@ -310,7 +313,7 @@ function Assert-ParametersValid
310313
[Parameter()]
311314
[System.Management.Automation.PSCredential]
312315
[System.Management.Automation.Credential()]
313-
$Password
316+
$Secret
314317
)
315318

316319
# Does the file in path exist?

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class MSFT_ReplaceText : OMI_BaseResource
33
{
44
[Key, Description("The path to the text file to replace the string in.")] String Path;
55
[Key, Description("The RegEx string to use to search the text file.")] String Search;
6-
[Write, Description("Specifies the value type to use as the replacement string."),ValueMap{"Text", "Password"},Values{"Text", "Password"}] string Type;
7-
[Write, Description("The text to replace the text identifed by the RegEx. Only used when Type is set to 'Text'.")] String Text;
8-
[write, Description("The password to replace the text identified by the RegEx. Only used when Type is set to 'Password'."),EmbeddedInstance("MSFT_Credential")] string Password;
6+
[Write, Description("Specifies the value type to use as the replacement string. Defaults to 'Text'."),ValueMap{"Text", "Secret"},Values{"Text", "Secret"}] String Type;
7+
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
8+
[write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
99
};
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Description
22

3-
The resource is used to replaces strings matching a regular expression in a file.
3+
The resource is used to replaces strings matching a regular expression in a text file.
4+
5+
It can be used to replace strings matched with a regular expression with either a text
6+
string or a secret which is provided in the password of a credential object.
Binary file not shown.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ ConvertFrom-StringData @'
77
StringReplacementRequiredMessage = String found using RegEx '{1}' in file '{0}', replacement required.
88
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.
99
StringReplaceTextMessage = String replaced by '{1}' in file '{0}'.
10-
StringReplacePasswordMessage = String replaced by password in file '{0}'.
10+
StringReplaceSecretMessage = String replaced by secret text in file '{0}'.
1111
FileNotFoundError = File '{0}' not found.
1212
'@
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<#
2+
.EXAMPLE
3+
Set all occrurances of the string '%appname%' to be 'Awesome App'
4+
in the file 'c:\inetpub\wwwroot\default.htm'.
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+
ReplaceText SetText
20+
{
21+
Path = 'c:\inetpub\wwwroot\default.htm'
22+
Search = '%appname%'
23+
Type = 'Text'
24+
Text = 'Awesome App'
25+
}
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#
2+
.EXAMPLE
3+
Set all occrurances of a string matching the regular expression
4+
"<img src=['`"][a-zA-Z0-9.]*['`"]>" with the text '<img src="imgs/placeholder.jpg">'
5+
in the file 'c:\inetpub\wwwroot\default.htm'
6+
#>
7+
Configuration Example
8+
{
9+
param
10+
(
11+
[Parameter()]
12+
[System.String[]]
13+
$NodeName = 'localhost'
14+
)
15+
16+
Import-DSCResource -ModuleName FileContentDsc
17+
18+
Node $NodeName
19+
{
20+
ReplaceText SetTextWithRegex
21+
{
22+
Path = 'c:\inetpub\wwwroot\default.htm'
23+
Search = "<img src=['`"][a-zA-Z0-9.]*['`"]>"
24+
Type = 'Text'
25+
Text = '<img src="imgs/placeholder.jpg">'
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)