forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertTo-RedactedText.ps1
More file actions
77 lines (59 loc) · 2.18 KB
/
ConvertTo-RedactedText.ps1
File metadata and controls
77 lines (59 loc) · 2.18 KB
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
<#
.SYNOPSIS
Redacts a text from one or more specified phrases.
.DESCRIPTION
Redacts a text using best effort from one or more specified phrases. For
it to work the sensitiv phrases must be known and passed into the parameter
RedactText. If any single character in a phrase is wrong the sensitiv
information will not be redacted. The redaction is case-insensitive.
.PARAMETER Text
Specifies the text that will be redacted.
.PARAMETER RedactPhrase
Specifies one or more phrases to redact from the text. Text strings will
be escaped so they will not be interpreted as regular expressions (RegEx).
.PARAMETER RedactWith
Specifies a phrase that will be used as redaction.
.EXAMPLE
ConvertTo-RedactedText -Text 'My secret phrase: secret123' -RedactPhrase 'secret123'
Returns the text with the phrases redacted with the default redaction phrase.
.EXAMPLE
ConvertTo-RedactedText -Text 'My secret phrase: secret123' -RedactPhrase 'secret123' -RedactWith '----'
Returns the text with the phrases redacted to '----'.
.INPUTS
`System.String`
Accepts text to be redacted via the pipeline.
.OUTPUTS
`System.String`
Returns the redacted text.
#>
function ConvertTo-RedactedText
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.String]
$Text,
[Parameter(Mandatory = $true)]
[System.String[]]
$RedactPhrase,
[Parameter()]
[System.String]
$RedactWith = '*******'
)
process
{
$redactedText = $Text
foreach ($redactString in $RedactPhrase)
{
<#
Escaping the string to handle strings which could look like
regular expressions, like passwords.
#>
$escapedRedactedString = [System.Text.RegularExpressions.Regex]::Escape($redactString)
$redactedText = $redactedText -ireplace $escapedRedactedString, $RedactWith # cSpell: ignore ireplace
}
return $redactedText
}
}