DSCResources/MSFT_xTokenize/MSFT_xTokenize.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# Default strings if a culture specific file cannot be imported
DATA localizedStrings
{
    # culture = "en-US"
    ConvertFrom-StringData @'
    ErrorRunningSetFunction = An error occurred while running Set-TargetResource function
    ErrorRunningTestFunction = An error occurred while running Test-TargetResource function
    Path = path = {0}
    Recursive = recurse = {0}
    SearchPattern = searchPattern = {0}
    UseTokenFiles = useTokenFiles = {0}
    Returned = Returned {0}
    ReplacingTokensParam = Replacing tokens in PSBoundParameters
    RemovingParameters = Removing parameters name and searchPattern from PSBoundParameters
    TestSingleFile = Test Single File
    Loading = Loading {0}
    NoTokensReplaced = No tokens were replaced
    TokensReplaced = Tokens replaced
    NoTokenFile = No token file found
    TestDirectory = Test Directory
    Filter = filter = {0}
    ProcessDirectory = Process Directory
    ProcessSingleFile = Process Single File
    Processing = Processing {0}
    CouldNotRead = Could not read contents of file
    Saving = Saving {0}
    ProcessingComplete = Processing complete
    ReplacingTokens = Replacing Tokens
    NullTokens = Tokens is null
    Replacing = Replacing {0} with {1}
    ReplacingFilter = Replacing filter {0} with {0}.token
    GetFiles = Get Files
    BuildHashtable = Build hashtable
    HashEntry = Key: {0}, Value: {1}
'@

}

# Load localized strings
Import-LocalizedData -BindingVariable localizedStrings -FileName MSFT_xTokenize.psd1 -ErrorAction:SilentlyContinue

function Get-TargetResource
{
   [CmdletBinding()]
   [OutputType([System.Collections.Hashtable])]
   param
   (
      [parameter(Mandatory = $true)]
      [string]
      $path,

      [bool]
      $recurse,

      [string]
      $searchPattern = "*.*",

      [Microsoft.Management.Infrastructure.CimInstance[]]
      $tokens,

      [bool]
      $useTokenFiles = $true
   )

   return @{
           Path = $path
           Recurse = $recurse
           SearchPattern = $searchPattern
           Tokens = $tokens
           UseTokenFiles = $useTokenFiles
        }
}

function Set-TargetResource
{
   [CmdletBinding()]
   param
   (
      [parameter(Mandatory = $true)]
      [string]
      $path,

      [bool]
      $recurse,

      [string]
      $searchPattern = "*.*",

      [Microsoft.Management.Infrastructure.CimInstance[]]
      $tokens,

      [bool]
      $useTokenFiles = $true
   )

   try
   {
      # Convert array into hashtable
      $tokensHashTable = ToHashtable $tokens

      # We have to remove name before splatting to ProcessDirectory
      # Pipe to Out-Null so it does not write the value of name to the
      # screen
      Write-Debug $localizedStrings.RemovingParameters
      $PSBoundParameters.Remove("name") | Out-Null
      $PSBoundParameters.Remove("searchPattern") | Out-Null
      $PSBoundParameters.Add("filter", $searchPattern)

      # We also need to replace our tokens value with what we created
      # instead
      Write-Debug $localizedStrings.ReplacingTokensParam
      $PSBoundParameters["tokens"] = $tokensHashTable

      ProcessDirectory @PSBoundParameters
   }
   catch
   {
      $exception = $_
      Write-Verbose $localizedStrings.ErrorRunningSetFunction

      while($exception.InnerException -ne $null)
      {
         $exception = $exception.InnerException

         if($exception.message -ne $null)
         {
            Write-Verbose $exception.message
         }
      }
   }
}

function Test-TargetResource
{
   [CmdletBinding()]
   [OutputType([System.Boolean])]
   param
   (
      [parameter(Mandatory = $true)]
      [string]
      $path,

      [bool]
      $recurse,

      [string]
      $searchPattern = "*.*",

      [Microsoft.Management.Infrastructure.CimInstance[]]
      $tokens,

      [bool]
      $useTokenFiles = $true
   )

   try
   {
      Write-Debug ($localizedStrings.Path -f $path)
      Write-Debug ($localizedStrings.Recursive -f $recurse)
      Write-Debug ($localizedStrings.SearchPattern -f $searchPattern)
      Write-Debug ($localizedStrings.UseTokenFiles -f $useTokenFiles)
   
      # Convert array into hashtable
      $tokensHashTable = ToHashtable $tokens

      # We have to remove name before splatting to ProcessDirectory
      # Pipe to Out-Null so it does not write the value of name to the
      # screen
      Write-Debug $localizedStrings.RemovingParameters
      $PSBoundParameters.Remove("name") | Out-Null
      $PSBoundParameters.Remove("searchPattern") | Out-Null
      $PSBoundParameters.Add("filter", $searchPattern)

      # We also need to replace our tokens value with what we created
      # instead
      Write-Debug $localizedStrings.ReplacingTokensParam
      $PSBoundParameters["tokens"] = $tokensHashTable

      $result = TestDirectory @PSBoundParameters

      Write-Verbose ($localizedStrings.Returned -f $result)

      return $result
   }
   catch
   {
      $exception = $_
      Write-Verbose $localizedStrings.ErrorRunningTestFunction

      while($exception.InnerException -ne $null)
      {
         $exception = $exception.InnerException

         if($exception.message -ne $null)
         {
            Write-Verbose $exception.message
         }
      }
   }
}

# The build process replaces the target file with the token file contents. So the
# drop location will have two identical files before the transformation.
# The goal is the load the target file and load and transform the token file. If the
# contents of the target file match the value of the transformed file or we can't find
# the token file return true. In all other cases return false.
function TestSingleFile {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
        [string] $path,
        [hashtable] $tokens,
        [bool] $useTokenFiles
    )

    Write-Verbose $localizedStrings.TestSingleFile
    Write-Debug ($localizedStrings.UseTokenFiles -f $useTokenFiles)
    Write-Debug ($localizedStrings.Path -f $path)

    $result = $false

    # Assume they are not using a token file. And if so the finalFilename
    # and the provided path are the same. If a token file was used that will
    # be taken care of later.
    $finalFileName = $path
    $tokenFile = $path

    # Only remove the .token if usetokenFiles is true. There is an
    # edge condition where removing it could overwrite a file with
    # the same name minus .token that was not to be touched.
    if($useTokenFiles)
    {
        $finalFileName = $path -replace "\.token", ""
    }
        
    $file = Get-Item -Path $finalFileName    

    if(Test-Path -Path $tokenFile)
    {
        $contents = Get-Content -Path $finalFileName

        if($useTokenFiles)
        {
            Write-Verbose ($localizedStrings.Loading -f $tokenFile)
            $tokenContents = Get-Content -Path $tokenFile
            
            $processedContents = ReplaceTokens -contents $tokenContents -tokens $tokens

            if("$processedContents" -eq "$tokens")
            {
                Write-Verbose $localizedStrings.NoTokensReplaced
            }
            else
            {
                Write-Verbose $localizedStrings.TokensReplaced
            }

            # To test when you are using a token file simply load the
            # target file off disk and process the token file. If they match
            # after return true.
            # Testing this way gives you the added advantage of being able to
            # just push a configuration change. Because the token file is never
            # changed we could process it with new values and compare it to the
            # existing target file (even if the target file had been previously
            # processed) and see if they match. If they don't return false.
            $result = "$contents" -eq "$processedContents"

            Write-Verbose ($localizedStrings.Returned -f $result)
        }
        else
        {
            # If you are not using a token file all we can do is make sure
            # we can't find any of the tokens in the target file.
            # Assume all is well
            $result = $true

            foreach($token in $tokens.Keys)
            {
                $toFind = "*__$($token)__*"
                if($contents -like $toFind)
                {
                    $result = $false
                    break
                }                
            }
        }
    }
    else
    {
        Write-Verbose $localizedStrings.NoTokenFile
        $result = $true
    }

    $result
}

function TestDirectory {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
      [string] $path,
      [string] $filter,
      [hashtable] $tokens,
      [bool] $recurse,
      [bool] $useTokenFiles
    )

   Write-Debug $localizedStrings.TestDirectory
   Write-Debug ($localizedStrings.Filter -f $filter)
   Write-Debug ($localizedStrings.Path -f $path)
   Write-Debug ($localizedStrings.Recursive -f $recurse)
   Write-Debug ($localizedStrings.UseTokenFiles -f $useTokenFiles)

    $result = $true

    # We have to remove tokens before splatting to GetFiles
    # Pipe to Out-Null so it does not write the value of useTokenFiles to the
    # screen
    $PSBoundParameters.Remove("tokens") | Out-Null

    $files = GetFiles @PSBoundParameters

    if($files.Count -ne 0)
    {
        foreach($file in $files)
        {
            Write-Verbose $file
            if((TestSingleFile -path $file.FullName -tokens $tokens -useTokenFiles $useTokenFiles) -eq $false)
            {
                $result = $false
                break;
            }
        }
    }

    return $result
}

function ProcessDirectory {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
      [string] $path,
      [string] $filter,
      [hashtable] $tokens,
      [bool] $recurse,
      [bool] $useTokenFiles
    )

    Write-Debug $localizedStrings.ProcessDirectory

    # We have to remove tokens before splatting to GetFiles
    # Pipe to Out-Null so it does not write the value of useTokenFiles to the
    # screen
    $PSBoundParameters.Remove("tokens") | Out-Null

    $files = GetFiles @PSBoundParameters

    foreach($file in $files)
    {
        ProcessFile -path $file.FullName -tokens $tokens
    }
}

# Takes in the arguments from the Resource and makes sure they
# are prepared for the call to ProcessFile.
function ProcessSingleFile {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
        [string] $path,
        [hashtable] $tokens,
        [bool] $useTokenFiles
    )
    
    Write-Debug $localizedStrings.ProcessSingleFile
    Write-Debug ($localizedStrings.Path -f $path)
    Write-Debug ($localizedStrings.UseTokenFiles -f $useTokenFiles)

    $file = Get-Item -Path $path
    $tokenFile = $path

    if($useTokenFiles)
    {
        $tokenFile = "$($path).token"
    }

    if(Test-Path -Path $tokenFile)
    {
        ProcessFile -path $tokenFile -tokens $tokens
    }
    else
    {
        Write-Verbose $localizedStrings.NoTokenFile
    }
}

# This is where all the real work gets done.
# Takes a single file replaces the tokens and writes the output.
# It also takes care of removing and resetting the readonly flag
# on the target file.
function ProcessFile {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
        [string] $path,
        [hashtable] $tokens
    )

    Write-Verbose ($localizedStrings.Processing -f $path)

    $contents = Get-Content -Path $path

    if($contents -eq $null)
    {
       Write-Warning $localizedStrings.CouldNotRead
       return
    }

    $result = ReplaceTokens -contents $contents -tokens $tokens

    # Assume they are not using a token file. And if so the finalFilename
    # and the provided path are the same. If a token file was used that will
    # be taken care of later.
    $finalFileName = $path

    # Only remove the .token if usetokenFiles is true. There is an
    # edge condition where removing it could overwrite a file with
    # the same name minus .token that was not to be touched.
    if($useTokenFiles)
    {
        $finalFileName = $path -replace "\.token", ""
    }

    Write-Verbose ($localizedStrings.Saving -f $finalFileName)

    $file = Get-Item -Path $finalFileName
    $originalAttributes = $file.Attributes
    $desiredAttributes = RemoveReadyOnlyAttribute($originalAttributes)
    $file.Attributes = $desiredAttributes

    Set-Content -Path $finalFileName -Value $result

    $file.Attributes = $originalAttributes

    Write-Verbose $localizedStrings.ProcessingComplete
}

# This method simply finds and replaces all the tokens in the provided contents.
# The tokens provided in the hashtable do not contain the underscores so those
# need to be added to the keys of the hashtable before they are searched for in
# the contents.
function ReplaceTokens {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
        [object[]] $contents,
        [hashtable] $tokens
    )

    Write-Verbose $localizedStrings.ReplacingTokens

    $result = $contents

    if($tokens -eq $null)
    {
        Write-Verbose $localizedStrings.NullTokens
        return $result
    }

    foreach($key in $($tokens.Keys)) {
        $value = $tokens[$key]
        $token = "__$($key)__"

        Write-Verbose ($localizedStrings.Replacing -f $token, $value)
        # Now replace all instances of token with value
        $result = $result -replace $token, $value
    }

    return $result
}

# When files are dropped into the drop location the ReadyOnly bit can be set. If that
# is the case it must be removed before the file can be transformed. This method makes
# it easy to only remove the ReadOnly bit leaving all the other attributes intact. Once
# the file is transformed the original attributes should be returned.
function RemoveReadyOnlyAttribute {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
        [System.IO.FileAttributes] $attributes
    )

    return ($attributes -band (-bnot [System.IO.FileAttributes]::ReadOnly)) -as [System.IO.FileAttributes]
}

# This will return all the files that need to be transformed.
# If the UseTokenFiles is present only files that match the
# filter AND have a .token file as well will be returned.
# If a folder contains
# web.config
# packages.config
# web.config.token
# and filter is *.config and UseTokenFiles is false both
# web.config and packages.config would be returned. However,
# if UseTokenFiles is true only web.config would be returned.
function GetFiles {
    # Use CmdletBinding to pass any -verbose flags into this function
    [CmdletBinding()]
    param
    (
      [string] $path,
      [string] $filter,
      [bool] $recurse,
      [bool] $useTokenFiles
    )

    Write-Debug $localizedStrings.GetFiles
    Write-Debug ($localizedStrings.Path -f $path)
    Write-Debug ($localizedStrings.Filter -f $filter)
    Write-Debug ($localizedStrings.Recursive -f $recurse)
    Write-Debug ($localizedStrings.UseTokenFiles -f $useTokenFiles)

    # We have to remove useTokenFiles before splatting to Get-ChildItem
    # Pipe to Out-Null so it does not write the value of useTokenFiles to the
    # screen
    $PSBoundParameters.Remove("useTokenFiles") | Out-Null

    # We only want to return files not directories so add the File switch
    $PSBoundParameters.Add("file", $true)

    if($useTokenFiles)
    {
        Write-Verbose ($localizedStrings.ReplacingFilter -f $filter)
        $PSBoundParameters["filter"] = "$($filter).token"
    }

    # Return all files that match the filter if UseTokenFiles is false or only
    # files that have a .token file as well.
    Get-ChildItem @PSBoundParameters
}

# Converts a MSFT_KeyValuePair array into a PowerShell hashtable
function ToHashtable
{
   [CmdletBinding()]
   param
   (
     [Microsoft.Management.Infrastructure.CimInstance[]] $tokens
   )

   Write-Debug $localizedStrings.BuildHashtable
   
   $hashTable = @{}

   foreach($instance in $tokens)
   {
      $hashTable.Add($instance.Key, $instance.Value)
      Write-Debug ($localizedStrings.HashEntry -f $instance.Key, $instance.Value)
   }

   return $hashTable
}

Export-ModuleMember -Function *-TargetResource