Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ node_modules
node_modules/*
markdownissues.txt
TestResults.xml
launch.json
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
to root folder of repository and remove test harness - Fixes [Issue #11](https://github.com/PlagueHO/FileContentDsc/issues/11).
- Converted Examples to support format for publishing to PowerShell
Gallery.
- Refactor Test-TargetResource to return $false in all DSC resource -Fixes [Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13)

## 1.0.0.0

Expand Down
18 changes: 15 additions & 3 deletions DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ function Set-TargetResource

Assert-ParametersValid @PSBoundParameters

if (-not (Test-Path -Path $Path))
{
Out-File -FilePath $Path -Force
}

if ($Type -eq 'Secret')
{
Write-Verbose -Message ($localizedData.SetIniSettingSecretMessage -f `
Expand Down Expand Up @@ -220,6 +225,12 @@ function Test-TargetResource

Assert-ParametersValid @PSBoundParameters

# Check if file being managed exists. If not return $False.
if (-not (Test-Path -Path $Path))
{
return $false
}

if ($Type -eq 'Secret')
{
$Text = $Secret.GetNetworkCredential().Password
Expand Down Expand Up @@ -306,11 +317,12 @@ function Assert-ParametersValid
$Secret
)

# Does the file in path exist?
if (-not (Test-Path -Path $Path))
# Does the file's parent path exist?
$parentPath = Split-Path -Path $Path -Parent
if (-not (Test-Path -Path $parentPath))
{
New-InvalidArgumentException `
-Message ($localizedData.FileNotFoundError -f $Path) `
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
-ArgumentName 'Path'
} # if
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ ConvertFrom-StringData @'
SetIniSettingSecretMessage = Setting the entry '{1}' key '{2}' to secret text in INI settings file '{0}'.
IniSettingMatchesMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is in the correct state. Change not required.
IniSettingMismatchMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is not in the correct state. Change required.
FileNotFoundError = File '{0}' not found.
FileParentNotFoundError = File parent path '{0}' not found.
'@
100 changes: 57 additions & 43 deletions DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function Set-TargetResource

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue

Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
$Path, $Name)
Expand All @@ -187,61 +187,68 @@ function Set-TargetResource
$Text = $Secret.GetNetworkCredential().Password
} # if

# Determine the EOL characters used in the file
$eolChars = Get-TextEolCharacter -Text $fileContent

# Setup the Regex Options that will be used
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
if ($IgnoreNameCase)
if ($null -ne $fileContent)
{
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
}
# Determine the EOL characters used in the file
$eolChars = Get-TextEolCharacter -Text $fileContent

# Search the key that matches the requested key
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
# Setup the Regex Options that will be used
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
if ($IgnoreNameCase)
{
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
}

if ($Ensure -eq 'Present')
{
# The key value pair should exist
$keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars
# Search the key that matches the requested key
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)

if ($results.Count -eq 0)
if ($Ensure -eq 'Present')
{
# The key value pair was not found so add it to the end of the file
if (-not $fileContent.EndsWith($eolChars))
# The key value pair should exist
$keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars

if ($results.Count -eq 0)
{
$fileContent += $eolChars
} # if
# The key value pair was not found so add it to the end of the file
if (-not $fileContent.EndsWith($eolChars))
{
$fileContent += $eolChars
} # if

$fileContent += $keyValuePair
$fileContent += $keyValuePair

Write-Verbose -Message ($localizedData.KeyAddMessage -f `
$Path, $Name)
Write-Verbose -Message ($localizedData.KeyAddMessage -f `
$Path, $Name)
}
else
{
# The key value pair was found so update it
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions)

Write-Verbose -Message ($localizedData.KeyUpdateMessage -f `
$Path, $Name)
} # if
}
else
{
# The key value pair was found so update it
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions)
if ($results.Count -eq 0)
{
# The Key does not exists and should not so don't do anything
return
}
else
{
# The Key exists in the file but should not so remove it
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)

Write-Verbose -Message ($localizedData.KeyUpdateMessage -f `
$Path, $Name)
Write-Verbose -Message ($localizedData.KeyRemoveMessage -f `
$Path, $Name)
}
} # if
}
else
{
if ($results.Count -eq 0)
{
# The Key does not exists and should not so don't do anything
return
}
else
{
# The Key exists in the file but should not so remove it
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)

Write-Verbose -Message ($localizedData.KeyRemoveMessage -f `
$Path, $Name)
}
$fileContent = '{0}={1}' -f $Name, $Text
} # if

Set-Content `
Expand Down Expand Up @@ -331,6 +338,12 @@ function Test-TargetResource
# Flag to signal whether settings are correct
[Boolean] $desiredConfigurationMatch = $true

# Check if file being managed exists. If not return $False.
if (-not (Test-Path -Path $Path))
{
return $false
}

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

Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
Expand Down Expand Up @@ -484,11 +497,12 @@ function Assert-ParametersValid
$IgnoreValueCase = $false
)

# Does the file in path exist?
if (-not (Test-Path -Path $Path))
# Does the file's parent path exist?
$parentPath = Split-Path -Path $Path -Parent
if (-not (Test-Path -Path $parentPath))
{
New-InvalidArgumentException `
-Message ($localizedData.FileNotFoundError -f $Path) `
-Message ($localizedData.FileParentNotFoundError -f $Path) `
-ArgumentName 'Path'
} # if
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ ConvertFrom-StringData @'
KeyFoundButNoReplacementMessage = Key '{1}' found in file '{0}' and should exist and value(s) are correct. Change not required.
KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required.
KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required.
FileNotFoundError = File '{0}' not found.
FileParentNotFoundError = File parent path '{0}' not found.
'@
24 changes: 19 additions & 5 deletions DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function Set-TargetResource

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue

if ($Type -eq 'Secret')
{
Expand All @@ -153,7 +153,14 @@ function Set-TargetResource
$Path, $Text)
} # if

$fileContent = $fileContent -Replace $Search, $Text
if ($null -eq $fileContent)
{
$fileContent = $Text
}
else
{
$fileContent = $fileContent -Replace $Search, $Text
}

Set-Content `
-Path $Path `
Expand Down Expand Up @@ -216,6 +223,12 @@ function Test-TargetResource

Assert-ParametersValid @PSBoundParameters

# Check if file being managed exists. If not return $False.
if (-not (Test-Path -Path $Path))
{
return $false
}

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

Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
Expand Down Expand Up @@ -315,11 +328,12 @@ function Assert-ParametersValid
$Secret
)

# Does the file in path exist?
if (-not (Test-Path -Path $Path))
# Does the file's parent path exist?
$parentPath = Split-Path -Path $Path -Parent
if (-not (Test-Path -Path $parentPath))
{
New-InvalidArgumentException `
-Message ($localizedData.FileNotFoundError -f $Path) `
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
-ArgumentName 'Path'
} # if
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ ConvertFrom-StringData @'
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.
StringReplaceTextMessage = String replaced by '{1}' in file '{0}'.
StringReplaceSecretMessage = String replaced by secret text in file '{0}'.
FileNotFoundError = File '{0}' not found.
FileParentNotFoundError = File parent path '{0}' not found.
'@
Loading