DSCResources/TextFile/TextFile.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# Import CommonHelper $script:dscResourcesFolderFilePath = Split-Path $PSScriptRoot -Parent $script:commonHelperFilePath = Join-Path -Path $script:dscResourcesFolderFilePath -ChildPath 'CommonHelper.psm1' Import-Module -Name $script:commonHelperFilePath function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [string] $Path ) $returnValue = @{ Ensure = 'Absent' Path = $Path Contents = $null } # check file exists if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { Write-Verbose ('File "{0}" not found.' -f $Path) $returnValue.Ensure = 'Absent' } else { $returnValue.Ensure = 'Present' $Item = Get-Item -LiteralPath $Path if ($Item.Length -gt 2048) { Write-Warning ('The file size is over 2 KB, so reading is canceled.') } else { $returnValue.Contents = (Get-NewContent -LiteralPath $Path -Raw) } } $returnValue } # end of Get-TargetResource function Set-TargetResource { [CmdletBinding()] param ( [Parameter()] [ValidateSet('Present', 'Absent')] [string] $Ensure = 'Present', [Parameter(Mandatory = $true)] [string] $Path, [Parameter()] [AllowEmptyString()] [string] $Contents = '', [Parameter()] [ValidateSet('utf8', 'utf8NoBOM', 'utf8BOM', 'utf32', 'unicode', 'bigendianunicode', 'ascii', 'sjis', 'Default')] [string] $Encoding = 'utf8NoBOM', [Parameter()] [ValidateSet('CRLF', 'LF')] [string] $NewLine = 'CRLF' ) # Ensure = 'Absent' if ($Ensure -eq 'Absent') { if (Test-Path -LiteralPath $Path -PathType Leaf) { Write-Verbose ("Removing File '{0}'" -f $Path) Remove-Item -LiteralPath $Path } } else { # Ensure = 'Present' # Create parent directory if not exist $ParentFolder = Split-Path -Path $Path -Parent -ErrorAction SilentlyContinue if ($ParentFolder -and (-not (Test-Path -Path $ParentFolder -PathType Container))) { $null = New-Item -Path $ParentFolder -ItemType Directory -Force -ErrorAction Stop } # Create empty file when the Contents parameter is not specified if ([string]::IsNullOrEmpty($Contents)) { $null = New-Item -Path $Path -ItemType File -Force } else { #Output text file Write-Verbose ("Creating File '{0}'" -f $Path) $Contents | Set-NewContent -Path $Path -Encoding $Encoding -NewLine $NewLine -NoNewline -Force -ErrorAction Stop } } } # end of Set-TargetResource function Test-TargetResource { [CmdletBinding()] [OutputType([bool])] param ( [Parameter()] [ValidateSet('Present', 'Absent')] [string] $Ensure = 'Present', [Parameter(Mandatory = $true)] [string] $Path, [Parameter()] [AllowEmptyString()] [string] $Contents = '', [Parameter()] [ValidateSet('utf8', 'utf8NoBOM', 'utf8BOM', 'utf32', 'unicode', 'bigendianunicode', 'ascii', 'sjis', 'Default')] [string] $Encoding = 'utf8NoBOM', [Parameter()] [ValidateSet('CRLF', 'LF')] [string] $NewLine = 'CRLF' ) if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { Write-Verbose ('The File {0} is not exist.' -f $Path) return ($Ensure -eq 'Absent') } elseif ($Ensure -eq 'Absent') { Write-Verbose ('The File {0} is exist. Test FAILED' -f $Path) return $false } else { $CurrentFile = Get-Item -LiteralPath $Path $Encoder = Get-Encoding -Encoding $Encoding -ErrorAction Stop $ContentBytes = $Contents | Convert-NewLine -NewLine $NewLine | ForEach-Object { if (($Encoding -eq 'utf8') -or ($Encoding -eq 'utf8NoBOM')) { $Encoder.GetBytes($_) } else { # Append BOM $Encoder.GetPreamble() + $Encoder.GetBytes($_) } } # Test Length if ($CurrentFile.Length -ne $ContentBytes.Length) { Write-Verbose ('File size is not matched. Test FAILED.') return $false } elseif ($ContentBytes.Length -eq 0) { Write-Verbose ('File size is matched (zero). Test PASSED.') return $true } # Test Hash try { $MemoryStream = [System.IO.MemoryStream]::New($ContentBytes) $ContentsHash = Get-FileHash -InputStream $MemoryStream -Algorithm SHA1 $CurrentFileHash = Get-FileHash -LiteralPath $Path -Algorithm SHA1 $TestResult = $CurrentFileHash.Hash -eq $ContentsHash.Hash if (-not $TestResult) { Write-Verbose ('File hash is not matched. Test FAILED.') return $false } else { Write-Verbose ('File hash is matched. Test PASSED.') return $true } } catch { Write-Error -Exception $_.Exception } finally { if ($null -ne $MemoryStream) { $MemoryStream.Close() } } } return $true } # end of Test-TargetResource |